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

Stabilize poison API of Once, rename poisoned() #81745

Merged
merged 1 commit into from
Feb 5, 2021
Merged
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
20 changes: 7 additions & 13 deletions library/std/src/sync/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ unsafe impl Send for Once {}

/// State yielded to [`Once::call_once_force()`]’s closure parameter. The state
/// can be used to query the poison status of the [`Once`].
#[unstable(feature = "once_poison", issue = "33577")]
#[stable(feature = "once_poison", since = "1.51.0")]
#[derive(Debug)]
pub struct OnceState {
poisoned: bool,
Expand Down Expand Up @@ -280,8 +280,6 @@ impl Once {
/// # Examples
///
/// ```
/// #![feature(once_poison)]
///
/// use std::sync::Once;
/// use std::thread;
///
Expand All @@ -301,13 +299,13 @@ impl Once {
///
/// // call_once_force will still run and reset the poisoned state
/// INIT.call_once_force(|state| {
/// assert!(state.poisoned());
/// assert!(state.is_poisoned());
/// });
///
/// // once any success happens, we stop propagating the poison
/// INIT.call_once(|| {});
/// ```
#[unstable(feature = "once_poison", issue = "33577")]
#[stable(feature = "once_poison", since = "1.51.0")]
pub fn call_once_force<F>(&self, f: F)
where
F: FnOnce(&OnceState),
Expand Down Expand Up @@ -526,8 +524,6 @@ impl OnceState {
/// A poisoned [`Once`]:
///
/// ```
/// #![feature(once_poison)]
///
/// use std::sync::Once;
/// use std::thread;
///
Expand All @@ -540,24 +536,22 @@ impl OnceState {
/// assert!(handle.join().is_err());
///
/// INIT.call_once_force(|state| {
/// assert!(state.poisoned());
/// assert!(state.is_poisoned());
/// });
/// ```
///
/// An unpoisoned [`Once`]:
///
/// ```
/// #![feature(once_poison)]
///
/// use std::sync::Once;
///
/// static INIT: Once = Once::new();
///
/// INIT.call_once_force(|state| {
/// assert!(!state.poisoned());
/// assert!(!state.is_poisoned());
/// });
#[unstable(feature = "once_poison", issue = "33577")]
pub fn poisoned(&self) -> bool {
#[stable(feature = "once_poison", since = "1.51.0")]
pub fn is_poisoned(&self) -> bool {
self.poisoned
}

Expand Down
4 changes: 2 additions & 2 deletions library/std/src/sync/once/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fn poison_bad() {
let mut called = false;
O.call_once_force(|p| {
called = true;
assert!(p.poisoned())
assert!(p.is_poisoned())
});
assert!(called);

Expand All @@ -92,7 +92,7 @@ fn wait_for_force_to_finish() {
let (tx2, rx2) = channel();
let t1 = thread::spawn(move || {
O.call_once_force(|p| {
assert!(p.poisoned());
assert!(p.is_poisoned());
tx1.send(()).unwrap();
rx2.recv().unwrap();
});
Expand Down