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/// Identifies an executor as being able to run a transport.
25///
26/// Implementing `RunsTransport` is optional and only enables some more
27/// convenient spawning APIs.
28pub trait RunsTransport<T: ?Sized> {}
29
30/// A transport which has an executor to spawn on.
31///
32/// Choosing an executor is optional and only enables some more convenient
33/// spawning APIs.
34pub trait HasExecutor {
35 /// The executor to spawn on. It must be able to run this transport.
36 type Executor: Executor + RunsTransport<Self>;
37
38 /// Returns a reference to the executor for this transport.
39 fn executor(&self) -> Self::Executor;
40}
41
42// Mpsc doesn't integrate with any executor internals, and so can run on any
43// executor.
44impl<E> RunsTransport<fidl_next_protocol::mpsc::Mpsc> for E {}