[−][src]Struct smart_access::iter_mut::Slice
A wrapper for a slice of references.
Available only through a (possibly mutable) reference.
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> Slice<T> where
T: ?Sized,
[src]
T: ?Sized,
pub fn new<'x>(slice: &'a [&'x T]) -> &'a Slice<T> where
'x: 'a,
[src]
'x: 'a,
The same as slice.into()
.
pub fn as_ref(&'a self) -> &'a [&'a T]
[src]
The original slice.
pub fn new_mut<'x>(slice: &'a mut [&'x mut T]) -> &'a mut Slice<T> where
'x: 'a,
[src]
'x: 'a,
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]
F: FnOnce(&'a mut [&'a mut T]) -> R,
Provides an access to the underlying slice of references via CPS.
Trait Implementations
impl<T> At<Range<usize>> for Slice<T>
[src]
type View = Slice<T>
fn access_at<R, F>(&mut self, i: Range<usize>, f: F) -> Option<R> where
F: FnOnce(&mut Self::View) -> R,
[src]
F: FnOnce(&mut Self::View) -> R,
impl<T> At<RangeFrom<usize>> for Slice<T>
[src]
type View = Slice<T>
fn access_at<R, F>(&mut self, i: RangeFrom<usize>, f: F) -> Option<R> where
F: FnOnce(&mut Self::View) -> R,
[src]
F: FnOnce(&mut Self::View) -> R,
impl<T> At<RangeFull> for Slice<T>
[src]
type View = Slice<T>
fn access_at<R, F>(&mut self, i: RangeFull, f: F) -> Option<R> where
F: FnOnce(&mut Self::View) -> R,
[src]
F: FnOnce(&mut Self::View) -> R,
impl<T> At<RangeInclusive<usize>> for Slice<T>
[src]
type View = Slice<T>
fn access_at<R, F>(&mut self, i: RangeInclusive<usize>, f: F) -> Option<R> where
F: FnOnce(&mut Self::View) -> R,
[src]
F: FnOnce(&mut Self::View) -> R,
impl<T> At<RangeTo<usize>> for Slice<T>
[src]
type View = Slice<T>
fn access_at<R, F>(&mut self, i: RangeTo<usize>, f: F) -> Option<R> where
F: FnOnce(&mut Self::View) -> R,
[src]
F: FnOnce(&mut Self::View) -> R,
impl<T> At<RangeToInclusive<usize>> for Slice<T>
[src]
type View = Slice<T>
fn access_at<R, F>(&mut self, i: RangeToInclusive<usize>, f: F) -> Option<R> where
F: FnOnce(&mut Self::View) -> R,
[src]
F: FnOnce(&mut Self::View) -> R,
impl<T> At<usize> for Slice<T>
[src]
type View = T
fn access_at<R, F>(&mut self, i: usize, f: F) -> Option<R> where
F: FnOnce(&mut T) -> R,
[src]
F: FnOnce(&mut T) -> R,
impl<'a, 'x, T> From<&'a [&'x T]> for &'a Slice<T> where
'x: 'a,
T: ?Sized,
[src]
'x: 'a,
T: ?Sized,
impl<'a, 'x, T> From<&'a mut [&'x mut T]> for &'a mut Slice<T> where
'x: 'a,
T: ?Sized,
[src]
'x: 'a,
T: ?Sized,