Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added get_mut_unchecked #232

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/imp_cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ impl<T> OnceCell<T> {
self.value.borrow(CriticalSection::new()).get().unwrap_unchecked()
}

/// Gets the mutable reference to the underlying value, without checking if the cell
/// is initialized.
///
/// # Safety
///
/// Caller must ensure that the cell is in initialized state, and that
/// the contents are acquired by (synchronized to) this thread.
pub(crate) unsafe fn get_mut_unchecked(&mut self) -> &mut T {
debug_assert!(self.is_initialized());
// SAFETY: The caller ensures that the value is initialized and access synchronized.
let slot = &mut *self.value.get();
slot.as_mut().unwrap_unchecked()
}

#[inline]
pub(crate) fn get_mut(&mut self) -> Option<&mut T> {
self.value.get_mut().get_mut()
Expand Down
13 changes: 13 additions & 0 deletions src/imp_pl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,19 @@ impl<T> OnceCell<T> {
slot.as_ref().unwrap_unchecked()
}

/// Gets the mutable reference to the underlying value, without checking if the cell
/// is initialized.
///
/// # Safety
///
/// Caller must ensure that the cell is in initialized state, and that
/// the contents are acquired by (synchronized to) this thread.
pub(crate) unsafe fn get_mut_unchecked(&mut self) -> &mut T {
debug_assert!(self.is_initialized());
let slot = &mut *self.value.get();
slot.as_mut().unwrap_unchecked()
}

/// Gets the mutable reference to the underlying value.
/// Returns `None` if the cell is empty.
pub(crate) fn get_mut(&mut self) -> Option<&mut T> {
Expand Down
13 changes: 13 additions & 0 deletions src/imp_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ impl<T> OnceCell<T> {
slot.as_ref().unwrap_unchecked()
}

/// Gets the reference to the underlying value, without checking if the cell
/// is initialized.
///
/// # Safety
///
/// Caller must ensure that the cell is in initialized state, and that
/// the contents are acquired by (synchronized to) this thread.
pub(crate) unsafe fn get_mut_unchecked(&mut self) -> &mut T {
debug_assert!(self.is_initialized());
let slot = &mut *self.value.get();
slot.as_mut().unwrap_unchecked()
}

/// Gets the mutable reference to the underlying value.
/// Returns `None` if the cell is empty.
pub(crate) fn get_mut(&mut self) -> Option<&mut T> {
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,18 @@ pub mod sync {
self.0.get_unchecked()
}

/// Get the mutable reference to the underlying value, without checking if the
/// cell is initialized.
///
/// # Safety
///
/// Caller must ensure that the cell is in initialized state, and that
/// the contents are acquired by (synchronized to) this thread.
#[inline]
pub unsafe fn get_mut_unchecked(&mut self) -> &mut T {
self.0.get_mut_unchecked()
}

/// Sets the contents of this cell to `value`.
///
/// Returns `Ok(())` if the cell was empty and `Err(value)` if it was
Expand Down