1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::{mem, num::NonZeroUsize};

pub trait SliceExt<T> {
    unsafe fn get_many_unchecked_mut<const N: usize>(&mut self, indices: [usize; N])
        -> [&mut T; N];

    fn get_many_mut_opt<const N: usize>(&mut self, indices: [usize; N]) -> Option<[&mut T; N]>;
    fn get_many_mut_res_simple<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Result<[&mut T; N], ErrorSimple<N>>;
    fn get_many_mut_res_direct<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Result<[&mut T; N], ErrorKind>;
    fn get_many_mut_res_indirect<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Result<[&mut T; N], Error<N>>;
    fn get_many_mut_res_indirect_niche<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Result<[&mut T; N], ErrorNiche<N>>;
}

impl<T> SliceExt<T> for [T] {
    #[inline]
    unsafe fn get_many_unchecked_mut<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> [&mut T; N] {
        // NB: This implementation is written as it is because any variation of
        // `indices.map(|i| self.get_unchecked_mut(i))` would make miri unhappy,
        // or generate worse code otherwise. This is also why we need to through
        // a raw pointer here.
        let slice: *mut [T] = self;
        let mut arr: mem::MaybeUninit<[&mut T; N]> = mem::MaybeUninit::uninit();
        let arr_ptr = arr.as_mut_ptr();

        // SAFETY: We expect `indices` to contain disjunct values that are
        // in bounds of `self`.
        unsafe {
            for i in 0..N {
                let idx = *indices.get_unchecked(i);
                *(*arr_ptr).get_unchecked_mut(i) = &mut *slice.get_unchecked_mut(idx);
            }
            arr.assume_init()
        }
    }

    #[inline]
    fn get_many_mut_opt<const N: usize>(&mut self, indices: [usize; N]) -> Option<[&mut T; N]> {
        if !get_many_check_valid(&indices, self.len()) {
            return None;
        }
        // SAFETY: The `get_many_check_valid()` call checked that all indices
        // are disjunct and in bounds.
        unsafe { Some(self.get_many_unchecked_mut(indices)) }
    }

    fn get_many_mut_res_simple<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Result<[&mut T; N], ErrorSimple<N>> {
        if !get_many_check_valid(&indices, self.len()) {
            return Err(ErrorSimple);
        }
        // SAFETY: The `get_many_check_valid()` call checked that all indices
        // are disjunct and in bounds.
        unsafe { Ok(self.get_many_unchecked_mut(indices)) }
    }

    fn get_many_mut_res_direct<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Result<[&mut T; N], ErrorKind> {
        get_many_check_valid_kinds(&indices, self.len())?;
        // SAFETY: The `get_many_check_valid()` call checked that all indices
        // are disjunct and in bounds.
        unsafe { Ok(self.get_many_unchecked_mut(indices)) }
    }

    fn get_many_mut_res_indirect<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Result<[&mut T; N], Error<N>> {
        if !get_many_check_valid(&indices, self.len()) {
            return Err(Error {
                indices,
                slice_len: self.len(),
            });
        }
        // SAFETY: The `get_many_check_valid()` call checked that all indices
        // are disjunct and in bounds.
        unsafe { Ok(self.get_many_unchecked_mut(indices)) }
    }

    fn get_many_mut_res_indirect_niche<const N: usize>(
        &mut self,
        indices: [usize; N],
    ) -> Result<[&mut T; N], ErrorNiche<N>> {
        if !get_many_check_valid(&indices, self.len()) {
            return Err(ErrorNiche {
                indices,
                slice_len: unsafe { SliceLenWithNiche(self.len().unchecked_add(2)) },
            });
        }
        // SAFETY: The `get_many_check_valid()` call checked that all indices
        // are disjunct and in bounds.
        unsafe { Ok(self.get_many_unchecked_mut(indices)) }
    }
}

/// This checks every index against each other, and against `len`.
///
/// This will do `binomial(N + 1, 2) = N * (N + 1) / 2 = 0, 1, 3, 6, 10, ..`
/// comparison operations.
fn get_many_check_valid<const N: usize>(indices: &[usize; N], len: usize) -> bool {
    // NB: The optimizer should inline the loops into a sequence
    // of instructions without additional branching.
    let mut valid = true;
    for (i, &idx) in indices.iter().enumerate() {
        valid &= idx < len;
        for &idx2 in &indices[..i] {
            valid &= idx != idx2;
        }
    }
    valid
}

// NB: The N here is there to be forward-compatible with adding more details
// to the error later
#[derive(Debug)]
pub struct ErrorSimple<const N: usize>;

pub struct Error<const N: usize> {
    indices: [usize; N],
    slice_len: usize,
}

#[rustc_layout_scalar_valid_range_start(2)]
#[rustc_nonnull_optimization_guaranteed]
struct SliceLenWithNiche(usize);

pub struct ErrorNiche<const N: usize> {
    indices: [usize; N],
    slice_len: SliceLenWithNiche,
}

pub enum ErrorKind {
    OutOfBounds,
    NotUnique,
}

/// This checks every index against each other, and against `len`.
///
/// This will do `binomial(N + 1, 2) = N * (N + 1) / 2 = 0, 1, 3, 6, 10, ..`
/// comparison operations.
fn get_many_check_valid_kinds<const N: usize>(
    indices: &[usize; N],
    len: usize,
) -> Result<(), ErrorKind> {
    // NB: The optimizer should inline the loops into a sequence
    // of instructions without additional branching.
    for (i, &idx) in indices.iter().enumerate() {
        if idx >= len {
            return Err(ErrorKind::OutOfBounds);
        }
        for &idx2 in &indices[..i] {
            if idx == idx2 {
                return Err(ErrorKind::NotUnique);
            }
        }
    }
    Ok(())
}

// TODO: write tests