superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

Solution for multiple mutable field accesses

Open michaelsproul opened this issue 3 years ago • 0 comments

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),
    }
}

michaelsproul avatar Mar 17 '21 04:03 michaelsproul