futures_executor/
enter.rs1use std::cell::Cell;
2use std::fmt;
3
4std::thread_local!(static ENTERED: Cell<bool> = const { Cell::new(false) });
5
6pub struct Enter {
10 _priv: (),
11}
12
13pub struct EnterError {
16 _priv: (),
17}
18
19impl fmt::Debug for EnterError {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 f.debug_struct("EnterError").finish()
22 }
23}
24
25impl fmt::Display for EnterError {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 write!(f, "an execution scope has already been entered")
28 }
29}
30
31impl std::error::Error for EnterError {}
32
33pub fn enter() -> Result<Enter, EnterError> {
56 ENTERED.with(|c| {
57 if c.get() {
58 Err(EnterError { _priv: () })
59 } else {
60 c.set(true);
61
62 Ok(Enter { _priv: () })
63 }
64 })
65}
66
67impl fmt::Debug for Enter {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 f.debug_struct("Enter").finish()
70 }
71}
72
73impl Drop for Enter {
74 fn drop(&mut self) {
75 ENTERED.with(|c| {
76 assert!(c.get());
77 c.set(false);
78 });
79 }
80}