generic_array::sequence

Trait 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.

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.

Implementors§

Source§

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