bump_scope/
raw_fixed_bump_vec.rs

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
122
123
use core::{
    mem::transmute,
    ptr::{self, NonNull},
};

use crate::{
    error_behavior::ErrorBehavior,
    polyfill::{nonnull, transmute_mut, transmute_ref},
    raw_bump_box::RawBumpBox,
    BumpAllocator, FixedBumpVec, MutBumpAllocator, SizedTypeProperties,
};

/// Like [`FixedBumpVec`] but without its lifetime.
#[repr(C)]
pub struct RawFixedBumpVec<T> {
    pub(crate) initialized: RawBumpBox<[T]>,
    pub(crate) capacity: usize,
}

impl<T> RawFixedBumpVec<T> {
    #[inline(always)]
    pub(crate) const unsafe fn cook<'a>(self) -> FixedBumpVec<'a, T> {
        transmute(self)
    }

    #[inline(always)]
    pub(crate) const unsafe fn cook_ref<'a>(&self) -> &FixedBumpVec<'a, T> {
        transmute_ref(self)
    }

    #[inline(always)]
    pub(crate) unsafe fn cook_mut<'a>(&mut self) -> &mut FixedBumpVec<'a, T> {
        transmute_mut(self)
    }

    #[inline(always)]
    pub(crate) unsafe fn from_cooked(cooked: FixedBumpVec<'_, T>) -> Self {
        let (initialized, capacity) = cooked.into_raw_parts();
        let initialized = RawBumpBox::from_cooked(initialized);
        Self { initialized, capacity }
    }

    pub(crate) const EMPTY: Self = RawFixedBumpVec {
        initialized: RawBumpBox::EMPTY,
        capacity: if T::IS_ZST { usize::MAX } else { 0 },
    };

    #[inline(always)]
    pub(crate) unsafe fn allocate<B: ErrorBehavior>(allocator: &impl BumpAllocator, len: usize) -> Result<Self, B> {
        let ptr = B::allocate_slice::<T>(allocator, len)?;

        Ok(Self {
            initialized: RawBumpBox::from_ptr(nonnull::slice_from_raw_parts(ptr, 0)),
            capacity: len,
        })
    }

    #[inline(always)]
    pub(crate) unsafe fn prepare_allocation<B: ErrorBehavior>(
        allocator: &mut impl MutBumpAllocator,
        len: usize,
    ) -> Result<Self, B> {
        let allocation = B::prepare_slice_allocation::<T>(allocator, len)?;

        Ok(Self {
            initialized: RawBumpBox::from_ptr(nonnull::slice_from_raw_parts(nonnull::as_non_null_ptr(allocation), 0)),
            capacity: allocation.len(),
        })
    }

    /// `new_cap` must be greater than `self.capacity`
    pub(crate) unsafe fn grow_prepared_allocation<B: ErrorBehavior>(
        &mut self,
        allocator: &mut impl MutBumpAllocator,
        minimum_new_cap: usize,
    ) -> Result<(), B> {
        debug_assert!(minimum_new_cap > self.capacity);
        let allocation = B::prepare_slice_allocation::<T>(allocator, minimum_new_cap)?;

        let new_ptr = allocation.cast::<T>();
        let new_cap = allocation.len();

        ptr::copy_nonoverlapping(self.as_ptr(), new_ptr.as_ptr(), self.len());

        self.initialized.set_ptr(new_ptr);
        self.capacity = new_cap;

        Ok(())
    }

    #[inline(always)]
    pub(crate) fn as_ptr(&self) -> *const T {
        self.initialized.as_non_null_ptr().as_ptr().cast()
    }

    #[inline(always)]
    pub(crate) fn as_mut_ptr(&mut self) -> *mut T {
        self.initialized.as_non_null_ptr().as_ptr().cast()
    }

    #[must_use]
    #[inline(always)]
    #[allow(dead_code)]
    pub fn as_non_null_ptr(&self) -> NonNull<T> {
        self.initialized.as_non_null_ptr().cast()
    }

    #[inline(always)]
    pub(crate) const fn len(&self) -> usize {
        self.initialized.as_non_null_ptr().len()
    }

    #[allow(dead_code)]
    #[inline(always)]
    pub(crate) unsafe fn set_ptr(&mut self, new_ptr: NonNull<T>) {
        self.initialized.set_ptr(new_ptr);
    }

    #[inline(always)]
    pub(crate) unsafe fn set_len(&mut self, new_len: usize) {
        self.initialized.set_len(new_len);
    }
}