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
use super::IsBarWrapper;
use crate::isbar::IsBar;
use core::cell::RefCell;
use std::cell::BorrowMutError;
#[cfg(feature = "nightly")]
use std::cell::RefMut;
#[cfg(not(feature = "nightly"))]
use std::ops::DerefMut;
use std::rc::Rc;

/// a wrapper around a [`Bar`], allowing the manager to keep a copy while
/// passing one to the user
///
/// When this is dropped, [`done`] *should* be called,
/// however it does not check if it errors or not to avoid panicking,
/// so it may not have sucseeded. if you want to check this, call [`done`] manually
///
/// [`Bar`]: IsBar
/// [`done`]: IsBar::done
#[derive(Clone, Debug)]
pub struct BarWrapper<B: IsBar>(Rc<RefCell<B>>);

#[cfg(not(feature = "nightly"))]
impl<B: IsBar> IsBarWrapper for BarWrapper<B> {
    type Bar = B;
    type Error = BorrowMutError;
    fn try_bar<'b>(
        &'b mut self,
    ) -> Result<Box<dyn DerefMut<Target = Self::Bar> + 'b>, BorrowMutError> {
        Ok(Box::new(self.0.try_borrow_mut()?))
    }
}

#[cfg(feature = "nightly")]
impl<B: IsBar> IsBarWrapper for BarWrapper<B> {
    type Bar = B;
    type Error = BorrowMutError;
    type BarGuard<'g>
    where
        Self: 'g,
    = RefMut<'g, Self::Bar>;
    fn try_bar<'g>(&'g mut self) -> Result<Self::BarGuard<'g>, BorrowMutError> {
        self.0.try_borrow_mut()
    }
}

impl<B: IsBar> From<Rc<RefCell<B>>> for BarWrapper<B> {
    fn from(item: Rc<RefCell<B>>) -> Self {
        Self(item)
    }
}

impl<B: IsBar> Drop for BarWrapper<B> {
    fn drop(&mut self) {
        if let Ok(mut b) = self.0.try_borrow_mut() {
            b.done();
        }
    }
}

impl<B: IsBar> crate::sealant::Sealed for BarWrapper<B> {}