Lines Matching defs:Lazy
62 //! ## Lazy Initialized Global Data
82 //! There are also the [`sync::Lazy`] and [`unsync::Lazy`] convenience types to streamline this pattern:
86 //! use once_cell::sync::Lazy;
88 //! static GLOBAL_DATA: Lazy<Mutex<HashMap<i32, String>>> = Lazy::new(|| {
100 //! Note that the variable that holds `Lazy` is declared as `static`, *not*
103 //! [`sync::Lazy`]: sync/struct.Lazy.html
104 //! [`unsync::Lazy`]: unsync/struct.Lazy.html
108 //! Unlike `lazy_static!`, `Lazy` works with local variables.
111 //! use once_cell::unsync::Lazy;
115 //! let thunk = Lazy::new(|| {
668 /// use once_cell::unsync::Lazy;
670 /// let lazy: Lazy<i32> = Lazy::new(|| {
684 pub struct Lazy<T, F = fn() -> T> {
689 impl<T, F: RefUnwindSafe> RefUnwindSafe for Lazy<T, F> where OnceCell<T>: RefUnwindSafe {}
691 impl<T: fmt::Debug, F> fmt::Debug for Lazy<T, F> {
693 f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
697 impl<T, F> Lazy<T, F> {
703 /// use once_cell::unsync::Lazy;
707 /// let lazy = Lazy::new(|| hello.to_uppercase());
712 pub const fn new(init: F) -> Lazy<T, F> {
713 Lazy { cell: OnceCell::new(), init: Cell::new(Some(init)) }
716 /// Consumes this `Lazy` returning the stored value.
718 /// Returns `Ok(value)` if `Lazy` is initialized and `Err(f)` otherwise.
719 pub fn into_value(this: Lazy<T, F>) -> Result<T, F> {
723 init.take().unwrap_or_else(|| panic!("Lazy instance has previously been poisoned"))
728 impl<T, F: FnOnce() -> T> Lazy<T, F> {
736 /// use once_cell::unsync::Lazy;
738 /// let lazy = Lazy::new(|| 92);
740 /// assert_eq!(Lazy::force(&lazy), &92);
743 pub fn force(this: &Lazy<T, F>) -> &T {
746 None => panic!("Lazy instance has previously been poisoned"),
757 /// use once_cell::unsync::Lazy;
759 /// let mut lazy = Lazy::new(|| 92);
761 /// assert_eq!(Lazy::force_mut(&mut lazy), &92);
764 pub fn force_mut(this: &mut Lazy<T, F>) -> &mut T {
774 /// use once_cell::unsync::Lazy;
776 /// let lazy = Lazy::new(|| 92);
778 /// assert_eq!(Lazy::get(&lazy), None);
780 /// assert_eq!(Lazy::get(&lazy), Some(&92));
782 pub fn get(this: &Lazy<T, F>) -> Option<&T> {
791 /// use once_cell::unsync::Lazy;
793 /// let mut lazy = Lazy::new(|| 92);
795 /// assert_eq!(Lazy::get_mut(&mut lazy), None);
797 /// assert_eq!(Lazy::get_mut(&mut lazy), Some(&mut 92));
799 pub fn get_mut(this: &mut Lazy<T, F>) -> Option<&mut T> {
804 impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> {
807 Lazy::force(self)
811 impl<T, F: FnOnce() -> T> DerefMut for Lazy<T, F> {
813 Lazy::force(self);
818 impl<T: Default> Default for Lazy<T> {
820 fn default() -> Lazy<T> {
821 Lazy::new(T::default)
1196 /// use once_cell::sync::Lazy;
1198 /// static HASHMAP: Lazy<HashMap<i32, String>> = Lazy::new(|| {
1220 pub struct Lazy<T, F = fn() -> T> {
1225 impl<T: fmt::Debug, F> fmt::Debug for Lazy<T, F> {
1227 f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
1231 // We never create a `&F` from a `&Lazy<T, F>` so it is fine to not impl
1235 unsafe impl<T, F: Send> Sync for Lazy<T, F> where OnceCell<T>: Sync {}
1238 impl<T, F: RefUnwindSafe> RefUnwindSafe for Lazy<T, F> where OnceCell<T>: RefUnwindSafe {}
1240 impl<T, F> Lazy<T, F> {
1243 pub const fn new(f: F) -> Lazy<T, F> {
1244 Lazy { cell: OnceCell::new(), init: Cell::new(Some(f)) }
1247 /// Consumes this `Lazy` returning the stored value.
1249 /// Returns `Ok(value)` if `Lazy` is initialized and `Err(f)` otherwise.
1250 pub fn into_value(this: Lazy<T, F>) -> Result<T, F> {
1254 init.take().unwrap_or_else(|| panic!("Lazy instance has previously been poisoned"))
1259 impl<T, F: FnOnce() -> T> Lazy<T, F> {
1266 /// use once_cell::sync::Lazy;
1268 /// let lazy = Lazy::new(|| 92);
1270 /// assert_eq!(Lazy::force(&lazy), &92);
1273 pub fn force(this: &Lazy<T, F>) -> &T {
1276 None => panic!("Lazy instance has previously been poisoned"),
1286 /// use once_cell::sync::Lazy;
1288 /// let mut lazy = Lazy::new(|| 92);
1290 /// assert_eq!(Lazy::force_mut(&mut lazy), &mut 92);
1292 pub fn force_mut(this: &mut Lazy<T, F>) -> &mut T {
1302 /// use once_cell::sync::Lazy;
1304 /// let lazy = Lazy::new(|| 92);
1306 /// assert_eq!(Lazy::get(&lazy), None);
1308 /// assert_eq!(Lazy::get(&lazy), Some(&92));
1310 pub fn get(this: &Lazy<T, F>) -> Option<&T> {
1319 /// use once_cell::sync::Lazy;
1321 /// let mut lazy = Lazy::new(|| 92);
1323 /// assert_eq!(Lazy::get_mut(&mut lazy), None);
1325 /// assert_eq!(Lazy::get_mut(&mut lazy), Some(&mut 92));
1327 pub fn get_mut(this: &mut Lazy<T, F>) -> Option<&mut T> {
1332 impl<T, F: FnOnce() -> T> Deref for Lazy<T, F> {
1335 Lazy::force(self)
1339 impl<T, F: FnOnce() -> T> DerefMut for Lazy<T, F> {
1341 Lazy::force(self);
1346 impl<T: Default> Default for Lazy<T> {
1348 fn default() -> Lazy<T> {
1349 Lazy::new(T::default)
1366 /// share(&once_cell::sync::Lazy::<S>::new(|| unimplemented!()));