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

feat: add take and into_inner methods to GILOnceCell #3556

Merged
merged 3 commits into from
Oct 29, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ impl<T> GILOnceCell<T> {
*inner = Some(value);
Ok(())
}

/// Takes the value out of the cell, moving it back to an uninitialized state.
///
/// Has no effect and returns None if the cell has not yet been written.
pub fn take(&mut self) -> Option<T> {
self.0.get_mut().take()
adamreichold marked this conversation as resolved.
Show resolved Hide resolved
}

/// Consumes the cell, returning the wrapped value.
///
/// Returns None if the cell has not yet been written.
pub fn into_inner(self) -> Option<T> {
self.0.into_inner()
}
}

impl GILOnceCell<Py<PyType>> {
Expand Down Expand Up @@ -278,7 +292,7 @@ mod tests {
#[test]
fn test_once_cell() {
Python::with_gil(|py| {
let cell = GILOnceCell::new();
let mut cell = GILOnceCell::new();

assert!(cell.get(py).is_none());

Expand All @@ -289,6 +303,9 @@ mod tests {
assert_eq!(cell.get(py), Some(&2));

assert_eq!(cell.get_or_try_init(py, || Err(5)), Ok(&2));

assert_eq!(cell.take(), Some(2));
assert_eq!(cell.into_inner(), None)
})
}
}