Struct Slice

Source
pub struct Slice<T: ?Sized> { /* private fields */ }
Expand description

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§

Source§

impl<'a, T: ?Sized> Slice<T>

Source

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

The same as slice.into().

Source

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

The original slice.

Source

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

The same as pair_ref.into().

Source

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

The original slice, mutable version.

Source

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

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

Trait Implementations§

Source§

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

Source§

fn from(slice: &'a [&'x T]) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(slice: &'a mut [&'x mut T]) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for Slice<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for Slice<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> !Send for Slice<T>

§

impl<T> !Sized for Slice<T>

§

impl<T> !Sync for Slice<T>

§

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

§

impl<T> UnwindSafe for Slice<T>
where T: RefUnwindSafe + ?Sized,

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