Lines Matching refs:cell
3 //! `once_cell` provides two new cell-like types, [`unsync::OnceCell`] and [`sync::OnceCell`]. A `OnceCell`
23 //! [`RefCell`]: https://doc.rust-lang.org/std/cell/struct.RefCell.html
174 //! cell: OnceCell<Vec<u8>>,
179 //! TestResource { path, cell: OnceCell::new() }
182 //! self.cell.get_or_init(|| {
211 //! pub struct LateInit<T> { cell: OnceCell<T> }
215 //! assert!(self.cell.set(value).is_ok())
220 //! fn default() -> Self { LateInit { cell: OnceCell::default() } }
226 //! self.cell.get().unwrap()
325 //! * [double-checked-cell](https://github.com/niklasf/double-checked-cell)
356 cell::{Cell, UnsafeCell},
364 /// A cell which can be written to only once. It is not thread safe.
366 /// Unlike [`std::cell::RefCell`], a `OnceCell` provides simple `&`
369 /// [`std::cell::RefCell`]: https://doc.rust-lang.org/std/cell/struct.RefCell.html
375 /// let cell = OnceCell::new();
376 /// assert!(cell.get().is_none());
378 /// let value: &String = cell.get_or_init(|| {
382 /// assert!(cell.get().is_some());
391 // by initializing the cell in closure and extracting the value in the
442 /// Creates a new empty cell.
447 /// Creates a new initialized cell.
454 /// Returns `None` if the cell is empty.
463 /// Returns `None` if the cell is empty.
472 /// let mut cell: OnceCell<u32> = OnceCell::new();
473 /// cell.set(92).unwrap();
474 /// *cell.get_mut().unwrap() = 93;
475 /// assert_eq!(cell.get(), Some(&93));
483 /// Sets the contents of this cell to `value`.
485 /// Returns `Ok(())` if the cell was empty and `Err(value)` if it was
492 /// let cell = OnceCell::new();
493 /// assert!(cell.get().is_none());
495 /// assert_eq!(cell.set(92), Ok(()));
496 /// assert_eq!(cell.set(62), Err(62));
498 /// assert!(cell.get().is_some());
507 /// Like [`set`](Self::set), but also returns a reference to the final cell value.
513 /// let cell = OnceCell::new();
514 /// assert!(cell.get().is_none());
516 /// assert_eq!(cell.try_insert(92), Ok(&92));
517 /// assert_eq!(cell.try_insert(62), Err((&92, 62)));
519 /// assert!(cell.get().is_some());
535 /// Gets the contents of the cell, initializing it with `f`
536 /// if the cell was empty.
540 /// If `f` panics, the panic is propagated to the caller, and the cell
543 /// It is an error to reentrantly initialize the cell from `f`. Doing
550 /// let cell = OnceCell::new();
551 /// let value = cell.get_or_init(|| 92);
553 /// let value = cell.get_or_init(|| unreachable!());
567 /// Gets the contents of the cell, initializing it with `f` if
568 /// the cell was empty. If the cell was empty and `f` failed, an
573 /// If `f` panics, the panic is propagated to the caller, and the cell
576 /// It is an error to reentrantly initialize the cell from `f`. Doing
583 /// let cell = OnceCell::new();
584 /// assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
585 /// assert!(cell.get().is_none());
586 /// let value = cell.get_or_try_init(|| -> Result<i32, ()> {
590 /// assert_eq!(cell.get(), Some(&92))
617 /// let mut cell: OnceCell<String> = OnceCell::new();
618 /// assert_eq!(cell.take(), None);
620 /// let mut cell = OnceCell::new();
621 /// cell.set("hello".to_string()).unwrap();
622 /// assert_eq!(cell.take(), Some("hello".to_string()));
623 /// assert_eq!(cell.get(), None);
633 /// let mut cell: OnceCell<u32> = OnceCell::new();
634 /// cell.set(92).unwrap();
635 /// cell = OnceCell::new();
643 /// Returns `None` if the cell was empty.
650 /// let cell: OnceCell<String> = OnceCell::new();
651 /// assert_eq!(cell.into_inner(), None);
653 /// let cell = OnceCell::new();
654 /// cell.set("hello".to_string()).unwrap();
655 /// assert_eq!(cell.into_inner(), Some("hello".to_string()));
685 cell: OnceCell<T>,
693 f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
713 Lazy { cell: OnceCell::new(), init: Cell::new(Some(init)) }
720 let cell = this.cell;
722 cell.into_inner().ok_or_else(|| {
744 this.cell.get_or_init(|| match this.init.take() {
783 this.cell.get()
800 this.cell.get_mut()
814 self.cell.get_mut().unwrap_or_else(|| unreachable!())
830 cell::Cell,
838 /// A thread-safe cell which can be written to only once.
844 /// thread A initializes the cell with `get_or_init(f)`, and thread B
914 /// Creates a new empty cell.
919 /// Creates a new initialized cell.
926 /// Returns `None` if the cell is empty, or being initialized. This
943 /// let mut cell = std::sync::Arc::new(OnceCell::new());
945 /// let cell = std::sync::Arc::clone(&cell);
946 /// move || cell.set(92).unwrap()
950 /// let _value_or_none = cell.get();
953 /// let value: &u32 = cell.wait();
970 /// Returns `None` if the cell is empty.
979 /// let mut cell: OnceCell<u32> = OnceCell::new();
980 /// cell.set(92).unwrap();
981 /// cell = OnceCell::new();
989 /// cell is initialized.
993 /// Caller must ensure that the cell is in initialized state, and that
1000 /// Sets the contents of this cell to `value`.
1002 /// Returns `Ok(())` if the cell was empty and `Err(value)` if it was
1030 /// Like [`set`](Self::set), but also returns a reference to the final cell value.
1037 /// let cell = OnceCell::new();
1038 /// assert!(cell.get().is_none());
1040 /// assert_eq!(cell.try_insert(92), Ok(&92));
1041 /// assert_eq!(cell.try_insert(62), Err((&92, 62)));
1043 /// assert!(cell.get().is_some());
1054 /// Gets the contents of the cell, initializing it with `f` if the cell
1063 /// If `f` panics, the panic is propagated to the caller, and the cell
1066 /// It is an error to reentrantly initialize the cell from `f`. The
1074 /// let cell = OnceCell::new();
1075 /// let value = cell.get_or_init(|| 92);
1077 /// let value = cell.get_or_init(|| unreachable!());
1091 /// Gets the contents of the cell, initializing it with `f` if
1092 /// the cell was empty. If the cell was empty and `f` failed, an
1098 /// the cell remains uninitialized.
1100 /// It is an error to reentrantly initialize the cell from `f`.
1108 /// let cell = OnceCell::new();
1109 /// assert_eq!(cell.get_or_try_init(|| Err(())), Err(()));
1110 /// assert!(cell.get().is_none());
1111 /// let value = cell.get_or_try_init(|| -> Result<i32, ()> {
1115 /// assert_eq!(cell.get(), Some(&92))
1142 /// let mut cell: OnceCell<String> = OnceCell::new();
1143 /// assert_eq!(cell.take(), None);
1145 /// let mut cell = OnceCell::new();
1146 /// cell.set("hello".to_string()).unwrap();
1147 /// assert_eq!(cell.take(), Some("hello".to_string()));
1148 /// assert_eq!(cell.get(), None);
1158 /// let mut cell: OnceCell<u32> = OnceCell::new();
1159 /// cell.set(92).unwrap();
1160 /// cell = OnceCell::new();
1167 /// `None` if the cell was empty.
1174 /// let cell: OnceCell<String> = OnceCell::new();
1175 /// assert_eq!(cell.into_inner(), None);
1177 /// let cell = OnceCell::new();
1178 /// cell.set("hello".to_string()).unwrap();
1179 /// assert_eq!(cell.into_inner(), Some("hello".to_string()));
1221 cell: OnceCell<T>,
1227 f.debug_struct("Lazy").field("cell", &self.cell).field("init", &"..").finish()
1244 Lazy { cell: OnceCell::new(), init: Cell::new(Some(f)) }
1251 let cell = this.cell;
1253 cell.into_inner().ok_or_else(|| {
1274 this.cell.get_or_init(|| match this.init.take() {
1311 this.cell.get()
1328 this.cell.get_mut()
1342 self.cell.get_mut().unwrap_or_else(|| unreachable!())