Skip to main content

bump_scope/
bump_claim_guard.rs

1use core::{
2    marker::PhantomData,
3    ops::{Deref, DerefMut},
4};
5
6use crate::{
7    BaseAllocator, BumpScope,
8    settings::{BumpAllocatorSettings, BumpSettings},
9};
10
11// For docs.
12#[allow(unused_imports)]
13use crate::traits::*;
14
15/// Returned from [`BumpAllocatorScope::claim`].
16#[must_use]
17pub struct BumpClaimGuard<'b, 'a, A, S = BumpSettings>
18where
19    A: BaseAllocator<S::GuaranteedAllocated>,
20    S: BumpAllocatorSettings,
21{
22    pub(crate) original: &'b BumpScope<'a, A, S>,
23    pub(crate) claimant: BumpScope<'a, A, S>,
24}
25
26impl<'b, 'a, A, S> BumpClaimGuard<'b, 'a, A, S>
27where
28    A: BaseAllocator<S::GuaranteedAllocated>,
29    S: BumpAllocatorSettings,
30{
31    #[inline(always)]
32    pub(crate) fn new(original: &'b BumpScope<'a, A, S>) -> Self {
33        let claimed = original.raw.claim();
34
35        let claimant = BumpScope {
36            raw: claimed,
37            marker: PhantomData,
38        };
39
40        Self { original, claimant }
41    }
42}
43
44impl<'a, A, S> Deref for BumpClaimGuard<'_, 'a, A, S>
45where
46    A: BaseAllocator<S::GuaranteedAllocated>,
47    S: BumpAllocatorSettings,
48{
49    type Target = BumpScope<'a, A, S>;
50
51    #[inline(always)]
52    fn deref(&self) -> &Self::Target {
53        &self.claimant
54    }
55}
56
57impl<A, S> DerefMut for BumpClaimGuard<'_, '_, A, S>
58where
59    A: BaseAllocator<S::GuaranteedAllocated>,
60    S: BumpAllocatorSettings,
61{
62    #[inline(always)]
63    fn deref_mut(&mut self) -> &mut Self::Target {
64        &mut self.claimant
65    }
66}
67
68impl<A, S> Drop for BumpClaimGuard<'_, '_, A, S>
69where
70    A: BaseAllocator<S::GuaranteedAllocated>,
71    S: BumpAllocatorSettings,
72{
73    #[inline(always)]
74    fn drop(&mut self) {
75        self.original.raw.reclaim(&self.claimant.raw);
76    }
77}