[][src]Struct loaf::Loaf

#[repr(C)]pub struct Loaf<T> {
    pub loaf: [T; 1],
    pub rest: [T],
}

Slice that guarantees to have at least one element

Usage

The implementation is very minimal, it only contains things that have reason to be here. If you want to use slice iterators or other slice methods, consider using them indirectly via as_slice or as_mut_slice

Fields

loaf: [T; 1]rest: [T]

Implementations

impl<T> Loaf<T>[src]

pub fn len(&self) -> usize[src]

Returns length of the underlying slice

let slice = &[0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice(slice).unwrap();
assert_eq!(loaf.len(), slice.len());

pub fn first(&self) -> &T[src]

Returns a reference to the first element

let slice = &[0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice(slice).unwrap();
assert_eq!(*loaf.first(), 0);

pub fn first_mut(&mut self) -> &mut T[src]

Returns a mutable reference to the first element

let slice = &mut [0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice_mut(slice).unwrap();
*loaf.first_mut() = 42;
assert_eq!(*loaf.first(), 42);

pub fn last(&self) -> &T[src]

Returns a reference to the last element

let slice = &[0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice(slice).unwrap();
assert_eq!(*loaf.last(), 4);

pub fn last_mut(&mut self) -> &mut T[src]

Returns a mutable reference to the last element

let slice = &mut [0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice_mut(slice).unwrap();
*loaf.last_mut() = 42;
assert_eq!(*loaf.last(), 42);

pub fn split_first(&self) -> (&T, &[T])[src]

Returns a reference to the first element and the rest of slice

let slice = &[0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice(slice).unwrap();
let (first, rest) = loaf.split_first();
assert_eq!(*first, 0);
assert_eq!(rest, &[1, 2, 3, 4]);

pub fn split_first_mut(&mut self) -> (&mut T, &mut [T])[src]

Returns a mutable reference to the first element and the rest of slice

let slice = &mut [0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice_mut(slice).unwrap();
let (first, rest) = loaf.split_first_mut();
*first = 40;
rest[0] = 41;
// slice[0] = 0; // this line does not compile, because slice is borrowed mutably
assert_eq!(*first, 40);
assert_eq!(rest, &[41, 2, 3, 4]);

pub fn from_slice(slice: &[T]) -> Option<&Self>[src]

Casts a &[T] with at least one element into &Loaf<T>. If slice does not contain any element, None is returned

let slice = &[0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice(slice).unwrap();
assert_eq!(loaf.loaf, [0]);
assert_eq!(loaf.rest, [1, 2, 3, 4]);

let slice: &[u8] = &[];
let optionloaf = Loaf::from_slice(slice);
assert!(optionloaf.is_none());

pub fn from_slice_mut(slice: &mut [T]) -> Option<&mut Self>[src]

Casts a &mut [T] with at least one element into &mut Loaf<T>. If slice does not contain any element, None is returned

let slice = &mut [0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice_mut(slice).unwrap();
loaf.loaf[0] = 42;
loaf.rest[3] = 14;
assert_eq!(slice, &[42u8, 1, 2, 3, 14]);

pub fn as_slice(&self) -> &[T][src]

Casts &Loaf<T> into &[T]

let slice = &[0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice(slice).unwrap();
assert_eq!(loaf.as_slice(), &[0u8, 1, 2, 3, 4]);

pub fn as_mut_slice(&mut self) -> &mut [T][src]

Casts &mut Loaf<T> into &mut [T]

let slice = &mut [0u8, 1, 2, 3, 4];
let loaf = Loaf::from_slice_mut(slice).unwrap();
loaf.loaf[0] = 42;
loaf.rest[3] = 14;
assert_eq!(loaf.as_slice(), &[42u8, 1, 2, 3, 14]);

pub unsafe fn from_slice_unchecked(slice: &[T]) -> &Self[src]

Casts a &[T] into &Loaf<T>.

Safety

Slice needs to hold at least one element

pub unsafe fn from_slice_mut_unchecked(slice: &mut [T]) -> &mut Self[src]

Casts a &mut [T] into &mut Loaf<T>.

Safety

Slice needs to hold at least one element

impl<T> Loaf<T>[src]

Avaliable with alloc feature

pub fn try_from_boxed_slice(boxed: Box<[T]>) -> Result<Box<Self>, Box<[T]>>[src]

Consumes a boxed slice returning a boxed Loaf.
If length of the slice is zero, the Box is returned back as error

let x: Box<[u8]> = Box::new([1, 2, 3]);
let loaf = Loaf::try_from_boxed_slice(x).unwrap();
assert_eq!(loaf.loaf, [1u8]);
assert_eq!(loaf.rest, [2u8, 3]);

let x: Box<[u8]> = Box::new([]);
let b: Box<[u8]> = match Loaf::try_from_boxed_slice(x) {
    Ok(_) => unreachable!(),
    Err(b) => b,
};

pub fn into_boxed_slice(self: Box<Self>) -> Box<[T]>[src]

Consumes a boxed Loaf returning a boxed slice

let x: Box<[u8]> = Box::new([1, 2, 3]);
let loaf = Loaf::try_from_boxed_slice(x).unwrap();
assert_eq!(loaf.loaf, [1u8]);
assert_eq!(loaf.rest, [2u8, 3]);
assert_eq!(loaf.into_boxed_slice().as_ref(), &[1u8, 2, 3]);

Trait Implementations

impl<T> Deref for Loaf<T>[src]

type Target = [T]

The resulting type after dereferencing.

impl<T> DerefMut for Loaf<T>[src]

Auto Trait Implementations

impl<T> Send for Loaf<T> where
    T: Send

impl<T> Sync for Loaf<T> where
    T: Sync

impl<T> Unpin for Loaf<T> where
    T: Unpin

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]