1use core::{
2 mem::forget,
3 ops::{Deref, DerefMut},
4};
5
6use crate::mem::{block::Block, object::Object};
7
8use super::{manager::Dealloc, ref_::Ref};
9
10#[repr(transparent)]
12#[derive(Debug)]
13pub struct MutRef<T: Object, D: Dealloc>(*mut Block<T, D>);
14
15impl<T: Object, D: Dealloc> MutRef<T, D> {
16 #[inline(always)]
17 pub const unsafe fn new(v: *mut Block<T, D>) -> Self {
18 Self(v)
19 }
20 #[inline(always)]
21 pub const fn to_ref(self) -> Ref<T, D> {
22 let result = unsafe { Ref::from_internal(self.0) };
23 forget(self);
24 result
25 }
26}
27
28impl<T: Object, D: Dealloc> Drop for MutRef<T, D> {
29 #[inline(always)]
30 fn drop(&mut self) {
31 unsafe { (*self.0).delete() }
32 }
33}
34
35impl<T: Object, D: Dealloc> Deref for MutRef<T, D> {
36 type Target = T;
37 #[inline(always)]
38 fn deref(&self) -> &Self::Target {
39 unsafe { (*self.0).object() }
40 }
41}
42
43impl<T: Object, D: Dealloc> DerefMut for MutRef<T, D> {
44 #[inline(always)]
45 fn deref_mut(&mut self) -> &mut Self::Target {
46 unsafe { (*self.0).object_mut() }
47 }
48}