fidl_next_bind/executor.rs
1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use core::future::Future;
6
7/// An executor which futures can be spawned on.
8pub trait Executor {
9 /// A join handle which completes with the output of a future.
10 ///
11 /// `JoinHandle`s have detach-on-drop semantics.
12 type JoinHandle<T>
13 where
14 T: 'static;
15
16 /// Spawns the given future on this executor, returning a `JoinHandle` for the
17 /// task.
18 fn spawn<F>(&self, future: F) -> Self::JoinHandle<F::Output>
19 where
20 F: Future + Send + 'static,
21 F::Output: Send + 'static;
22}
23
24/// A local executor which supports spawning non-`Send` futures.
25pub trait LocalExecutor: Executor {
26 /// Spawns the given non-`Send` future on this executor, returning a
27 /// `JoinHandle` for the task.
28 fn spawn_local<F>(&self, future: F) -> Self::JoinHandle<F::Output>
29 where
30 F: Future + 'static,
31 F::Output: 'static;
32}
33
34/// Identifies an executor as being able to run a transport.
35///
36/// Implementing `RunsTransport` is optional and only enables some more
37/// convenient spawning APIs.
38pub trait RunsTransport<T: ?Sized> {}
39
40/// A transport which has an executor to spawn on.
41///
42/// Choosing an executor is optional and only enables some more convenient
43/// spawning APIs.
44pub trait HasExecutor {
45 /// The executor to spawn on. It must be able to run this transport.
46 type Executor: Executor + RunsTransport<Self>;
47
48 /// Returns a reference to the executor for this transport.
49 fn executor(&self) -> Self::Executor;
50}
51
52// Mpsc doesn't integrate with any executor internals, and so can run on any
53// executor.
54impl<E> RunsTransport<fidl_next_protocol::mpsc::Mpsc> for E {}