Trait SliceSpread

Source
pub trait SliceSpread<T>: Slice<Item = T> {
    // Required methods
    const fn spread<const M: usize>(&self) -> [&[Padded<T, M>]; M]
       where [(); { _ }]:;
    const fn spread_mut<const M: usize>(&mut self) -> [&mut [Padded<T, M>]; M]
       where [(); { _ }]:;
}

Required Methods§

Source

const fn spread<const M: usize>(&self) -> [&[Padded<T, M>]; M]
where [(); { _ }]:,

Spreads elements equally across M slices. Slices will have equal length only if the operand slice’s length is divisible by M.

§Example

Take, for instance, that we want to divide a slice into odd and even elements:

#![feature(generic_const_exprs)]
 
use slice_ops::ops::*;
 
let arr = [1, 2, 3];
let slice = arr.as_slice();
 
let [odd, even] = slice.spread();
 
assert_eq!(odd, [1, 3]);
assert_eq!(even, [2]);
Source

const fn spread_mut<const M: usize>(&mut self) -> [&mut [Padded<T, M>]; M]
where [(); { _ }]:,

Spreads elements equally across M mutable slices. Slices will have equal length only if the operand slice’s length is divisible by M.

§Example
#![feature(generic_const_exprs)]
 
use slice_ops::ops::*;
 
let mut arr = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"];
let slice = arr.as_mut_slice();
 
let [_, _, fizz] = slice.spread_mut::<3>();
assert_eq!(fizz, ["3", "6", "9", "12", "15"]);
for fizz in fizz.iter_mut()
{
    **fizz = "fizz";
}
 
let [_, _, _, _, buzz] = slice.spread_mut::<5>();
assert_eq!(buzz, ["5", "10", "fizz"]);
for buzz in buzz.iter_mut()
{
    if **buzz == "fizz"
    {
        **buzz = "fizzbuzz";
        continue;
    }
    **buzz = "buzz";
}
 
assert_eq!(arr, ["1", "2", "fizz", "4", "buzz", "fizz", "7", "8", "fizz", "buzz", "11", "fizz", "13", "14", "fizzbuzz"]);

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> SliceSpread<T> for [T]

Source§

fn spread<const M: usize>(&self) -> [&[Padded<T, M>]; M]
where [(); { _ }]:,

Source§

fn spread_mut<const M: usize>(&mut self) -> [&mut [Padded<T, M>]; M]
where [(); { _ }]:,

Implementors§