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
pub(crate) mod linear_storage_len;
pub(crate) mod linear_storage_mut;
pub(crate) mod linear_storage_slice;
use core::mem::needs_drop;
/// A storage that can be represented through a contiguous segment of memory.
pub(crate) trait LinearStorage<T> {
/// See [`needs_drop`].
const NEEDS_DROP: bool = needs_drop::<T>();
/// See [`linear_storage_len::LinearStorageLen`].
type Len: linear_storage_len::LinearStorageLen;
/// See [`linear_storage_slice::LinearStorageSlice`].
type Slice: linear_storage_slice::LinearStorageSlice<Data = T> + ?Sized;
// ***** REQUIRED *****
/// Returns a raw pointer to the vector’s buffer, or a dangling raw pointer valid for zero sized
/// reads if the storage didn’t allocate.
fn as_ptr(&self) -> *const T;
/// Returns the total number of elements the vector can hold without reallocating.
fn capacity(&self) -> Self::Len;
/// Returns the number of elements in the vector, also referred to as its `length`.
fn len(&self) -> Self::Len;
// ***** PROVIDED *****
/// Extracts a slice containing the entire vector.
#[inline]
fn as_slice(&self) -> &Self::Slice {
use linear_storage_len::LinearStorageLen as _;
use linear_storage_slice::LinearStorageSlice as _;
// SAFETY: it is assumed that implementations ensured `self.len()` initialized elements
unsafe { Self::Slice::from_raw_parts(self.as_ptr(), self.len().usize()) }
}
/// Returns the capacity left in the storage.
#[inline]
fn remaining(&self) -> Self::Len {
use linear_storage_len::LinearStorageLen as _;
self.capacity().wrapping_sub(self.len())
}
}
impl<T> LinearStorage<T> for alloc::vec::Vec<T> {
type Len = usize;
type Slice = [T];
#[inline]
fn as_ptr(&self) -> *const T {
(*self).as_ptr()
}
#[inline]
fn capacity(&self) -> Self::Len {
(*self).capacity()
}
#[inline]
fn len(&self) -> Self::Len {
(*self).len()
}
}