superstruct
superstruct copied to clipboard
Solution for multiple mutable field accesses
Unlike regular fields, superstruct
's mutable getters don't lend themselves to disjoint mutable borrows, i.e. borrowing field1
and field2
simultaneously, where at least one of the borrows is mutable.
This can be worked around on a case-by-case basis by writing helper functions like this, but I wonder if we should generate these automatically for given pairs/lists of fields:
/// Convenience accessor for validators and balances simultaneously.
pub fn validators_and_balances_mut(&mut self) -> (&mut [Validator], &mut [u64]) {
match self {
BeaconState::Base(state) => (&mut state.validators, &mut state.balances),
BeaconState::Altair(state) => (&mut state.validators, &mut state.balances),
}
}