pub struct LocalSpawnPool(/* private fields */);Expand description
A pool of tasks to spawn futures and wait for them on a single thread.
It is inspired by and has almost the same functionality as tokio::task::LocalSet,
but this standalone crate allows you to avoid importing the whole tokio crate if you don’t need it.
Unlike the tokio::task::LocalSet, LocalSpawnPool doesn’t
handle panics.
In some cases, it is necessary to run one or more futures that do not implement Send and thus are unsafe to send between
threads. In these cases, a LocalSpawnPool may be used to schedule one or more !Send futures to run together on the same
thread.
You can use the LocalSpawnPool::run_until function to run a future to completion on the LocalSpawnPool, returning its
output (see LocalSpawnPool::run_until for more details). And you can use the LocalSpawnPool::spawn and spawn
functions to spawn futures on the LocalSpawnPool. To wait for all the spawned futures to complete, await the
LocalSpawnPool itself:
§Awaiting the LocalSpawnPool
Example:
use local_spawn_pool::LocalSpawnPool;
async fn run() {
let pool = LocalSpawnPool::new();
pool.spawn(async {
// This future will be spawned inside `pool`
local_spawn_pool::spawn(async {
// This future will be spawned inside `pool`
local_spawn_pool::spawn(async {
// This future will be spawned inside `pool`
});
});
local_spawn_pool::spawn(async {
// This future will be spawned inside `pool`
});
});
pool.await; // Will wait for all the futures inside the local_spawn_pool to complete
}Awaiting a LocalSpawnPool is !Send.
Implementations§
Source§impl LocalSpawnPool
impl LocalSpawnPool
Sourcepub fn new() -> Self
pub fn new() -> Self
Returns a new LocalSpawnPool.
Sourcepub async fn run_until<F>(&self, future: F) -> F::Outputwhere
F: Future + 'static,
pub async fn run_until<F>(&self, future: F) -> F::Outputwhere
F: Future + 'static,
Runs a future to completion on the LocalSpawnPool, returning its output.
This returns a future that runs the given future in a LocalSpawnPool, allowing it to call spawn to spawn additional
!Send futures. Any futures spawned on the LocalSpawnPool will be driven in the background until the future passed to
run_until completes. When the future passed to run_until finishes, any futures which have not completed will remain
on the LocalSpawnPool, and will be driven on subsequent calls to run_until or when
awaiting the LocalSpawnPool itself.
Sourcepub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> ⓘwhere
F: Future + 'static,
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> ⓘwhere
F: Future + 'static,
Spawns a !Send task onto the LocalSpawnPool.
This task is guaranteed to be run on the current thread.
Unlike the free function spawn, this method may be used to spawn local tasks when the LocalSpawnPool is not running.
The provided future will start running once the LocalSpawnPool is next started, even if you don’t await the returned
JoinHandle.
Trait Implementations§
Source§impl Default for LocalSpawnPool
impl Default for LocalSpawnPool
Source§impl Future for LocalSpawnPool
impl Future for LocalSpawnPool
Auto Trait Implementations§
impl !Freeze for LocalSpawnPool
impl !RefUnwindSafe for LocalSpawnPool
impl !Send for LocalSpawnPool
impl !Sync for LocalSpawnPool
impl Unpin for LocalSpawnPool
impl !UnwindSafe for LocalSpawnPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
Source§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f. Read moreSource§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
Source§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll will never again be called once it has
completed. This method can be used to turn any Future into a
FusedFuture. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
Source§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
Source§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = ()>.Source§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Future<Output = T> into a
TryFuture<Ok = T, Error = Never>.