Crate array_insertion

source ·
Expand description

Insert an element into the array.

§Examples

use std::mem::MaybeUninit;

use array_insertion::array_insert;

fn uninit_array<T, const N: usize>() -> [MaybeUninit<T>; N] {
    unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() }
}

let mut array = uninit_array::<String, 10>();
array[0].write("A".to_owned());
array[1].write("B".to_owned());
array[2].write("C".to_owned());
array[3].write("E".to_owned());
array[4].write("F".to_owned());

unsafe {
    array_insert(&mut array, 3, 5, "D".to_owned());

    let init = &*(&array[..6] as *const [_] as *const [String]);
    assert_eq!(init, ["A", "B", "C", "D", "E", "F"]);

    for e in &mut array[..6] {
        e.assume_init_drop();
    }
}
use std::mem::MaybeUninit;

use array_insertion::array_splice;

fn uninit_array<T, const N: usize>() -> [MaybeUninit<T>; N] {
    unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() }
}

let mut dst = uninit_array::<String, 10>();
dst[0].write("A".to_owned());
dst[1].write("B".to_owned());
dst[2].write("C".to_owned());
dst[3].write("F".to_owned());
dst[4].write("G".to_owned());
let mut src = uninit_array::<String, 10>();
src[0].write("D".to_owned());
src[1].write("E".to_owned());

unsafe {
    array_splice(&mut dst, 3, 5, &src, 2);

    let init = &*(&dst[..7] as *const [_] as *const [String]);
    assert_eq!(init, ["A", "B", "C", "D", "E", "F", "G"]);

    for e in &mut dst[..7] {
        e.assume_init_drop();
    }
}

Functions§