Repository navigation
Expand file tree
/
Copy pathbuffer.rs
More file actions
121 lines (107 loc) · 4.51 KB
/
Copy pathbuffer.rs
File metadata and controls
121 lines (107 loc) · 4.51 KB
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
//! Implements the [`MaybeUninitSliceExt`] extension type.
use std::mem::MaybeUninit;
/// A buffer which has been split into initialized and uninitialized parts.
///
/// See [`MaybeUninitSlice`] and [`MaybeUninitSliceExt`].
pub type SplitInitBuffer<'a, T> = (&'a mut [T], &'a mut MaybeUninitSlice<T>);
/// A slice of data whose elements may or may not be initialized.
///
/// Newtype wrapper around [`[MaybeUninit<T>]`](MaybeUninit),
/// with extension methods available through [`MaybeUninitSliceExt`]
pub type MaybeUninitSlice<T> = [MaybeUninit<T>];
/// Extension methods for a [`MaybeUninitSlice`] (`[MaybeUninit<T>]`).
///
/// Contains functionality not yet available on stable rust.
pub trait MaybeUninitSliceExt<T>: sealed::Sealed<T> {
/// Convert a mutable reference to an uninitialized array,
/// into a mutable reference to an uninitialized slice.
///
/// If you have an `[MaybeUninit<T>; N]` instead,
/// you can use a deref conversion.
fn from_uninit_array_mut<const N: usize>(array: &mut MaybeUninit<[T; N]>) -> &mut Self;
/// Convert a reference to an uninitialized array,
/// into a reference to an uninitialized slice.
///
/// If you have an `[MaybeUninit<T>; N]` instead,
/// you can use a deref conversion.
fn from_uninit_array_ref<const N: usize>(array: &MaybeUninit<[T; N]>) -> &Self;
/// Convert a slice of initialized data into a slice of uninitialized data.
///
/// This is safe, because it is removing guarantees, not adding them.
/// There is no chance of double-initialization or leaks,
/// because the slice is a shared reference (not mutable)
fn from_init_slice(slice: &[T]) -> &Self;
/// Convert a mutable slice of initialized data into a slice of uninitialized data.
///
/// This is safe, because it is removing guarantees, not adding them.
/// Because of the requirement `T: Copy`, there is no chance of double-initialization or leaks.
fn from_init_slice_mut(slice: &mut [T]) -> &mut Self
where
T: Copy;
/// Assume the data in the slice is fully initialized.
///
/// Stable alternative to [`slice::assume_init_ref`].
///
/// ## Safety
/// Calling this when the content is not yet fully initialized causes undefined behavior.
#[inline]
unsafe fn assume_init_ref(&self) -> &[T] {
// SAFETY: The
unsafe { std::slice::from_raw_parts(self.as_slice().as_ptr().cast(), self.as_slice().len()) }
}
/// Assume the data in the slice is fully initialized.
///
/// Stable alternative to [`slice::assume_init_mut`].
///
/// ## Safety
/// Calling this when the content is not yet fully initialized causes undefined behavior.
#[inline]
unsafe fn assume_init_mut(&mut self) -> &mut [T] {
let len = self.as_slice().len();
// SAFETY: CAller guarantees data is initialized
unsafe { std::slice::from_raw_parts_mut(self.as_mut_slice().as_mut_ptr().cast(), len) }
}
}
impl<T> MaybeUninitSliceExt<T> for MaybeUninitSlice<T> {
#[inline]
fn from_uninit_array_mut<const N: usize>(array: &mut MaybeUninit<[T; N]>) -> &mut Self {
// SAFETY: Array is valid ref, length is constant
unsafe { std::slice::from_raw_parts_mut(array.as_mut_ptr().cast::<MaybeUninit<T>>(), N) }
}
#[inline]
fn from_uninit_array_ref<const N: usize>(array: &MaybeUninit<[T; N]>) -> &Self {
// SAFETY: Array is valid ref, length is constant
unsafe { std::slice::from_raw_parts(array.as_ptr().cast::<MaybeUninit<T>>(), N) }
}
#[inline]
fn from_init_slice(slice: &[T]) -> &Self {
// SAFETY: Just forgetting initialization, not adding it
unsafe { std::slice::from_raw_parts(slice.as_ptr().cast::<MaybeUninit<T>>(), slice.len()) }
}
#[inline]
fn from_init_slice_mut(slice: &mut [T]) -> &mut MaybeUninitSlice<T>
where
T: Copy,
{
// SAFETY: Just forgetting initialization, not adding it, T: Copy means no leaks
unsafe { std::slice::from_raw_parts_mut(slice.as_mut_ptr().cast::<MaybeUninit<T>>(), slice.len()) }
}
}
mod sealed {
use super::MaybeUninitSlice;
use std::mem::MaybeUninit;
pub trait Sealed<T> {
fn as_slice(&self) -> &[MaybeUninit<T>];
fn as_mut_slice(&mut self) -> &mut [MaybeUninit<T>];
}
impl<T> Sealed<T> for MaybeUninitSlice<T> {
#[inline]
fn as_slice(&self) -> &[MaybeUninit<T>] {
self
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [MaybeUninit<T>] {
self
}
}
}