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
use {
    crate::{polyfill::layout_for_metadata, Storage},
    core::{
        alloc::{AllocError, Layout},
        mem::MaybeUninit,
        ptr::{self, Pointee},
    },
};

/// A raw vec around some slice storage. Bundles the storage and its handle.
///
/// Note that this is *even lower level* than [alloc's `RawVec`] currently. That
/// raw vec handles amortized growth; this raw vec just does exactly as asked.
///
/// [alloc's `RawVec`]: https://github.com/rust-lang/rust/blob/master/library/alloc/src/raw_vec.rs
pub struct RawVec<T, S: Storage> {
    handle: S::Handle,
    metadata: <[T] as Pointee>::Metadata,
    storage: S,
}

impl<T, S: Storage> RawVec<T, S> {
    fn heap_layout(&self) -> Layout {
        Self::heap_layout_for(self.len())
    }

    fn heap_layout_for(len: usize) -> Layout {
        unsafe { layout_for_metadata::<[T]>(len).unwrap_unchecked() }
    }

    /// Create a new empty growable slice in the given storage.
    pub fn new(mut storage: S) -> Result<Self, S> {
        if let Ok(handle) = storage.allocate(Self::heap_layout_for(0)) {
            Ok(Self {
                handle,
                metadata: 0,
                storage,
            })
        } else {
            Err(storage)
        }
    }

    /// Get a reference to the boxed slice.
    pub fn as_ref(&self) -> &[MaybeUninit<T>] {
        unsafe {
            let (addr, _) = self
                .storage
                .resolve(self.handle, self.heap_layout())
                .as_ptr()
                .to_raw_parts();
            &*ptr::from_raw_parts(addr, self.len())
        }
    }

    /// Get a mutable reference to the boxed slice.
    pub fn as_mut(&mut self) -> &mut [MaybeUninit<T>] {
        unsafe {
            let (addr, _) = self
                .storage
                .resolve_mut(self.handle, self.heap_layout())
                .as_mut_ptr()
                .to_raw_parts();
            &mut *ptr::from_raw_parts_mut(addr, self.len())
        }
    }

    /// Get the length of the slice.
    pub fn len(&self) -> usize {
        self.metadata
    }

    /// Grow the length of the slice to `new_len`. Does not change the length
    /// if the slice is already long enough. Does not do amortization.
    pub fn grow_to(&mut self, new_len: usize) -> Result<(), AllocError> {
        if new_len <= self.len() {
            Ok(())
        } else {
            self.handle = unsafe {
                self.storage.grow(
                    self.handle,
                    self.heap_layout(),
                    Self::heap_layout_for(new_len),
                )
            }?;
            self.metadata = new_len;
            Ok(())
        }
    }

    /// Shrink the length of the slice to `new_len`. Does not change the length
    /// if the slice is already shorter than the given length.
    pub fn shrink_to(&mut self, new_len: usize) -> Result<(), AllocError> {
        if new_len >= self.len() {
            Ok(())
        } else {
            self.handle = unsafe {
                self.storage.shrink(
                    self.handle,
                    self.heap_layout(),
                    Self::heap_layout_for(new_len),
                )
            }?;
            Ok(())
        }
    }
}

unsafe impl<#[may_dangle] T, S: Storage> Drop for RawVec<T, S> {
    fn drop(&mut self) {
        unsafe { self.storage.deallocate(self.handle, self.heap_layout()) }
    }
}