Lines Matching defs:OnceCell

3 //! `once_cell` provides two new cell-like types, [`unsync::OnceCell`] and [`sync::OnceCell`]. A `OnceCell`
8 //! impl<T> OnceCell<T> {
9 //! const fn new() -> OnceCell<T> { ... }
21 //! [`unsync::OnceCell`]: unsync/struct.OnceCell.html
22 //! [`sync::OnceCell`]: sync/struct.OnceCell.html
29 //! `OnceCell` might be useful for a variety of patterns.
36 //! use once_cell::sync::OnceCell;
42 //! static INSTANCE: OnceCell<Logger> = OnceCell::new();
69 //! use once_cell::sync::OnceCell;
72 //! static INSTANCE: OnceCell<Mutex<HashMap<i32, String>>> = OnceCell::new();
122 //! If you need a lazy field in a struct, you probably should use `OnceCell`
128 //! use once_cell::unsync::OnceCell;
132 //! config: OnceCell<String>,
153 //! static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
170 //! use once_cell::sync::OnceCell;
174 //! cell: OnceCell<Vec<u8>>,
179 //! TestResource { path, cell: OnceCell::new() }
209 //! use once_cell::sync::OnceCell;
211 //! pub struct LateInit<T> { cell: OnceCell<T> }
220 //! fn default() -> Self { LateInit { cell: OnceCell::default() } }
257 //! |`unsync::OnceCell<T>` | `&T` | assignable only once |
263 //! |`sync::OnceCell<T>` | `&T` | assignable only once, may block the thread |
283 //! To implement a sync flavor of `OnceCell`, this crates uses either a custom
286 //! is the same for both cases, but the `parking_lot` based `OnceCell<T>` is
353 /// Single-threaded version of `OnceCell`.
366 /// Unlike [`std::cell::RefCell`], a `OnceCell` provides simple `&`
373 /// use once_cell::unsync::OnceCell;
375 /// let cell = OnceCell::new();
384 pub struct OnceCell<T> {
389 // Similarly to a `Sync` bound on `sync::OnceCell`, we can use
390 // `&unsync::OnceCell` to sneak a `T` through `catch_unwind`,
393 impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceCell<T> {}
394 impl<T: UnwindSafe> UnwindSafe for OnceCell<T> {}
396 impl<T> Default for OnceCell<T> {
402 impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
405 Some(v) => f.debug_tuple("OnceCell").field(v).finish(),
406 None => f.write_str("OnceCell(Uninit)"),
411 impl<T: Clone> Clone for OnceCell<T> {
412 fn clone(&self) -> OnceCell<T> {
414 Some(value) => OnceCell::with_value(value.clone()),
415 None => OnceCell::new(),
427 impl<T: PartialEq> PartialEq for OnceCell<T> {
433 impl<T: Eq> Eq for OnceCell<T> {}
435 impl<T> From<T> for OnceCell<T> {
437 OnceCell::with_value(value)
441 impl<T> OnceCell<T> {
443 pub const fn new() -> OnceCell<T> {
444 OnceCell { inner: UnsafeCell::new(None) }
448 pub const fn with_value(value: T) -> OnceCell<T> {
449 OnceCell { inner: UnsafeCell::new(Some(value)) }
465 /// This method is allowed to violate the invariant of writing to a `OnceCell`
470 /// use once_cell::unsync::OnceCell;
472 /// let mut cell: OnceCell<u32> = OnceCell::new();
490 /// use once_cell::unsync::OnceCell;
492 /// let cell = OnceCell::new();
511 /// use once_cell::unsync::OnceCell;
513 /// let cell = OnceCell::new();
548 /// use once_cell::unsync::OnceCell;
550 /// let cell = OnceCell::new();
581 /// use once_cell::unsync::OnceCell;
583 /// let cell = OnceCell::new();
608 /// Takes the value out of this `OnceCell`, moving it back to an uninitialized state.
610 /// Has no effect and returns `None` if the `OnceCell` hasn't been initialized.
615 /// use once_cell::unsync::OnceCell;
617 /// let mut cell: OnceCell<String> = OnceCell::new();
620 /// let mut cell = OnceCell::new();
626 /// This method is allowed to violate the invariant of writing to a `OnceCell`
631 /// use once_cell::unsync::OnceCell;
633 /// let mut cell: OnceCell<u32> = OnceCell::new();
635 /// cell = OnceCell::new();
641 /// Consumes the `OnceCell`, returning the wrapped value.
648 /// use once_cell::unsync::OnceCell;
650 /// let cell: OnceCell<String> = OnceCell::new();
653 /// let cell = OnceCell::new();
685 cell: OnceCell<T>,
689 impl<T, F: RefUnwindSafe> RefUnwindSafe for Lazy<T, F> where OnceCell<T>: RefUnwindSafe {}
713 Lazy { cell: OnceCell::new(), init: Cell::new(Some(init)) }
826 /// Thread-safe, blocking version of `OnceCell`.
836 use super::{imp::OnceCell as Imp, unwrap_unchecked};
840 /// `OnceCell` provides `&` references to the contents without RAII guards.
842 /// Reading a non-`None` value out of `OnceCell` establishes a
850 /// use once_cell::sync::OnceCell;
852 /// static CELL: OnceCell<String> = OnceCell::new();
866 pub struct OnceCell<T>(Imp<T>);
868 impl<T> Default for OnceCell<T> {
869 fn default() -> OnceCell<T> {
870 OnceCell::new()
874 impl<T: fmt::Debug> fmt::Debug for OnceCell<T> {
877 Some(v) => f.debug_tuple("OnceCell").field(v).finish(),
878 None => f.write_str("OnceCell(Uninit)"),
883 impl<T: Clone> Clone for OnceCell<T> {
884 fn clone(&self) -> OnceCell<T> {
899 impl<T> From<T> for OnceCell<T> {
905 impl<T: PartialEq> PartialEq for OnceCell<T> {
906 fn eq(&self, other: &OnceCell<T>) -> bool {
911 impl<T: Eq> Eq for OnceCell<T> {}
913 impl<T> OnceCell<T> {
915 pub const fn new() -> OnceCell<T> {
916 OnceCell(Imp::new())
920 pub const fn with_value(value: T) -> OnceCell<T> {
921 OnceCell(Imp::with_value(value))
941 /// use once_cell::sync::OnceCell;
943 /// let mut cell = std::sync::Arc::new(OnceCell::new());
972 /// This method is allowed to violate the invariant of writing to a `OnceCell`
977 /// use once_cell::sync::OnceCell;
979 /// let mut cell: OnceCell<u32> = OnceCell::new();
981 /// cell = OnceCell::new();
1008 /// use once_cell::sync::OnceCell;
1010 /// static CELL: OnceCell<i32> = OnceCell::new();
1035 /// use once_cell::unsync::OnceCell;
1037 /// let cell = OnceCell::new();
1072 /// use once_cell::sync::OnceCell;
1074 /// let cell = OnceCell::new();
1106 /// use once_cell::sync::OnceCell;
1108 /// let cell = OnceCell::new();
1133 /// Takes the value out of this `OnceCell`, moving it back to an uninitialized state.
1135 /// Has no effect and returns `None` if the `OnceCell` hasn't been initialized.
1140 /// use once_cell::sync::OnceCell;
1142 /// let mut cell: OnceCell<String> = OnceCell::new();
1145 /// let mut cell = OnceCell::new();
1151 /// This method is allowed to violate the invariant of writing to a `OnceCell`
1156 /// use once_cell::sync::OnceCell;
1158 /// let mut cell: OnceCell<u32> = OnceCell::new();
1160 /// cell = OnceCell::new();
1166 /// Consumes the `OnceCell`, returning the wrapped value. Returns
1172 /// use once_cell::sync::OnceCell;
1174 /// let cell: OnceCell<String> = OnceCell::new();
1177 /// let cell = OnceCell::new();
1221 cell: OnceCell<T>,
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 {}
1244 Lazy { cell: OnceCell::new(), init: Cell::new(Some(f)) }
1358 /// share(&once_cell::sync::OnceCell::<S>::new());