Skip to main content

SliceExt

Trait SliceExt 

Source
pub trait SliceExt {
    type Item;

    // Required methods
    fn bitcoin_as_chunks<const N: usize>(
        &self,
    ) -> (&[[Self::Item; N]], &[Self::Item]);
    fn bitcoin_as_chunks_mut<const N: usize>(
        &mut self,
    ) -> (&mut [[Self::Item; N]], &mut [Self::Item]);
    fn get_array<const ARRAY_LEN: usize>(
        &self,
        offset: usize,
    ) -> Option<&[Self::Item; ARRAY_LEN]>;
    fn split_first_chunk<const ARRAY_LEN: usize>(
        &self,
    ) -> Option<(&[Self::Item; ARRAY_LEN], &[Self::Item])>;
    fn split_last_chunk<const ARRAY_LEN: usize>(
        &self,
    ) -> Option<(&[Self::Item], &[Self::Item; ARRAY_LEN])>;
}
Expand description

Extension trait for slice.

Required Associated Types§

Source

type Item

The item type the slice is storing.

Required Methods§

Source

fn bitcoin_as_chunks<const N: usize>( &self, ) -> (&[[Self::Item; N]], &[Self::Item])

Splits up the slice into a slice of arrays and a remainder.

Note that N must not be zero:

let slice = [1, 2, 3];
let _fail = slice.bitcoin_as_chunks::<0>(); // Fails to compile
Source

fn bitcoin_as_chunks_mut<const N: usize>( &mut self, ) -> (&mut [[Self::Item; N]], &mut [Self::Item])

Splits up the slice into a slice of arrays and a remainder.

Note that N must not be zero:

let mut slice = [1, 2, 3];
let _fail = slice.bitcoin_as_chunks_mut::<0>(); // Fails to compile
Source

fn get_array<const ARRAY_LEN: usize>( &self, offset: usize, ) -> Option<&[Self::Item; ARRAY_LEN]>

Tries to access a sub-array of length ARRAY_LEN at the specified offset.

Returns None in case of out-of-bounds access.

Source

fn split_first_chunk<const ARRAY_LEN: usize>( &self, ) -> Option<(&[Self::Item; ARRAY_LEN], &[Self::Item])>

Splits the slice into an array and remainder if it’s long enough.

Returns None if the slice is shorter than ARRAY_LEN

Source

fn split_last_chunk<const ARRAY_LEN: usize>( &self, ) -> Option<(&[Self::Item], &[Self::Item; ARRAY_LEN])>

Splits the slice into a remainder and an array if it’s long enough.

Returns None if the slice is shorter than ARRAY_LEN

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T> SliceExt for [T]

Source§

type Item = T

Source§

fn bitcoin_as_chunks<const N: usize>( &self, ) -> (&[[Self::Item; N]], &[Self::Item])

Source§

fn bitcoin_as_chunks_mut<const N: usize>( &mut self, ) -> (&mut [[Self::Item; N]], &mut [Self::Item])

Source§

fn get_array<const ARRAY_LEN: usize>( &self, offset: usize, ) -> Option<&[Self::Item; ARRAY_LEN]>

Source§

fn split_first_chunk<const ARRAY_LEN: usize>( &self, ) -> Option<(&[Self::Item; ARRAY_LEN], &[Self::Item])>

Source§

fn split_last_chunk<const ARRAY_LEN: usize>( &self, ) -> Option<(&[Self::Item], &[Self::Item; ARRAY_LEN])>

Implementors§