11 ms·
Rust extension traits, greppability and IDEs
- melissalobos 5y agoI feel like this is a pretty reasonable article, and it makes a good point. Much like Lisp most Rust codebases that I have seen are written usually by smaller teams of people. The issue the Author brings up is one that would be a problem if you had a very large team. Does anyone have any experience working on a large Rust project where there was extensive use of Traits?
- masklinn 5y ago> "Interesting", you think, "I didn't know that Vec has a write_u16 method". You quickly check the documentation - indeed, it doesn't! So where is it coming from? You grep the project... nothing. It's nowhere in the imports. You examine the imports one by one > [...] > It's entirely possible that using a language like Rust without a sophisticated IDE is madness, and I'm somewhat stuck in the past. But I have to say, I do lament the loss of greppability. Seems more like Eli has completely missed an important and integral feature of the rust ecosystem: `cargo doc`. While the stdlib sadly remains out of it (long-standing RFC 2324[0]), cargo doc will otherwise generate the documentation including all dependencies by default. This means you can in fact get this information by "checking the documentation", just the right one: a global search in the project's `cargo doc` will quickly tell you where the method comes from[1], without the need for any IDE or complicated integration (though I don't think it works with text browsers as it uses javascript, for terminal / console contexts maybe try `rusty-man`, I don't know how good it is tho). I couldn't imagine working on a Rust project without having its `cargo doc` permanently open in a tab, having the unified offline documentation for all your dependencies is just way too useful, I run `cargo doc` almost as often as I run `cargo check` (despite that being mostly useless for binary crates...) [0] https://github.com/rust-lang/rfcs/issues/2324 https://github.com/rust-lang/rfcs/issues/2324 [1] just tried it on a local project, the first methods it surfaces are 4 methods from byteorder (on ByteOrder, WriteBytesEx, BigEndian, and LittleEndian) and one from serde_json (on Formatter).
- arghwhat 5y agoCargo doc is nowhere near as useful as something like go doc, where giving a symbol name immediately prints plain text documentation in the terminal. Cargo doc is just… inconvenient in comparison.
- masklinn 5y agoNot only is that not relevant (that two commands have the same name doesn’t mean they have the same purpose), as far as I know you’ve managed to bring up “go doc” in a context where it is useless: if you give a symbol to `go doc`, it’s going to search that in the current package, which for the purpose of TFA is no more helpful than `grep`.
- arghwhat 5y agoThat go doc does not currently handle rust traits, a feature that has no equivalent in Go, is a pointless observation. Rust doc shows traits together with the class they are implemented for, so a “rust doc std::vec::Vec” should print them too. The point is that Rust is behind Go on convenient documentation access, which is ironic seeing that Rust requires documentation access much more than Go. go doc also accesses all documentation for packages used in a project, making it do quite a lot more than grep.
- masklinn 5y agoAs far as I know you are, once again, completely wrong. `go doc` looks up symbols in a single package, which is the current package by default. That is what go doc's own documentation states.
- arghwhat 5y agoIf you had tried using it or otherwise knew what you were talking about you would know that you use it by specifying relative or absolute import paths for any package fetched with go mod or $GOPATH. Just in case this needs an ELI5: it queries project packages to print the relevant documentation for any accessible component: “go doc container/list.List” - bar any typos from a phone keyboard - prints all documentation for the stdlib linked list. In case Rust had a decent doc tool it would be the equivalent of “rust doc std::collections::LinkedList”. (And no, that the Go tool needs to do less work to find all relevant documentation than a Rust equivalent is not relevant to the discussion.)
- Diggsey 5y agoIt's a shame when articles show non-idiomatic Rust code to people who might be not realise it :/ fn magic_num(&self) -> usize { return if self.name.len() == 0 { 2 } else { 33 }; } The `return` statement is only used for early-exit, so this would be written as: fn magic_num(&self) -> usize { if self.name.len() == 0 { 2 } else { 33 } }
- The_rationalist 5y ago
- steveklabnik 5y ago(Also clippy will probably suggest is_empty() over len() == 0)
- tialaramex 5y agoI should probably write is_empty() more often, but, it is actually doing anything that len() == 0 isn't doing, or is it just that is_empty() better explains what I presumably wanted to ask?
- spoiler 5y agoI've not looked at the implementation, so I can't answer the first question, but to answer your second point/question: yes! I think getting across intent is very important for the other people reading your code. I guess here it doesn't matter as much since it's a simple enough cases, but I'm just talking in the general sense. With that said, I think is_empty probably has very slightly smaller overhead when it comes to figuring out the author's intent. Because, well, that was the intent. Length checking against 0 is merely a method of that intent. Also, this "intent decoding" while reading code (no pun intended) adds up in mental cost, IMO. So, it wears down the the reader/reviewer attention more quickly.
- steveklabnik 5y agoFrom clippy’s docs: > Some structures can answer .is_empty() much faster than calculating their length. So it is good to get into the habit of using .is_empty(), and having it is cheap. Besides, it makes the intent clearer than a manual comparison in some contexts. It also has a lint for implementing both methods if you’ve only defined one, to help perpetuate the convention.
- thesuperbigfrog 5y agoI am not a Rust expert, but wouldn't extension traits and Unicode character look-alikes allow for a subtle attack vector? What would stop someone from creating a trait that looks similar to an existing function name, but that does something bad?
- richardwhiuk 5y agoRust emits a compiler warning if you use homoglyphs: https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#unicode-identifiers https://blog.rust-lang.org/2021/06/17/Rust-1.53.0.html#unico...
- chewbacha 5y agoYou would still need to import the malicious trait in order for it to be extended. If you already have included a bad crate which has a bad trait, then why not replace the implementation of a well-named function. I can’t see a reason to use a homoglyph in the function name.
- melissalobos 5y agoSimilar-ish concerns have been raised before: https://www.lightbluetouchpaper.org/2021/11/01/trojan-source-invisible-vulnerabilities/ https://www.lightbluetouchpaper.org/2021/11/01/trojan-source...
- capitalsigma 5y agoRemember that in Python you can mutate `globals` to change the meaning of arbitrary symbols. It seems like a lost cause if you are running a compromised library.
- cletus 5y agoSo I've used Hack extensively and I'm sympathetic on the obscurity that can come from traits. It can get completely out of control. To be fair, object hierarchies, even interface hierarchies, can also get out of control, particularly when multiple inheritance is allowed. But the real message here is if you're relying on regular expressions to implement IDE functions, plugins, language syntax checking, auto completion and so on, you're going to have a bad time. Period. This is the real weakness of VS Code IMO. An IDE that operates on the syntax of the language is infinitely better than one that relies on regular expressions and simply treating source files as "text". This is one reason I will always use Jetbrains IDEs given any choice because I know I can hover over a symbol and it'll tell me where it comes from. "Go to definition" will just work. As soon as you start designing your language practices around the limitations of what regexes can do you're going to have an even worse time. For example, in Hack (@FB) we couldn't use namespaces or trait aliasing. You then had people asking "why are people creating all these abstract final classes with static methods in them?" when the answer is obvious: because they can't use namespaces). My experience using C++ with VS Code was just horrible because the IDE was unreliable when it came to pointing out syntax or type errors. So you could waste your time compiling something that doesn't compile. I've never had this issue with CLion, for example.
- halpert 5y agoVS Code definitely has Rust language server support via the Rust extension. https://marketplace.visualstudio.com/items?itemName=rust-lang.rust https://marketplace.visualstudio.com/items?itemName=rust-lan...
- veber-alex 5y agoYou should not be using the RLS extension, it hasn't been updated in over a year. Instead you should use rust-analyzer which will soon become the official rust extension. https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer https://marketplace.visualstudio.com/items?itemName=matklad....
- School-Cotton 5y agoSorry, had to downvote this as I know people who have gotten burned by installing this plugin in the past and got confused by why their setup was so broken. What you really want is to install the rust-analyzer plugin, not the Rust plugin.
- kvark 5y agoI like to include traits closer to where they are used, to help with this. For example, “use cgmath::InnerSpace as _;” at the start of a small function. The “_” is a strong signal that I don’t need the type itself, but only properties of this trait.
- vlmutolo 5y agoThis is the first suggestion I've seen in this thread that actually helps to make the code more readable.
- chewbacha 5y agoI think I’ve unconsciously biased myself away from extension traits and have favored using traits at function boundaries instead. This still keeps the greppability and the polymorphism. But sometimes can make lifetimes trickier.
- pjmlp 5y agoStuck in the past indeed, using IDEs was already a thing in Windows 3.x and Mac OS classic, exercise for the reader when they came to be. Or better yet, the development experience of using Toga in Mesa/Cedar (1987). http://toastytech.com/guis/cedar.html http://toastytech.com/guis/cedar.html https://www.youtube.com/watch?v=z_dt7NG38V4 https://www.youtube.com/watch?v=z_dt7NG38V4 Or Smalltalk, Interlisp-D or Lisp Machines for that matter. The time a language must be usable with Notepad alone is long past due.
- nyanpasu64 5y agoThe problem with non-greppable code isn't that "you should use an IDE". I use rust-analyzer and I dislike non-greppable code. One concrete issue I ran into is that when cleaning up a list of imports, I can't use Ctrl+F to find out which ones are unused, since I can accidentally remove a trait which is never used by name but only imported for its methods. And I can't count on rust-analyzer's unused import detection working 100% of the time. And it's bad that some methods (for example the infamous `Read` or two `Write` traits) are inaccessible unless you add a trait import (which rust-analyzer sometimes knows how to add when I press Ctrl+.). To me, the overarching value of Rust is locality of reasoning, and clearly indicating nonlocal reasoning in explicitly-readable and machine-checkable ways (eg. the `Cell` family of types). Trait methods are nonlocal reasoning, and they aren't clearly demarcated (a trait import behaves as`use module::Trait::*` but isn't written that way in the source code). If you're opposed to wildcard module name imports, you should be opposed to wildcard trait method imports.
- pjmlp 5y agoThat only shows how much it is lacking versus proper IDE experiences. IDEs for other languages provide plenty of capabilities to code navigation, no need to go Ctrl+F.
- davidkunz 5y agoI like the concept of making the 99% case as easy as possible and providing a failsafe when you encounter the 1% case, here an example. People usually complain about Nim's import behaviour which is roughly an `import *` in other languages, making it hard to immediately tell where things come from. Personally, I find it fantastic because it removes a lot of boilerplate. In case of conflicts, the compiler throws an error, forcing you to qualify your import. Now in Rust, you explicitly need to import the trait in order to use it because there _might_ be naming conflicts. Here, the 1% case spoils the 99% case.
- tobz1000 5y agoI prefer the Rust/ES/Python etc. import syntax not because of ambiguity, but readability. Having everything traceable by quickly scanning the code, without IDE assistance, often speeds up my understanding of a file. And it becomes essential of you're reading code in an IDE-less environment, e.g. github gists. Worth the verbosity IMO.
- uncomputation 5y agoEach language picks its own trade-offs. I’m glad Rust errs on the side of caution for imports. Unlike most other languages, with Rust I know I can enter any file and know exactly and precisely what symbols resolve to. This not only simplifies things for me but fits my mental model of programming much better (deterministic, predictable, explicit).
- slaymaker1907 5y agoAs a big proponent of grepability, I think extension traits are still reasonable. You need to find out what the trait is that defines that method, but that often isn't too difficult. Once you know that, you just need to look at the module defining the struct and the module defining the trait. Two things that can improve this are preferring Trait::method(obj) notation and importing traits explicitly rather than relying on crate preludes.
- eyelidlessness 5y agoAs someone who has only dabbled with Rust, this: > preferring Trait::method(obj) … vastly improved my understanding of trait usage.
- wffurr 5y agoHaving magic names show up in my code without an explicit import somewhere can be really frustrating, especially in unfamiliar code bases where I'm trying to contribute a small change. I think your suggestions do a good job addressing that; could be a thing for a style guide.
- erwincoumans 5y agogreppability has a high googleability.
- eliben 5y agoNo kidding :) This post is now #3 on google for "greppability"
- jolux 5y agoThe bigger problem is that neither rust-analyzer nor IntelliJ Rust currently support go-to-definition for trait methods. They will just give you a popup with all the overloads. I understand this is being worked on with Chalk but it's quite jarring coming from Java and C#. edit: here's the GitHub thread https://github.com/rust-analyzer/rust-analyzer/issues/4558 https://github.com/rust-analyzer/rust-analyzer/issues/4558
- veber-alex 5y agoAs mentioned in that github thread, this works in JetBrains IDEs. For rust-analyzer it's blocked on improvements in Chalk.
- jolux 5y agoYeah, except I've tried it in CLion with the plugin, and it doesn't work.
- IshKebab 5y agoIt does support go to definition for trait methods. It just goes to the declaration, not the implementation. Not ideal, but still way better than just not doing anything.
- jolux 5y agoWell, it should support go to implementation too.
- staticassertion 5y agoI'll take almost any cost for extension traits. Going back to Python this is part of what I miss most. There is no way to add methods to a type in Python that mypy understands. You have to create a new type. This leads to code that is very difficult to extend and pushes you towards inheritance. I talk a bit about this here: https://insanitybit.github.io/2020/07/19/intersection-types-in-python https://insanitybit.github.io/2020/07/19/intersection-types-... For me, this became a huge problem when I was building a plugin system in Python. I wanted types to only own their own logic, but I also wanted types to be able to tell other types about themselves. For example, we have a Process and a File. I want to be able to go from Process to File with only one of those knowing about the other. Without extensions you are forced to create ugly workarounds. Also, I've never had a problem with intellij and extension traits. I kind of wonder why you'd stick with a tool that's a bad experience.
- animal_spirits 5y agoI read your blog post and TBH I'm not really sure what extension types are, I've never used rust. But I tried this solution in python and am curious is this is something you tried? >>> class A: ... def foo(self): ... print('foo') ... >>> class B: ... def bar(self): ... print('bar') ... >>> A.bar = B.bar >>> A().bar() bar Functions in python are objects as well. So while you can't do this to built in python types, there is a high likelihood you can do this to most objects in python libraries.
- staticassertion 5y agoThe problem with your code is that it won't type check. It runs just fine because CPython doesn't care about type checking. But if you run mypy on it it will not understand.
- animal_spirits 5y agoOh I see, yea you are correct
- dathinab 5y agoI wonder if the author is aware that rust has a well working, well searchable documentations you can easily open locally in case you have no internet ("rustup doc" & "cargo doc --open"). > You check the documentation of that crate and indeed, you find the write_u16 method there; phew. Like, all wild card impl. traits are in the documentation, including such which are dereferenced. EDIT: Just to be clear if you pull in other code, including potential traits, you need to open the documentation specific to your crate (cargo doc).
- Sindisil 5y ago> Who in their right mind has the courage to tackle a Java project without an IDE? /me raising hand I've used all the major IDEs on real projects for years at a time each, and so done years (often on the same projects) with just a quality editor and a set of external tools, and I honestly slightly prefer the latter. I'm effective both ways, but my observation is that my work is of better quality when working in a non-IDE environment. I fully understand that, as with most such things, says as much or more about me than about IDEs. I think a large element is that, not having auto-complete, I find myself reading more docs (increasing system familiarity) and noticing unergonomic interfaces sooner. The one feature I do miss is language aware navigation, but that loss is somewhat balanced by the lack of disruption when said navigation inevitably breaks, due to non-compiling code or working with new language features not yet supported by the tools.
- missed-pos 5y agoCan you tell more about tools you use? And what about renaming functions/variables...? It seems it's error prone without IDE/language server.
- Sindisil 5y agoNothing fancy. * Vim or VSCode - essentially zero plugins in both cases - format on save using google-java-format - syntax highlighting turned off in Vim, minimized in VSCode - code folding disabled - quickfix mode in vim and error display/navigatin in VSCode set up * maven, gradle, or ant to build * browser open to JavaDocs for stdlib and any library I happen to be using (though I work very hard to minimize dependencies) * JMC, VisualVM, JitWatch, async-profiler, etc. for profiling I do occasionally still fire up IDEA to use the debugger or other tools, but rarely. As for renaming methods/variables, for the latter, scope will almost always be small, so rename is usually trivial. For methods, it's almost always easy enough to rename, rebuild, then fix the errors. I also typically use Vim for C programing, though there I often use cscope and/or ctags to somewhat smooth out navigation. Again, I do not have a problem with good IDEs, and still occasionally use them. I just don't feel a need for them, and often prefer working in a plain editor.
- brundolf 5y agoI think it's fair to be sad that greappability is being lost. But I also don't think we should limit ourselves to it; treating it as a design goal holds back the field. Languages can be much more powerful when they drop that constraint and assume the reader has (language-aware) tooling available to help them make sense of code. I think instead we should focus on continuing to push the tooling forward. Language servers were an important step in this direction, freeing us from comprehensive IDEs, but I think that's just the tip of the iceberg. More recently we have Tree Sitter, and GitHub at least can do some basic semantic analysis of code right in their UI. How can we make it even easier to build tools that understand a language? And what new interfaces can we put on top of them? What about a CLI that lets you grep code at an AST level, maybe showing the types of the expressions it yields? What about a declarative format like TextMate that lets you describe the basics of how a type system is wired, so you don't have to craft a whole language server by hand? Maybe there's also something to be said for designing languages to be "tooling-friendly", even if that doesn't mean "as plain-text". Lowering the bar for writing tooling that can have a semantic understanding of the syntax and types, etc. There's lots to be explored here, in my opinion
- berkut 5y agoIt's not just editors/IDEs though... Which diff/merge tools support language servers currently? i.e. how do you thoroughly code-review changes without switching between a decent diff program and an IDE continually?
- brundolf 5y agoGitHub and VSCode both allow this (and I'm sure other rich editors do too). In fact, using GitLab at work, I open up most of the MRs I review in my local editor specifically for this reason
- berkut 5y agoDo you mean "github.com" or Atom their editor? Yeah, sure Rich Editors support the "basics" of diffing, but for complex (i.e. 3-way) merges, I find them pretty limiting: stuff like Beyond Compare, p4diff are more useful in those situations IMO.
- cliftonk 5y agowhat hes looking for can trivially be seen using `cargo docs` or `rust-analyzer` https://rust-analyzer.github.io/manual.html https://rust-analyzer.github.io/manual.html rust-analyzer supports vim/emacs as well. in vim i can put my cursor over a symbol and get all the information id get from an ide via a quick shortcut. a feature-rich vimrc for rust can be seen here: https://github.com/jonhoo/configs/blob/master/editor/.config/nvim/init.vim https://github.com/jonhoo/configs/blob/master/editor/.config...
- lr1970 5y agoissue `cargo doc` command to generate the documentation that will expose extension traits among other things. Not perfect, certainly. But together with rust-analizer and grep the generated docs cover all the bases.
- SquibblesRedux 5y agoI have been bit by the lack of greppability for traits a few times. I love the trait system, but I wish there was a way to track back to definitions without relying on the IDE of the day. It reminds me of old C++ nightmare scenarios where the original developers would overload all the operators and bury all sorts of redefinitions in an endless hierarchy of includes.
- camgunz 5y agoI've been digging more into Rust, and things not working because I forgot to import the trait drives me bonkers. Like, you create a struct, you build an implementation, you implement a trait on it, you try and use it in another module, you can't, you feel like you must have goofed something, you hack around for an hour, oh you remember you forgot to import the trait you already implemented, you open up HN in a fury.... Is there a really important reason rustc doesn't know what traits are implemented on what structs? It can't be a dependency graph thing, it has to know about the struct in the first place.
- deleted 5y ago[deleted]
- wyager 5y agoHaskell automatically imports all typeclass definitions (equivalent to traits) when you import a module, so you sometimes see lines like `import Foo.Bar ()` Which looks like it's just importing nothing at all (`()`) but is actually pulling in some traits and nothing else. You don't see this often because "orphan instances" (instances defined in a file besides the location of the trait definition or the type definition) are discouraged, but they do occur.
- tome 5y agoFYI that pattern is useful even in the absence of orphans.
- wyager 5y agoInteresting, where?
- tome 5y agoI use it in test suites, where the tests are simply to check that something compiles, not its behaviour, for example https://github.com/tomjaguarpaw/product-profunctors/blob/faf52f0a134099ed8adb6b29ea822d48920e7ac3/Test/Main.hs#L1 https://github.com/tomjaguarpaw/product-profunctors/blob/faf... I had also thought that there was a reason one could need that pattern to import instances even in the absence of orphans, but on reflection I think I was mistaken.
- deleted 5y ago[deleted]
- _ZeD_ 5y ago> Who in their right mind has the courage to tackle a Java project without an IDE? onestly, as an old-jeezer, I still don't understand the hate on IDEs. Who in their right mind has the courage to tackle ANY project without an IDE? I constantly see my collegues insist to use vscode without plugins, then fork or COMMANDLINE GIT (ugh..) then external tool for viewing jsons, and explicitly use postman and whatever tool... and then fail to understand the very basic fact that an integrated collections of tools works better for the end user. ...I... I don'k know...
- elsjaako 5y agoPersonally I've just have bad experiences with integrated tools like that. I'm sure if you just use it in some "normal" way it's fine, but I don't know how that is, and I've spent many hours wrestling with visual studio because the magic "easy to use" functionality stopped working or broke something. Add to that the interface changing with different versions. Having several tools that do one thing that I understand has led to less frustration for me.
- ithkuil 5y agoI tried and managed to avoid IDEs for years. Then I started to use rust for work. I accepted that I just had to use an IDE and I picked one. I was happy with CLion; it has a very good support for rust. Unfortunately I had to develop on a remote machine (because compiling rust in my local machine is too slow). Jetbrains solution for remote development just didn't work for my use case (the remote was a M1 ARM running MacOS, not supported by jetbrains gateway). So I switched to vscode. Vscode editor is great, the support for remote development is also very good. Unfortunately rust-analyzer is not as good as jetbrains's rust plugin. This is a problem with IDEs: you're either all in or you're out. Language servers did help decoupling language support from the actual window where you type stuff, but still not everything is in a language server (e.g. jetbrains rust plugin is not a reusable language server)
- pjmlp 5y agoOr just use remote desktop.
- 5y ago
- woodruffw 5y agoI strongly agree with this as someone who enjoys writing Rust and who doesn't use an IDE (or an LSP plugin, or anything like that): it's mildly annoying to have to `use` a bunch of traits to bring their contracts into scope, but for that `use` to not be explicit about the contents of those contracts. Forgetting to import `Read`, `Write`, `TryFrom`, etc. probably accounts for over 75% of my "small" compilation errors. I can't find it now, but I remember an RFC that proposes a solution to this, although in the opposite direction (i.e., more traits in the standard prelude). It would be interesting to see a `use` variant in a future edition of the language that allows users to bring in specific parts of the trait's contract, e.g. `use std::io::Read::read_exact`.
- fmorel 5y agoOne possibility could be something like C# global usings added last year. Instead of having your usings in every file, you can define them with the `global` keyword and take effect in every file in your project. It's great for common things like the `IEnumerable` extension methods which reside in `System.Linq`.
- gameswithgo 5y agoDepending on the day I will either dream of a language specifically designed with a particular IDE in mind and all the cool stuff you could do with that as a ground up assumption (google f# type providers for some inspiration), other days I dream of a language whose design specifically does not require an IDE, so you can easily understand the code from github or vim or anywhere. For instance global or function wide type inference is handy but can make non IDE use of a language really hard because you can’t immediately see what the types are. Perhaps the general design principle should be to tend towards one extreme or the other, rather than end up with a language that needs an IDE but doesn’t leverage it fully.
- rfoo 5y agoThis is also the thing I don't like in Go: sometimes it's really hard to find what interface a type implements, why a type implements (or not) a certain interface and where the f the implementations are.
- ohCh6zos 5y agoI have not found any good way to solve this in Go. At least in Rust you can grep for the impl * for <name> Edit: After thinking about it. This only really helps inside a given codebase.
- hkalbasi 5y ago> Apparently, not all tooling has access to sophisticated language servers; for example, as far as I can tell GitHub source analysis won't be able to find where write_u16 is coming from, and the same is true of Sourcegraph. FYI, Sourcegraph supports LSIF generated by rust-analyzer. For example, https://sourcegraph.com/github.com/matrix-org/matrix-rust-sdk/-/blob/crates/matrix-sdk-base/src/client.rs?L1246 https://sourcegraph.com/github.com/matrix-org/matrix-rust-sd... shows lock method belongs to tokio. It should be able to show origin of write_u16 as well (In the precise mode, not search based). Gitlab can also consume LSIF, and github should do it someday, IMO, their current way doesn't scale. In most languages you should implement a full compiler front end in order to provide a 100% correct goto definition.