/rust/registry/src/index.crates.io-1949cf8c6b5b557f/futures-executor-0.3.32/src/enter.rs
Line | Count | Source |
1 | | use std::cell::Cell; |
2 | | use std::fmt; |
3 | | |
4 | | std::thread_local!(static ENTERED: Cell<bool> = const { Cell::new(false) }); |
5 | | |
6 | | /// Represents an executor context. |
7 | | /// |
8 | | /// For more details, see [`enter` documentation](enter()). |
9 | | pub struct Enter { |
10 | | _priv: (), |
11 | | } |
12 | | |
13 | | /// An error returned by `enter` if an execution scope has already been |
14 | | /// entered. |
15 | | pub struct EnterError { |
16 | | _priv: (), |
17 | | } |
18 | | |
19 | | impl fmt::Debug for EnterError { |
20 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
21 | 0 | f.debug_struct("EnterError").finish() |
22 | 0 | } |
23 | | } |
24 | | |
25 | | impl fmt::Display for EnterError { |
26 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
27 | 0 | write!(f, "an execution scope has already been entered") |
28 | 0 | } |
29 | | } |
30 | | |
31 | | impl std::error::Error for EnterError {} |
32 | | |
33 | | /// Marks the current thread as being within the dynamic extent of an |
34 | | /// executor. |
35 | | /// |
36 | | /// Executor implementations should call this function before beginning to |
37 | | /// execute a task, and drop the returned [`Enter`](Enter) value after |
38 | | /// completing task execution: |
39 | | /// |
40 | | /// ``` |
41 | | /// use futures::executor::enter; |
42 | | /// |
43 | | /// let enter = enter().expect("..."); |
44 | | /// /* run task */ |
45 | | /// drop(enter); |
46 | | /// ``` |
47 | | /// |
48 | | /// Doing so ensures that executors aren't |
49 | | /// accidentally invoked in a nested fashion. |
50 | | /// |
51 | | /// # Error |
52 | | /// |
53 | | /// Returns an error if the current thread is already marked, in which case the |
54 | | /// caller should panic with a tailored error message. |
55 | 0 | pub fn enter() -> Result<Enter, EnterError> { |
56 | 0 | ENTERED.with(|c| { |
57 | 0 | if c.get() { |
58 | 0 | Err(EnterError { _priv: () }) |
59 | | } else { |
60 | 0 | c.set(true); |
61 | | |
62 | 0 | Ok(Enter { _priv: () }) |
63 | | } |
64 | 0 | }) |
65 | 0 | } |
66 | | |
67 | | impl fmt::Debug for Enter { |
68 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
69 | 0 | f.debug_struct("Enter").finish() |
70 | 0 | } |
71 | | } |
72 | | |
73 | | impl Drop for Enter { |
74 | 0 | fn drop(&mut self) { |
75 | 0 | ENTERED.with(|c| { |
76 | 0 | assert!(c.get()); |
77 | 0 | c.set(false); |
78 | 0 | }); |
79 | 0 | } |
80 | | } |