Add example for Executor::leak

This commit is contained in:
james7132 2024-04-11 18:34:27 -07:00
parent 5aa751ccf3
commit 48134c7055
No known key found for this signature in database
GPG Key ID: BDB0606BDC85825B
1 changed files with 15 additions and 0 deletions

View File

@ -13,6 +13,21 @@ impl Executor<'static> {
/// Largely equivalent to calling `Box::leak(Box::new(executor))`, but the produced
/// [`LeakedExecutor`]'s functions are optimized to require fewer synchronizing operations
/// when spawning, running, and finishing tasks.
///
/// # Example
///
/// ```
/// use async_executor::Executor;
/// use futures_lite::future;
///
/// let ex = Executor::new().leak();
///
/// let task = ex.spawn(async {
/// println!("Hello world");
/// });
///
/// future::block_on(ex.run(task));
/// ```
pub fn leak(self) -> LeakedExecutor {
let ptr = self.state_ptr();
// SAFETY: So long as an Executor lives, it's state pointer will always be valid