[][src]Struct multiref::Slice

#[repr(transparent)]pub struct Slice<T: ?Sized> { /* fields omitted */ }

A wrapper for a slice of references.

Available only through a (possibly mutable) reference.

Usage

Can be created from (a (possibly mutable) reference to) a slice of (possibly mutable) references by means of the From trait or with the help of new and new_mut functions.

The current version of the crate provides only a minimal viable interface: the distributive laws and the modify method.

To get a concrete reference (or a sublice) out of the Slice you can write

let (mut a, mut b, mut c, mut d) = (1, 2, 3, 4);
let mut array = [&mut a, &mut b, &mut c, &mut d];
let slice = Slice::new_mut(&mut array[..]);

// Very clumsy but works!
*slice.as_mut()[0] = 4;
*((&mut slice.as_mut()[1..3])[0]) = 5;

// Continuation-passing style is a little more convenient:
let forty_two = slice.modify(|real_slice| { *real_slice[2] = 6; 42 });
assert!(forty_two == 42);

// Modifications can be chained:
slice.modify(|real_slice| {
    *real_slice[3] += 1;
    Slice::new_mut(real_slice)
}).modify(|real_slice| { 
    *real_slice[3] += 2; 
});

assert!(a == 4);
assert!(b == 5);
assert!(c == 6);
assert!(d == 7);

Next versions of the crate are expected to provide an interface analogous to the one of standard slices (unfortunately, the lazy solution, i.e. implementing the Deref trait, can't be used, because of the necessary &-head of the type).

Implementations

impl<'a, T: ?Sized> Slice<T>[src]

pub fn new<'x: 'a>(slice: &'a [&'x T]) -> &'a Self[src]

The same as slice.into().

pub fn as_ref(&'a self) -> &'a [&'a T][src]

The original slice.

pub fn new_mut<'x: 'a>(slice: &'a mut [&'x mut T]) -> &'a mut Self[src]

The same as pair_ref.into().

pub fn as_mut(&'a mut self) -> &'a mut [&'a mut T][src]

The original slice, mutable version.

pub fn modify<R, F>(&'a mut self, f: F) -> R where
    F: FnOnce(&'a mut [&'a mut T]) -> R, 
[src]

Provides an access to the underlying slice of references via CPS.

Trait Implementations

impl<'a, 'x: 'a, T> From<&'a [&'x T]> for &'a Slice<T> where
    T: ?Sized
[src]

impl<'a, 'x: 'a, T> From<&'a mut [&'x mut T]> for &'a mut Slice<T> where
    T: ?Sized
[src]

Auto Trait Implementations

impl<T> !Send for Slice<T>

impl<T> !Sync for Slice<T>

impl<T: ?Sized> Unpin for Slice<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]