git.delta.rocks / jrsonnet / refs/commits / 5f620e2b9aa7

difftreelog

source

crates/jrsonnet-evaluator/src/dynamic.rs831 Bsourcehistory
1use std::cell::RefCell;23use jrsonnet_gcmodule::{Cc, Trace};45// TODO: Replace with OnceCell once in std6#[derive(Clone, Trace)]7pub struct Pending<V: Trace + 'static>(pub Cc<RefCell<Option<V>>>);8impl<T: Trace + 'static> Pending<T> {9	pub fn new() -> Self {10		Self(Cc::new(RefCell::new(None)))11	}12	pub fn new_filled(v: T) -> Self {13		Self(Cc::new(RefCell::new(Some(v))))14	}15	/// # Panics16	/// If wrapper is filled already17	pub fn fill(self, value: T) {18		assert!(self.0.borrow().is_none(), "wrapper is filled already");19		self.0.borrow_mut().replace(value);20	}21}22impl<T: Clone + Trace + 'static> Pending<T> {23	/// # Panics24	/// If wrapper is not yet filled25	pub fn unwrap(&self) -> T {26		self.0.borrow().as_ref().cloned().unwrap()27	}28}2930impl<T: Trace + 'static> Default for Pending<T> {31	fn default() -> Self {32		Self::new()33	}34}