-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract a set-exactly-once boolean value
- Loading branch information
1 parent
35dcf44
commit e491da4
Showing
2 changed files
with
37 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use std::sync::{ | ||
atomic::{AtomicBool, Ordering}, | ||
Once, | ||
}; | ||
|
||
pub struct OnceBool { | ||
start: Once, | ||
enabled: AtomicBool, | ||
} | ||
|
||
impl OnceBool { | ||
pub const fn new() -> Self { | ||
Self { | ||
start: Once::new(), | ||
enabled: AtomicBool::new(false), | ||
} | ||
} | ||
|
||
pub fn get<F: FnOnce() -> bool>(&self, f: F) -> bool { | ||
self.start.call_once(|| { | ||
let enabled = f(); | ||
self.enabled.store(enabled, Ordering::SeqCst); | ||
}); | ||
|
||
self.enabled.load(Ordering::SeqCst) | ||
} | ||
} |