Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve concerns for the original PR
- `set_executor` and `register_runtime` are changed to allow multiple calls, at the expense of slightly worse error messages when they aren't called as necessary. - Renamed the top-level re-export to `tasks` instead of `asn`. - Added documentation for previously undocumented public items.
- Loading branch information
Showing
11 changed files
with
34 additions
and
89 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 |
---|---|---|
@@ -1,45 +1,21 @@ | ||
use std::cell::Cell; | ||
|
||
use futures_task::LocalSpawn; | ||
use once_cell::unsync::OnceCell as UnsyncCell; | ||
use thiserror::Error; | ||
|
||
thread_local!( | ||
static LOCAL_SPAWN: UnsyncCell<&'static dyn LocalSpawn> = UnsyncCell::new(); | ||
static LOCAL_SPAWN: Cell<Option<&'static dyn LocalSpawn>> = Cell::new(None); | ||
); | ||
|
||
/// Error returned by `set_*_executor` if an executor of the kind has already been set. | ||
#[derive(Error, Debug)] | ||
#[error("an executor is already set")] | ||
pub struct SetExecutorError { | ||
_private: (), | ||
} | ||
|
||
impl SetExecutorError { | ||
fn new() -> Self { | ||
SetExecutorError { _private: () } | ||
} | ||
} | ||
|
||
pub(crate) fn local_spawn() -> Option<&'static dyn LocalSpawn> { | ||
LOCAL_SPAWN.with(|cell| cell.get().copied()) | ||
LOCAL_SPAWN.with(|cell| cell.get()) | ||
} | ||
|
||
/// Sets the global executor for the current thread to a `Box<dyn LocalSpawn>`. This value is leaked. | ||
pub fn set_boxed_executor(sp: Box<dyn LocalSpawn>) -> Result<(), SetExecutorError> { | ||
pub fn set_boxed_executor(sp: Box<dyn LocalSpawn>) { | ||
set_executor(Box::leak(sp)) | ||
} | ||
|
||
/// Sets the global executor for the current thread to a `&'static dyn LocalSpawn`. | ||
pub fn set_executor(sp: &'static dyn LocalSpawn) -> Result<(), SetExecutorError> { | ||
LOCAL_SPAWN.with(|cell| cell.set(sp).map_err(|_| SetExecutorError::new())) | ||
} | ||
|
||
/// Sets the global executor for the current thread with a function that will only be called | ||
/// if an executor isn't set yet. | ||
pub fn ensure_executor_with<F>(f: F) | ||
where | ||
F: FnOnce() -> &'static dyn LocalSpawn, | ||
{ | ||
LOCAL_SPAWN.with(|cell| { | ||
cell.get_or_init(f); | ||
}); | ||
pub fn set_executor(sp: &'static dyn LocalSpawn) { | ||
LOCAL_SPAWN.with(|cell| cell.set(Some(sp))) | ||
} |
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
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
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
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
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