Struct forma::buffer::layout::SliceCache

pub struct SliceCache { /* private fields */ }
Expand description

A cache of non-overlapping mutable sub-slices of that enforces lifetimes dynamically.

This type is useful when you have to give up on the mutable reference to a slice but need a way to cache mutable sub-slices deriving from it.

§Examples

let mut array = [1, 2, 3];

let mut cache = SliceCache::new(3, |span| {
    let (left, right) = span.split_at(1);
    Box::new([left, right])
});

for slice in cache.access(&mut array).unwrap().iter_mut() {
    for val in slice.iter_mut() {
        *val += 1;
    }
}

assert_eq!(array, [2, 3, 4]);

Implementations§

§

impl SliceCache

pub fn new<F>(len: usize, f: F) -> SliceCache
where F: Fn(Span<'_>) -> Box<[Span<'_>]> + 'static,

Creates a new slice cache by storing sub-spans created from a root passed to the closure f. len is the minimum slice length that can then be passed to access.

§Examples
let mut cache = SliceCache::new(3, |span| {
    let (left, right) = span.split_at(1);
    // All returned sub-spans stem from the span passed above.
    Box::new([left, right])
});

pub fn access<'c, 's, T>( &'c mut self, slice: &'s mut [T] ) -> Option<Ref<'c, [Slice<'s, T>]>>

Accesses the slice by returning all the sub-slices equivalent to the previously created spans in the closure passed to new.

If the slice does not have a length at least as large as the one passed to new, this function returns None.

Note: this method should not be called concurrently with any other access calls since it will wait for the previously returned Ref to be dropped.

§Examples
let mut array = [1, 2, 3];

let mut cache = SliceCache::new(3, |span| {
    let (left, right) = span.split_at(1);
    Box::new([left, right])
});

let mut copy = array;
let skipped_one = cache.access(&mut array).unwrap();

assert_eq!(&*skipped_one[1], &copy[1..]);

Trait Implementations§

§

impl Debug for SliceCache

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V