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
#[macro_export] macro_rules! future_wrapper { ($orig: ty, $wrapper: ident) => { #[derive(Debug, Clone)] pub struct $wrapper(pub std::rc::Rc<std::cell::RefCell<Option<$orig>>>); impl $wrapper { pub fn unwrap(self) -> $orig { self.0.borrow().as_ref().map(|e| e.clone()).unwrap() } pub fn new() -> Self { $wrapper(std::rc::Rc::new(std::cell::RefCell::new(None))) } pub fn fill(self, val: $orig) -> $orig { if self.0.borrow().is_some() { panic!("wrapper is filled already"); } { self.0.borrow_mut().replace(val); } self.unwrap() } } impl Default for $wrapper { fn default() -> Self { Self::new() } } }; } #[macro_export] macro_rules! rc_fn_helper { ($name: ident, $macro_name: ident, $fn: ty) => { #[derive(Clone)] #[doc = "Function wrapper"] pub struct $name(pub std::rc::Rc<$fn>); impl std::fmt::Debug for $name { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct(std::stringify!($name)).finish() } } impl std::cmp::PartialEq for $name { fn eq(&self, other: &$name) -> bool { std::ptr::eq(&self.0, &other.0) } } #[doc = "Macro to ease wrapper creation"] #[macro_export] macro_rules! $macro_name { ($val: expr) => { $crate::$name(std::rc::Rc::new($val)) }; } }; }