Skip to content

Commit

Permalink
Merge pull request #4 from IsaacDynamo/no-panic
Browse files Browse the repository at this point in the history
No panic init variant
  • Loading branch information
Dirbaio authored Jul 3, 2023
2 parents 914ea2a + 04c9f0e commit a0b85db
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ impl<T> StaticCell<T> {
}
}

/// Initialize the `StaticCell` with a value, returning a mutable reference to it,
/// if no value is stored in it yet.
///
/// Using this method, the compiler usually constructs `val` in the stack and then moves
/// it into the `StaticCell`. If `T` is big, this is likely to cause stack overflows.
/// Considering using [`StaticCell::try_init_with`] instead, which will construct it in-place inside the `StaticCell`.
///
/// Will only return a Some(&'static mut T) when the `StaticCell` was not yet initialized.
#[inline]
#[allow(clippy::mut_from_ref)]
pub fn try_init(&'static self, val: T) -> Option<&'static mut T> {
Some(self.try_uninit()?.write(val))
}

/// Initialize the `StaticCell` with the closure's return value, returning a mutable reference to it,
/// if no value is stored in it yet.
///
/// Will only return a Some(&'static mut T) when the `StaticCell` was not yet initialized.
#[inline]
#[allow(clippy::mut_from_ref)]
pub fn try_init_with(&'static self, val: impl FnOnce() -> T) -> Option<&'static mut T> {
Some(self.try_uninit()?.write(val()))
}

/// Returns a mutable reference to the uninitialized data owned by the `StaticCell`,
/// if no value is stored in it yet.
///
Expand Down

0 comments on commit a0b85db

Please sign in to comment.