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 task which completes with the output of a future.
10 ///
11 /// `Tasks`s have abort-on-drop semantics.
12 type Task<T>: Future<Output = T>
13 where
14 T: 'static;
15
16 /// Spawns the given future on this executor, returning a `Task` for the
17 /// task.
18 fn spawn<F>(&self, future: F) -> Self::Task<F::Output>
19 where
20 F: Future + Send + 'static,
21 F::Output: Send + 'static;
22
23 /// Detaches the given task so that it can run independely in the
24 /// background.
25 fn detach<T>(&self, task: Self::Task<T>);
26}
27
28/// Identifies an executor as being able to run a transport.
29///
30/// Implementing `RunsTransport` is optional and only enables some more
31/// convenient spawning APIs.
32pub trait RunsTransport<T: ?Sized> {}
33
34/// A transport which has an executor to spawn on.
35///
36/// Choosing an executor is optional and only enables some more convenient
37/// spawning APIs.
38pub trait HasExecutor {
39 /// The executor to spawn on. It must be able to run this transport.
40 type Executor: Executor + RunsTransport<Self>;
41
42 /// Returns a reference to the executor for this transport.
43 fn executor(&self) -> Self::Executor;
44}
45
46// Mpsc doesn't integrate with any executor internals, and so can run on any
47// executor.
48impl<E> RunsTransport<fidl_next_protocol::mpsc::Mpsc> for E {}