Isolate

Trait Isolate 

Source
pub trait Isolate<I> {
    type Output;

    // Required methods
    unsafe fn isolate_unchecked(self, idx: I) -> Self::Output;
    fn try_isolate(self, idx: I) -> Option<Self::Output>;

    // Provided method
    fn isolate(self, idx: I) -> Self::Output
       where Self: Sized { ... }
}
Expand description

Since we cannot alias mutable references, in order to index a mutable view of elements, we must consume the original mutable reference. Since we can’t use slices for general composable collections, its impossible to match against a &mut self in the getter function to be able to use it with owned collections, so we opt to have an interface that is designed specifically for mutably borrowed collections. For composable collections, this is better described by a subview operator, which is precisely what this trait represents. Incidentally this can also work for owned collections, which is why it’s called Isolate instead of SubView.

Required Associated Types§

Required Methods§

Source

unsafe fn isolate_unchecked(self, idx: I) -> Self::Output

Unchecked version of isolate.

§Safety

The given index must be within the bounds of this collection, otherwise this function may cause undefined behaviour (UB). In otherwords try_isolate(idx) must not be None when called with the same idx to avoid UB.

Source

fn try_isolate(self, idx: I) -> Option<Self::Output>

Provided Methods§

Source

fn isolate(self, idx: I) -> Self::Output
where Self: Sized,

Return a value at the given index. This is provided as the checked version of try_isolate that will panic if the equivalent try_isolate call is None, which typically means that the given index is out of bounds.

§Panics

This function will panic if self.get(idx) returns None.

Implementors§

Source§

impl<S, I> Isolate<I> for S
where I: IsolateIndex<Self>,

A blanket implementation of Isolate for any collection which has an implementation for IsolateIndex.