bump_scope/
raw_bump_box.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
124
125
use core::{
    marker::PhantomData,
    mem::{self, transmute},
    ptr::{self, NonNull},
};

use crate::{
    polyfill::{nonnull, transmute_mut, transmute_ref},
    BumpBox,
};

/// Like [`BumpBox`] but without its lifetime.
#[repr(transparent)]
pub struct RawBumpBox<T: ?Sized> {
    pub(crate) ptr: NonNull<T>,

    /// Marks ownership over T. (<https://doc.rust-lang.org/nomicon/phantom-data.html#generic-parameters-and-drop-checking>)
    marker: PhantomData<T>,
}

unsafe impl<T: ?Sized + Send> Send for RawBumpBox<T> {}
unsafe impl<T: ?Sized + Sync> Sync for RawBumpBox<T> {}

impl<T: ?Sized> Drop for RawBumpBox<T> {
    #[inline(always)]
    fn drop(&mut self) {
        unsafe { self.ptr.as_ptr().drop_in_place() }
    }
}

impl<T: ?Sized> RawBumpBox<T> {
    #[allow(dead_code)]
    #[inline(always)]
    pub(crate) unsafe fn cook<'a>(self) -> BumpBox<'a, T> {
        transmute(self)
    }

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

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

    #[inline(always)]
    pub(crate) unsafe fn from_cooked(cooked: BumpBox<'_, T>) -> Self {
        Self {
            ptr: cooked.into_raw(),
            marker: PhantomData,
        }
    }

    #[inline(always)]
    pub(crate) const unsafe fn from_ptr(ptr: NonNull<T>) -> Self {
        Self {
            ptr,
            marker: PhantomData,
        }
    }

    #[inline(always)]
    pub(crate) fn into_ptr(self) -> NonNull<T> {
        let ptr = unsafe { ptr::read(&self.ptr) };
        mem::forget(self);
        ptr
    }

    #[must_use]
    #[inline(always)]
    pub const fn as_non_null_ptr(&self) -> NonNull<T> {
        self.ptr
    }
}

impl<T> RawBumpBox<[T]> {
    pub(crate) const EMPTY: Self = Self {
        ptr: nonnull::slice_from_raw_parts(NonNull::dangling(), 0),
        marker: PhantomData,
    };

    #[allow(dead_code)]
    #[inline(always)]
    pub(crate) fn len(&self) -> usize {
        self.ptr.len()
    }

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

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

impl RawBumpBox<str> {
    pub(crate) const EMPTY_STR: Self = Self {
        ptr: nonnull::str_from_utf8(nonnull::slice_from_raw_parts(NonNull::dangling(), 0)),
        marker: PhantomData,
    };

    #[inline(always)]
    pub(crate) fn len(&self) -> usize {
        nonnull::str_bytes(self.ptr).len()
    }

    #[inline(always)]
    pub(crate) unsafe fn set_ptr(&mut self, new_ptr: NonNull<u8>) {
        let len = self.len();
        self.ptr = nonnull::str_from_utf8(nonnull::slice_from_raw_parts(new_ptr, len));
    }

    #[inline(always)]
    pub(crate) unsafe fn set_len(&mut self, new_len: usize) {
        let ptr = self.ptr.cast::<u8>();
        self.ptr = nonnull::str_from_utf8(nonnull::slice_from_raw_parts(ptr, new_len));
    }
}