- "child place(s)": similar in concept to a struct field but opaque to the borrow checker, examples being an array index or a hash map's entry.
- "dependant trait": a trait which abstracts over some requirement of another trait
Summary
Add a new trait and dependant unsafe trait which together abstract over the retrieval of multiple &mut to child places.
The normal trait would be added as part of the v1 prelude so that these functions are easy to find and use.
Example: (assume the trait function is called get_mut_many)
let place1: usize = ...;
let place2: usize = ...; // where place1 != place2
let mut x = ;
if let Some = x.get_mut_many
dbg!;
Motivation
This is currently something that is not possible to do in safe code in rust because the borrow checker doesn't know how to deal with indexing or other forms of child places.
This mechanism would provide a standard way for types to implement the retrieval of multiple &mut to child places.
Because this exposes a safe API it can be used by everyone easily and thus reduces the difficulty of wanting to get multiple &mut into data structures at arbitrary locations.
Guide-level explanation
Two new traits are added the core called GetMutMany and GetMutManyRaw.
It is not needed to import GetMutMany as it is part of the prelude.
There is a provided implementation of GetMutMany in terms of types that implement GetMutManyRaw.
It is then possible to implement either trait to get this functionality.
Unless there is a good reason to, it is recommended to implement GetMutManyRaw.
This is to reuse the unsafe code within the default implementation as much as possible.
Normally, when rust code uses the IndexMut or BorrowMut trait it borrows the whole owned value and returns a single &mut.
However, for some data structures, such as arrays and vectors, it is possible and sometimes desirable to get more than one &mut out.
This is because these data structures represent a managed collection of values which are logically (and from a memory safety point of view) disjoint from each other.
This trait is the common way for a data structure to expose this disjoint nature of its child places.
When the get_mut_many method is called it checks that all keys are valid and that none of the resulting &mut would violate the &mut contracts.
If either of those are false then None is returned.
Examples:
Individual Entries:
let mut x = ;
if let Some = x.get_mut_many
dbg!; // [4, 1, 2, 3, 0]
Reference-level explanation
Traits:
/// This trait is unsafe to implement because it is used in upholding the
/// safety guarantees of `GetMutMany`
unsafe
- The default implementation is present to make this feature easier for implementers to use.
- Because of the default implementation there is an
unsafe traitto abstract over the type used forRawEntry. - This allows for the easy adding of new impls.
RawEntryis also required to correctly handle ZSTs, because two different ZSTs of the same type can exist at the same memory address. So it is necessary to have some way of differentiating between them.
For example the following could be an implementation of this trait for arrays. Even accounting for ZSTs:
unsafe
This second examples will be for the BTreeMap<K, V> type.
Since this type works over user defined types (for its keys) this example shows the validity of the unsafe code in the default impl.
unsafe
Drawbacks
- This adds another trait to the standard library which could be a crate.
- This adds another trait to the prelude (for visibility reasons)
- This could in theory be implemented in some form by the borrow checker in the future and having these traits might lead some to question if that is needed.
- Doesn't support ranges currently because each range is a different type and the return type would have to be between individual items (
&mut T) and multiple items (&mut [T]).
Rationale and alternatives
get_mut_manymight panic instead of returningNonelike other indexing traits. If this was the case then it might also be a good idea to have atry_get_mut_manywhich returnsNoneinstead of panicking.- Add some form of multiple disjoint (and arbitrary) child place borrowing in the language instead of on top of it.
- Add more specific array patterns into the language (specifying indices).
Though this has been noted as not being something that is currently desired (non-
RangeFullin patterns). - Implement such functionality separately for all types with no trait backing.
This might be a good idea because the trait doesn't seem very helpful to begin with (it is rather complicated) and might not be useful to be able to abstract over types that can
get_mut_many. - A different name, more in line with the other indexing operational traits might be better, for instance something like
IndexMutMany. However, since this RFC is not proposing augmenting the indexing syntax that might get a bit confusing.
Prior art
This was discussed on rust internals.
It started as a way to transform just arrays into an array of borrows but was soon found that while that already basically exists, it does not allow for arbitrary "picking" because of the limitations of array pattern.
However, that doesn't really help with other types that will probably never get structural patterns against them (ie HashMap).
This was sort of inspired by the deferred borrows paper. This is not in fact a solution at all to the problems set out in that paper, but arguably a stepping stone. This is because that paper points to a possible future where types can define what sort of deferred borrows they support. Whereas in this feature types can define what sort of multiple child place borrows they support.
HashBrown has recently added a similar sort of API (currently unstable).
Its API is slightly different as each element is returned as Result<&mut T, UnavailableMutError>.
This means that each element has to be checked by the caller as well.
That is a possible alternative to the API but makes the simple general case (all or nothing) much more difficult to use.
Perhaps supporting both would be useful as it doesn't add too much complexity.
Unresolved questions
- The names of the traits
- The names of the functions
- Individual impls or a trait
Future possibilities
Either individual impls or a trait backing, it seems reasonable to add support for range based child place slice borrowing. However, because this involves at least 6 distinct types:
It doesn't seem likely that this would be trait backed.
Instead would probably be macro implemented like the old array impls.
Until such time as Rust gains a variadic generics support (at the very least).