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
use crate::raw::{
capacity::{capacity, fixed_capacity_reserve_error, Round},
Storage,
};
use core::mem::{align_of, size_of, MaybeUninit};
#[repr(transparent)]
pub struct UninitSlice<T>([MaybeUninit<T>]);
unsafe impl<T> Send for UninitSlice<T> {}
unsafe impl<T> Sync for UninitSlice<T> {}
#[cfg(not(feature = "nightly"))]
impl<T> UninitSlice<T> {
pub fn from_mut(buffer: &mut [MaybeUninit<T>]) -> &mut Self { unsafe { &mut *(buffer as *mut [_] as *mut Self) } }
pub unsafe fn to_mut(&mut self) -> &mut [MaybeUninit<T>] { unsafe { &mut *(self as *mut Self as *mut [_]) } }
}
#[cfg(feature = "nightly")]
impl<T> UninitSlice<T> {
pub const fn from_mut(buffer: &mut [MaybeUninit<T>]) -> &mut Self {
unsafe { &mut *(buffer as *mut [_] as *mut Self) }
}
pub const unsafe fn to_mut(&mut self) -> &mut [MaybeUninit<T>] { unsafe { &mut *(self as *mut Self as *mut [_]) } }
}
unsafe impl<T, U> Storage<U> for UninitSlice<T> {
const IS_ALIGNED: bool = align_of::<T>() >= align_of::<U>();
fn capacity(&self) -> usize { capacity(self.0.len(), size_of::<T>(), size_of::<U>(), Round::Down) }
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 = capacity(new_capacity, size_of::<U>(), size_of::<T>(), Round::Up);
if new_capacity > self.0.len() {
fixed_capacity_reserve_error(self.0.len(), new_capacity)
}
}
fn try_reserve(&mut self, capacity: usize) -> bool { capacity <= Storage::<U>::capacity(self) }
}
unsafe impl<T: Copy> crate::raw::StorageInit<T> for [T] {}
unsafe impl<T: Copy> Storage<T> for [T] {
const IS_ALIGNED: bool = true;
fn capacity(&self) -> usize { self.len() }
fn as_ptr(&self) -> *const T { self.as_ptr().cast() }
fn as_mut_ptr(&mut self) -> *mut T { self.as_mut_ptr().cast() }
fn reserve(&mut self, capacity: usize) {
if capacity > self.len() {
fixed_capacity_reserve_error(self.len(), capacity)
}
}
fn try_reserve(&mut self, capacity: usize) -> bool { capacity <= self.capacity() }
}