bump_scope/
raw_fixed_bump_string.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
use core::{mem::transmute, ptr::NonNull};

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

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

impl RawFixedBumpString {
    #[inline(always)]
    pub(crate) const unsafe fn cook<'a>(self) -> FixedBumpString<'a> {
        transmute(self)
    }

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

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

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

    pub(crate) const EMPTY: Self = RawFixedBumpString {
        initialized: RawBumpBox::EMPTY_STR,
        capacity: 0,
    };

    #[inline(always)]
    pub(crate) unsafe fn allocate<B: ErrorBehavior>(allocator: &impl BumpAllocator, len: usize) -> Result<Self, B> {
        let ptr = B::allocate_slice::<u8>(allocator, len)?;
        let initialized = RawBumpBox::from_ptr(nonnull::str_from_utf8(nonnull::slice_from_raw_parts(ptr, 0)));
        Ok(Self {
            initialized,
            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::<u8>(allocator, len)?;
        let initialized = RawBumpBox::from_ptr(nonnull::str_from_utf8(nonnull::slice_from_raw_parts(
            nonnull::as_non_null_ptr(allocation),
            0,
        )));

        Ok(Self {
            initialized,
            capacity: allocation.len(),
        })
    }

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

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

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

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

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