git.delta.rocks / jrsonnet / refs/commits / ce2d8d503b4a

difftreelog

source

crates/jrsonnet-evaluator/src/dynamic.rs1.3 KiBsourcehistory
1#[macro_export]2macro_rules! future_wrapper {3	($orig: ty, $wrapper: ident) => {4		#[derive(Debug, Clone)]5		pub struct $wrapper(pub std::rc::Rc<std::cell::RefCell<Option<$orig>>>);6		impl $wrapper {7			pub fn unwrap(self) -> $orig {8				self.0.borrow().as_ref().map(|e| e.clone()).unwrap()9			}10			pub fn new() -> Self {11				$wrapper(std::rc::Rc::new(std::cell::RefCell::new(None)))12			}13			pub fn fill(self, val: $orig) -> $orig {14				if self.0.borrow().is_some() {15					panic!("wrapper is filled already");16				}17				{18					self.0.borrow_mut().replace(val);19				}20				self.unwrap()21			}22		}23		impl Default for $wrapper {24			fn default() -> Self {25				Self::new()26			}27		}28	};29}3031#[macro_export]32macro_rules! rc_fn_helper {33	($name: ident, $macro_name: ident, $fn: ty) => {34		#[derive(Clone)]35		#[doc = "Function wrapper"]36		pub struct $name(pub std::rc::Rc<$fn>);37		impl std::fmt::Debug for $name {38			fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {39				f.debug_struct(std::stringify!($name)).finish()40			}41		}42		impl std::cmp::PartialEq for $name {43			fn eq(&self, other: &$name) -> bool {44				std::ptr::eq(&self.0, &other.0)45			}46		}47		#[doc = "Macro to ease wrapper creation"]48		#[macro_export]49		macro_rules! $macro_name {50			($val: expr) => {51				$crate::$name(std::rc::Rc::new($val))52			};53		}54	};55}