Trait generic_array::sequence::Remove

source ·
pub unsafe trait Remove<T, N: ArrayLength>: GenericSequence<T> {
    type Output: GenericSequence<T>;

    // Required methods
    unsafe fn remove_unchecked(self, idx: usize) -> (T, Self::Output);
    unsafe fn swap_remove_unchecked(self, idx: usize) -> (T, Self::Output);

    // Provided methods
    fn remove(self, idx: usize) -> (T, Self::Output) { ... }
    fn swap_remove(self, idx: usize) -> (T, Self::Output) { ... }
}
Expand description

Defines a GenericSequence which can be shortened by removing an element at a given index.

§Safety

While the remove and swap_remove methods are marked safe, care must be taken when implementing it. The remove_unchecked and swap_remove_unchecked methods are unsafe and must be used with caution.

Required Associated Types§

source

type Output: GenericSequence<T>

Resulting sequence formed by removing an element at the given index.

Required Methods§

source

unsafe fn remove_unchecked(self, idx: usize) -> (T, Self::Output)

Removes an element at the given index without bounds checking, shifting elements after the given index to the left to fill the gap, resulting in a time complexity of O(n) where n=N-idx-1

See remove for an example.

§Safety

The caller must ensure that the index is within bounds, otherwise it is undefined behavior.

source

unsafe fn swap_remove_unchecked(self, idx: usize) -> (T, Self::Output)

Removes an element at the given index without bounds checking, swapping it with the last element.

See swap_remove for an example.

§Safety

The caller must ensure that the index is within bounds, otherwise it is undefined behavior.

Provided Methods§

source

fn remove(self, idx: usize) -> (T, Self::Output)

Removes an element at the given index, shifting elements after the given index to the left to fill the gap, resulting in a time complexity of O(n) where n=N-idx-1

§Example
let a = arr![1, 2, 3, 4];

let (removed, b) = a.remove(2);
assert_eq!(removed, 3);
assert_eq!(b, arr![1, 2, 4]);
§Panics

Panics if the index is out of bounds.

source

fn swap_remove(self, idx: usize) -> (T, Self::Output)

Removes an element at the given index, swapping it with the last element.

§Example
let a = arr![1, 2, 3, 4];

let (removed, b) = a.swap_remove(1);
assert_eq!(removed, 2);
assert_eq!(b, arr![1, 4, 3]); // note 4 is now at index 1
§Panics

Panics if the index is out of bounds.

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T, N> Remove<T, N> for GenericArray<T, N>
where N: ArrayLength + Sub<B1>, Sub1<N>: ArrayLength,

§

type Output = GenericArray<T, <N as Sub<B1>>::Output>