devela::all

Trait ExtSlice

Source
pub trait ExtSlice<T>: Sealed {
    // Required methods
    fn slice_lsplit(&self, len: usize) -> &[T] ;
    fn slice_rsplit(&self, len: usize) -> &[T] ;
    fn slice_msplit_left(&self, len: usize) -> &[T] ;
    fn slice_msplit_right(&self, len: usize) -> &[T] ;
    fn slice_into_array<U, const N: usize>(&self) -> [U; N]
       where T: Clone,
             U: From<T>;
    fn slice_into_vec<U>(&self) -> Vec<U> 
       where T: Clone,
             U: From<T>;
    fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E> 
       where T: Clone,
             U: TryFrom<T, Error = E>;
}
Expand description

Extension trait providing additional methods for &[T].

This trait is sealed and cannot be implemented for any other type.

Required Methods§

Source

fn slice_lsplit(&self, len: usize) -> &[T]

Returns a left subslice of slice with the given maximum len.

If left_len > slice.len() it returns the full slice.

See also Slice::lsplit for the standalone const version.

Source

fn slice_rsplit(&self, len: usize) -> &[T]

Returns a right subslice of slice with the given maximum len.

If left_len > slice.len() it returns the full slice.

See also Slice::rsplit for the standalone const version.

Source

fn slice_msplit_left(&self, len: usize) -> &[T]

Returns a middle subslice of slice with the given maximum len and a left bias.

In case of a non-perfect middle split, it will have one character more on the left.

If len > slice.len() returns the full slice.

See also Slice::msplit_left for the standalone const version.

Source

fn slice_msplit_right(&self, len: usize) -> &[T]

Returns a middle subslice of slice with the given maximum len and a right bias.

In case of a non-perfect middle split, it will have one character more on the right.

If len > slice.len() returns the full slice.

See also Slice::msplit_right for the standalone const version.

Source

fn slice_into_array<U, const N: usize>(&self) -> [U; N]
where T: Clone, U: From<T>,

Converts &[T] to [U; N] when U implements From<T>.

§Panics

Panics if the length of the slice is less than the length of the array.

§Examples
assert_eq![[1_u16, 2, 3], [1_u8, 2, 3].slice_into_array::<u16, 3>()];
assert_eq![[1_u16, 2, 3], [1_u8, 2, 3].slice_into_array::<u16, 3>()];
§Features

If the unsafe_array feature is enabled it uses MaybeUninit to improve performance.

Source

fn slice_into_vec<U>(&self) -> Vec<U>
where T: Clone, U: From<T>,

Available on crate feature alloc only.

Converts &[T] to Vec<U> when U implements From<T>.

§Examples
assert_eq![vec![1_i16, 2, 3], [1_u8, 2, 3].slice_into_vec::<i16>()];
assert_eq![vec![1_i16, 2, 3], [1_u8, 2, 3].slice_into_vec::<i16>()];
Source

fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E>
where T: Clone, U: TryFrom<T, Error = E>,

Available on crate feature alloc only.

Tries to convert &[T] to Vec<U> when U implements TryFrom<T>.

§Examples
assert_eq![Ok(vec![1_i32, 2, 3]), [1_i64, 2, 3].slice_try_into_vec()];
assert_eq![Ok(vec![1_i32, 2, 3]), [1_i64, 2, 3].slice_try_into_vec::<_, i32>()];

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<T> ExtSlice<T> for &[T]

Source§

fn slice_lsplit(&self, len: usize) -> &[T]

Source§

fn slice_rsplit(&self, len: usize) -> &[T]

Source§

fn slice_msplit_left(&self, len: usize) -> &[T]

Source§

fn slice_msplit_right(&self, len: usize) -> &[T]

Source§

fn slice_into_array<U, const N: usize>(&self) -> [U; N]
where T: Clone, U: From<T>,

Source§

fn slice_into_vec<U>(&self) -> Vec<U>
where T: Clone, U: From<T>,

Available on crate feature alloc only.
Source§

fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E>
where T: Clone, U: TryFrom<T, Error = E>,

Available on crate feature alloc only.
Source§

impl<T> ExtSlice<T> for &mut [T]

Source§

fn slice_lsplit(&self, len: usize) -> &[T]

Source§

fn slice_rsplit(&self, len: usize) -> &[T]

Source§

fn slice_msplit_left(&self, len: usize) -> &[T]

Source§

fn slice_msplit_right(&self, len: usize) -> &[T]

Source§

fn slice_into_array<U, const N: usize>(&self) -> [U; N]
where T: Clone, U: From<T>,

Source§

fn slice_into_vec<U>(&self) -> Vec<U>
where T: Clone, U: From<T>,

Available on crate feature alloc only.
Source§

fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E>
where T: Clone, U: TryFrom<T, Error = E>,

Available on crate feature alloc only.
Source§

impl<T> ExtSlice<T> for [T]

Source§

fn slice_lsplit(&self, len: usize) -> &[T]

Source§

fn slice_rsplit(&self, len: usize) -> &[T]

Source§

fn slice_msplit_left(&self, len: usize) -> &[T]

Source§

fn slice_msplit_right(&self, len: usize) -> &[T]

Source§

fn slice_into_array<U, const N: usize>(&self) -> [U; N]
where T: Clone, U: From<T>,

Source§

fn slice_into_vec<U>(&self) -> Vec<U>
where T: Clone, U: From<T>,

Available on crate feature alloc only.
Source§

fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E>
where T: Clone, U: TryFrom<T, Error = E>,

Available on crate feature alloc only.
Source§

impl<T, const LEN: usize> ExtSlice<T> for [T; LEN]

Source§

fn slice_lsplit(&self, len: usize) -> &[T]

Source§

fn slice_rsplit(&self, len: usize) -> &[T]

Source§

fn slice_msplit_left(&self, len: usize) -> &[T]

Source§

fn slice_msplit_right(&self, len: usize) -> &[T]

Source§

fn slice_into_array<U, const N: usize>(&self) -> [U; N]
where T: Clone, U: From<T>,

Source§

fn slice_into_vec<U>(&self) -> Vec<U>
where T: Clone, U: From<T>,

Available on crate feature alloc only.
Source§

fn slice_try_into_vec<E, U>(&self) -> Result<Vec<U>, E>
where T: Clone, U: TryFrom<T, Error = E>,

Available on crate feature alloc only.

Implementors§

Source§

impl<T> ExtSlice<T> for Vec<T>