-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: convert tuple to a struct by creating a new struct type #13179
Comments
I guess tracking uses would be hard, but even a minimal action would be useful: given a tuple expression: (foo, bar, baz) get variable names from it, their types, and generate a struct type with these name/type pairs: struct Untupled { foo: FooType, bar: BarType, baz: BazType } Converting tuple syntax into this mix of types and names requires too much copy-pasting. |
How can we retrive the variable names from tuples? It may be easy in the case of call with immediate destructurings, but not so in the general case. |
if no heurstic applies we can just pick whatever, like |
I realize that if a tuple is constructed from more complex expressions then there won't be a clear name to pick. However, I find this pattern pretty common: let foo = get_foo();
let bar = get_bar();
(foo, bar) which gives the tuple literal some names to use. |
Just dropping my +1 in for this. The use case I have is successive refactoring: I'm working with some WGPU code, where I have several variables being created that depend on previous state. I've been selecting these, creating a function from them (which returns a tuple), and then converting those tuples into structs, so that I go from let blah = blah;
let blah2 = something_with(&blah); to fn make_blahs() -> (Blah, Blah2) {
let blah = blah;
let blah2 = something_with(&blah);
(blah, blah2)
} to struct BlahState {
blah: Blah,
blah2: Blah2,
}
impl BlahState {
fn new() -> Self {
let blah = blah;
let blah2 = something_with(&blah);
Self { blah, blah2 }
}
} I currently have to do the last step manually - a r-a tool to automate this would make my life much easier ^_^ |
I wish rust-analyzer had a code action to refactor this:
into this:
I tend to be lazy and overuse tuples. They're convenient when I have 2-3 values to work with, but eventually I end up with a tuple of 7 values and lose track of which field is which. It'd be great to have a code action that I can point to a tuple instance, and it will replace it with a struct type, and follow all usages of this tuple (places where it has become a compile error, not all places that use the same types together) and replace them with the struct type too.
The text was updated successfully, but these errors were encountered: