nanvm_lib/mem/
optional_ref.rs

1use core::{
2    hash::{Hash, Hasher},
3    mem::forget,
4};
5
6use crate::common::ref_mut::RefMut;
7
8use super::{optional_block::OptionalBlock, ref_counter_update::RefCounterUpdate};
9
10#[derive(Debug)]
11#[repr(transparent)]
12pub struct OptionalRef<T: OptionalBlock> {
13    value: T,
14}
15
16impl<T: OptionalBlock> PartialEq for OptionalRef<T> {
17    #[inline(always)]
18    fn eq(&self, other: &Self) -> bool {
19        self.value == other.value
20    }
21}
22
23impl<T: OptionalBlock> Eq for OptionalRef<T> {}
24
25impl<T: OptionalBlock> Hash for OptionalRef<T> {
26    #[inline(always)]
27    fn hash<H: Hasher>(&self, state: &mut H) {
28        self.value.hash(state)
29    }
30}
31
32impl<T: OptionalBlock> OptionalRef<T> {
33    #[inline(always)]
34    pub const unsafe fn from_internal(value: T) -> Self {
35        Self { value }
36    }
37    #[inline(always)]
38    pub const unsafe fn internal(&self) -> T {
39        self.value
40    }
41    #[inline(always)]
42    pub fn is_ref(&self) -> bool {
43        self.value.is_ref()
44    }
45    #[inline(always)]
46    pub unsafe fn move_to_internal(mut self) -> T {
47        let result = self.value.to_mut_ptr().read();
48        forget(self);
49        result
50    }
51}
52
53impl<T: OptionalBlock> Clone for OptionalRef<T> {
54    #[inline(always)]
55    fn clone(&self) -> Self {
56        unsafe { self.value.ref_counter_update(RefCounterUpdate::AddRef) };
57        Self { value: self.value }
58    }
59}
60
61impl<T: OptionalBlock> Drop for OptionalRef<T> {
62    fn drop(&mut self) {
63        unsafe {
64            if let Some(header) = self.value.ref_counter_update(RefCounterUpdate::Release) {
65                self.value.delete(header);
66            }
67        }
68    }
69}