git.delta.rocks / jrsonnet / refs/commits / 3154bc9d5381

difftreelog

source

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