Skip to content
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

Sameline match nested struct destructuring #136

Open
knappador opened this issue Feb 14, 2019 · 0 comments
Open

Sameline match nested struct destructuring #136

knappador opened this issue Feb 14, 2019 · 0 comments

Comments

@knappador
Copy link

Libraries using nested enums with struct members lead to deep pattern matching to extract the desired data. When starting a nested destructure, placing a newline before the type of the item and the first field does not add any additional visual cues about what is being destructured but does add unnecessary whitespace that makes it harder to differentiate the nested destructure in the match arm from match bodies and match/body pairs.

indent_style = "Visual" and several other options appear not to convince rustfmt to put these names and types on the same line, resulting in match patterns visually larger than the data being matched.

Data being destructured

/*
    WindowEvent {
        event: KeyboardInput {
            input: KeyboardInput {
                scancode: 33,
                state: Released,
                modifiers: ModifiersState {
                    shift: false, ctrl: false, alt: false, logo: false
                }
            }
        }
    }
*/

fn mod_keys_off(&ev: Event) -> bool {
    // function body unindented below...
}

With several compact and visual indent options on we still get so many lines

match ev {
    Event::WindowEvent {
        event:  // why 
        WindowEvent::KeyboardInput {
            input:  // so many
            KeyboardInput {
                modifiers: // newlines?
                ModifiersState {
                    shift: false,
                    ctrl: false,
                    alt: false,
                    logo: false,
                    ..
                },
                ..
            },
            ..
        },
        ..
    } => true,
    // additional match arms
}

Without newlines between nested destructure name & type

match ev {    
    // How it should be
    Event::WindowEvent {
        event: WindowEvent::KeyboardInput {
            input: KeyboardInput {
                modifiers: ModifiersState {
                    shift: false,
                    ctrl: false,
                    alt: false,
                    logo: false,
                    ..
                },
                ..
            },
            ..
        },
        ..
    } => true,
    // additional match arms
}

We still can pile up quite a bit of vertical space with the field omissions and closing braces, but moving any braces seems to require breaking up each member's fields into two indent levels (if .. is considered a field) and the match arm shouldn't be moved under any circumstance, so there's a unique solution, which we're already at in this example.

rustfmt.toml used to obtain the odious long version

use_small_heuristics = "Max"
brace_style = "PreferSameLine"
fn_single_line = true
where_single_line = true
indent_style = "Visual"
use_field_init_shorthand = true
match_arm_blocks = false
overflow_delimited_expr = true

What I recommend is a similar option overflow_delimited_expr = true but applied to nested destructuring, not just function argument lists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant