Skip to content

Commit

Permalink
Document the allowed violation of the "once" invariant for `&mut self…
Browse files Browse the repository at this point in the history
…` methods.

Closes #153.
  • Loading branch information
dherman committed Jun 22, 2021
1 parent 8671ffc commit c559b03
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,18 @@ pub mod unsync {
/// Gets a mutable reference to the underlying value.
///
/// Returns `None` if the cell is empty.
///
/// This method is allowed to violate the invariant of writing to a `OnceCell`
/// at most once because it requires `&mut` access to `self`. As with all
/// interior mutability, `&mut` access permits arbitrary modification:
///
/// ```
/// use once_cell::unsync::OnceCell;
///
/// let mut cell: OnceCell<u32> = OnceCell::new();
/// cell.set(92).unwrap();
/// cell = OnceCell::new();
/// ```
pub fn get_mut(&mut self) -> Option<&mut T> {
// Safe because we have unique access
unsafe { &mut *self.inner.get() }.as_mut()
Expand Down Expand Up @@ -590,6 +602,18 @@ pub mod unsync {
/// assert_eq!(cell.take(), Some("hello".to_string()));
/// assert_eq!(cell.get(), None);
/// ```
///
/// This method is allowed to violate the invariant of writing to a `OnceCell`
/// at most once because it requires `&mut` access to `self`. As with all
/// interior mutability, `&mut` access permits arbitrary modification:
///
/// ```
/// use once_cell::unsync::OnceCell;
///
/// let mut cell: OnceCell<u32> = OnceCell::new();
/// cell.set(92).unwrap();
/// cell = OnceCell::new();
/// ```
pub fn take(&mut self) -> Option<T> {
mem::replace(self, Self::default()).into_inner()
}
Expand Down Expand Up @@ -835,6 +859,18 @@ pub mod sync {
/// Gets the mutable reference to the underlying value.
///
/// Returns `None` if the cell is empty.
///
/// This method is allowed to violate the invariant of writing to a `OnceCell`
/// at most once because it requires `&mut` access to `self`. As with all
/// interior mutability, `&mut` access permits arbitrary modification:
///
/// ```
/// use once_cell::sync::OnceCell;
///
/// let mut cell: OnceCell<u32> = OnceCell::new();
/// cell.set(92).unwrap();
/// cell = OnceCell::new();
/// ```
pub fn get_mut(&mut self) -> Option<&mut T> {
self.0.get_mut()
}
Expand Down Expand Up @@ -999,6 +1035,18 @@ pub mod sync {
/// assert_eq!(cell.take(), Some("hello".to_string()));
/// assert_eq!(cell.get(), None);
/// ```
///
/// This method is allowed to violate the invariant of writing to a `OnceCell`
/// at most once because it requires `&mut` access to `self`. As with all
/// interior mutability, `&mut` access permits arbitrary modification:
///
/// ```
/// use once_cell::sync::OnceCell;
///
/// let mut cell: OnceCell<u32> = OnceCell::new();
/// cell.set(92).unwrap();
/// cell = OnceCell::new();
/// ```
pub fn take(&mut self) -> Option<T> {
mem::replace(self, Self::default()).into_inner()
}
Expand Down

0 comments on commit c559b03

Please sign in to comment.