Skip to main content

bump_scope/
bump_claim_guard.rs

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