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
use crate::raw::{AllocError, Init, Storage, Uninit};

use core::mem::{size_of, MaybeUninit};

/// An uninitialized slice storage
pub type UninitSlice<'a, T> = Uninit<&'a mut [MaybeUninit<T>]>;
/// An initialized slice storage, can only store `Copy` types
pub type Slice<'a, T> = Init<&'a mut [T]>;

unsafe impl<T> Send for UninitSlice<'_, T> {}
unsafe impl<T> Sync for UninitSlice<'_, T> {}

unsafe impl<T, U> Storage<U> for UninitSlice<'_, T> {
    fn is_valid_storage() -> bool { true }

    fn capacity(&self) -> usize { crate::raw::capacity(self.0.len(), size_of::<T>(), size_of::<U>()) }

    fn as_ptr(&self) -> *const U { self.0.as_ptr().cast() }

    fn as_mut_ptr(&mut self) -> *mut U { self.0.as_mut_ptr().cast() }

    fn reserve(&mut self, new_capacity: usize) {
        let new_capacity = crate::raw::capacity(new_capacity, size_of::<U>(), size_of::<T>());
        if new_capacity > self.0.len() {
            crate::raw::fixed_capacity_reserve_error(self.0.len(), new_capacity)
        }
    }

    fn try_reserve(&mut self, capacity: usize) -> Result<(), AllocError> {
        if capacity <= Storage::<U>::capacity(self) {
            Ok(())
        } else {
            Err(AllocError)
        }
    }
}

unsafe impl<T: Copy> crate::raw::StorageInit<T> for Slice<'_, T> {}
unsafe impl<T: Copy> Storage<T> for Slice<'_, T> {
    fn is_valid_storage() -> bool { true }

    fn capacity(&self) -> usize { self.0.len() }

    fn as_ptr(&self) -> *const T { self.0.as_ptr().cast() }

    fn as_mut_ptr(&mut self) -> *mut T { self.0.as_mut_ptr().cast() }

    fn reserve(&mut self, capacity: usize) {
        if capacity > self.0.len() {
            crate::raw::fixed_capacity_reserve_error(self.0.len(), capacity)
        }
    }

    fn try_reserve(&mut self, capacity: usize) -> Result<(), AllocError> {
        if capacity <= self.capacity() {
            Ok(())
        } else {
            Err(AllocError)
        }
    }
}