Skip to main content

consortium_ipc/
transport.rs

1// Copyright 2026 Ethan Wu
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16
17//! The `Transport` trait family and related types.
18use crate::future::MaybeSend;
19
20/// Provides the `Error` associated type shared by [`SendTransport`] and [`RecvTransport`].
21pub trait TransportError {
22    type Error;
23}
24
25/// Send half of a transport — moves bytes to the remote side.
26///
27/// The returned future is an opaque `impl Future` (RPITIT), so implementations
28/// may be plain `async fn`s — including ones that await unnameable futures such
29/// as `embedded_io_async::Write::write` — or return hand-rolled named futures.
30pub trait SendTransport: TransportError {
31    /// Write `data` to the transport and ring the doorbell.
32    fn send(&mut self, data: &[u8]) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend;
33
34    /// Maximum bytes transferable in a single send.
35    fn max_send_size(&self) -> usize;
36}
37
38/// Receive half of a transport — awaits bytes from the remote side.
39///
40/// The returned future is an opaque `impl Future` (RPITIT); see
41/// [`SendTransport`] for the implementation latitude this allows.
42pub trait RecvTransport: TransportError {
43    /// Await the doorbell and read bytes into `buf`.
44    /// Returns the number of bytes written into `buf`.
45    fn recv(
46        &mut self,
47        buf: &mut [u8],
48    ) -> impl Future<Output = Result<usize, Self::Error>> + MaybeSend;
49
50    /// Maximum bytes transferable in a single recv.
51    fn max_recv_size(&self) -> usize;
52}
53
54/// Combined transport — covers both send and receive directions.
55///
56/// Implement this for full-duplex types; use [`SendTransport`] / [`RecvTransport`]
57/// directly when only one direction is needed (e.g. after a [`split`]).
58///
59/// [`split`]: crate::channel::Channel
60pub trait Transport: SendTransport + RecvTransport {}