Vscode-rust: Feature: Implement trait for struct

Created on 29 Jul 2017  路  8Comments  路  Source: rust-lang/vscode-rust

It would be really useful to be able to generate the function skeletons necessary to implement a trait for a struct. So for example, if I have the following:

trait Foo {
    fn do_stuff(s: &str) -> u64;
}

struct Bar {
    x: u64,
}

impl Foo for Bar {
//cursor
}

With the cursor at the indicated location, invoking the Implement Trait for Struct command would generate the following:

impl Foo for Bar {
    fn do_stuff(s: &str) -> u64 {
        unimplemented!();
    }
}
enhancement rls

Most helpful comment

Holy smokes! I use:

Version: 1.42.1
Commit: c47d83b293181d9be64f27ff093689e8e7aed054
Date: 2020-02-17T09:32:31.598Z
Electron: 6.1.7
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Linux x64 4.19.106-1-MANJARO

with rls-vscode 0.7.0 and I can implement the traits.

see it in action yourself

Thank you the holy gods of Rust and the contributors of rls-vscode!

By the way, did you forget to close the issue?

All 8 comments

This would be great!

Some thoughts on doing this:

  • afaict, there is not a good way to give input to vscode for things like refactoring, so this would probably have to work by typing impl Foo for Bar and then expanding that (I guess we could just type and select Foo and expand from there too)
  • we should probably handle super-traits - save-analysis knows about sub/super trait relationships (or did, might have been removed actually), but the RLS does not. I don't think this is that hard.
  • We already know all the items in a trait, so I think we just have to copy their signatures, and replace ; with { unimplemented! } for functions
  • However, we don't include the signature info for the RLS to save space. We would need to be able to fetch this from the compiler on demand (including for previously compiled crates, which would be challenging).
  • I wonder what we should do for default methods? Provide stubs or not?
  • Should we replace Self by Bar? We don't need to, I'm not sure which is more idiomatic.
  • Are there any other type substitutions we need to do? (I don't think so)
  • Ideally we would do any imports that need doing, but we don't have a mechanism for that yet.

If I understand you correctly, RLS doesn't currently store function signatures which you would need for this. They would probably also be helpful for another feature I'd love to see: function parameter info popups.

If I understand you correctly, RLS doesn't currently store function signatures

Correct. Save-analysis can provide it (the new rustdoc will use it) but the RLS requests not to get it to save space in the data (one of the biggest sources slowdown is processing large chunks of JSON data, so this is an important optimisation). We might be able to use a slimmed down version for both functon signature assistance (which I'd also love to have) and for this feature.

Would it be possible to get "poor man's" function signature assistance by displaying the same info one gets from hovering over the function name? Mainly this is to avoid the awkwardness of moving my hand off the keyboard to hover the mouse over the function name after I type each parameter (which makes the tooltip disappear).

Would it be possible to get "poor man's" function signature assistance by...

I'm not really sure, I really need to look into function signature assistance in more detail. It's been on my todo list for a long time, but I haven't yet looked into the details.

Holy smokes! I use:

Version: 1.42.1
Commit: c47d83b293181d9be64f27ff093689e8e7aed054
Date: 2020-02-17T09:32:31.598Z
Electron: 6.1.7
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Linux x64 4.19.106-1-MANJARO

with rls-vscode 0.7.0 and I can implement the traits.

see it in action yourself

Thank you the holy gods of Rust and the contributors of rls-vscode!

By the way, did you forget to close the issue?

@erayerdin I see
image

  • it doesn't look fixed for me. Same vscode, same rls-vscode. Are you sure it's not some other extension you have offering it, like rusty-snippets?

@rbtcollins Sorry for the late reply. I did not prefer to write to give wrong information about my development environment.

I have VSCode installed at two machines, one has this feature while the other one, oddly, does not. I have looked at the extension pack I'm using, it definitely uses this extension. I could not understand what's different.

Was this page helpful?
0 / 5 - 0 ratings