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

Add VariantData#fields_mut #26

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ impl VariantData {
VariantData::Unit => &[],
}
}

pub fn fields_mut(&mut self) -> &mut [Field] {
match *self {
VariantData::Struct(ref mut fields) |
VariantData::Tuple(ref mut fields) => fields,
VariantData::Unit => &mut [],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was shocked that this worked. I get that it's completely valid from a memory safety point of view since there's no operation that could actually mutate an empty slice, but I didn't think the compiler would special case empty slices vs non-empty here.

}
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down