interprocess/local_socket/tokio/stream/
trait.rs

1#![allow(private_bounds)]
2
3use {
4    crate::{
5        bound_util::{RefTokioAsyncRead, RefTokioAsyncWrite},
6        local_socket::Name,
7        Sealed,
8    },
9    std::{future::Future, io},
10    tokio::io::{AsyncRead, AsyncWrite},
11};
12
13/// Tokio local socket stream implementations.
14///
15/// Types on which this trait is implemented are variants of the
16/// [`Stream` enum](super::enum::Stream). In addition, it is implemented on `Stream` itself, which
17/// makes it a trait object of sorts. See its documentation for more on the semantics of the methods
18/// seen here.
19pub trait Stream:
20    AsyncRead + RefTokioAsyncRead + AsyncWrite + RefTokioAsyncWrite + Send + Sync + Sized + Sealed
21{
22    /// Receive half type returned by [`.split()`](Stream::split).
23    type RecvHalf: RecvHalf<Stream = Self>;
24    /// Send half type returned by [`.split()`](Stream::split).
25    type SendHalf: SendHalf<Stream = Self>;
26
27    /// Asynchronously connects to a remote local socket server.
28    fn connect(name: Name<'_>) -> impl Future<Output = io::Result<Self>> + Send + Sync;
29
30    /// Splits a stream into a receive half and a send half, which can be used to receive from and
31    /// send to the stream concurrently from different Tokio tasks, entailing a memory allocation.
32    fn split(self) -> (Self::RecvHalf, Self::SendHalf);
33
34    /// Attempts to reunite a receive half with a send half to yield the original stream back,
35    /// returning both halves as an error if they belong to different streams (or when using this
36    /// method on streams that haven't been split to begin with).
37    fn reunite(rh: Self::RecvHalf, sh: Self::SendHalf) -> ReuniteResult<Self>;
38}
39
40/// Receive halves of Tokio [`Stream`]s, obtained through [`.split()`](Stream::split).
41///
42/// Types on which this trait is implemented are variants of the
43/// [`RecvHalf` enum](super::enum::RecvHalf). In addition, it is implemented on `RecvHalf` itself,
44/// which makes it a trait object of sorts.
45pub trait RecvHalf: Sized + AsyncRead + RefTokioAsyncRead + Sealed {
46    /// The stream type the half is split from.
47    type Stream: Stream;
48}
49
50/// Send halves of Tokio [`Stream`]s, obtained through [`.split()`](Stream::split).
51///
52/// Types on which this trait is implemented are variants of the
53/// [`SendHalf` enum](super::enum::SendHalf). In addition, it is implemented on `SendHalf` itself,
54/// which makes it a trait object of sorts.
55pub trait SendHalf: Sized + AsyncWrite + RefTokioAsyncWrite + Sealed {
56    /// The stream type the half is split from.
57    type Stream: Stream;
58}
59
60/// [`ReuniteResult`](crate::error::ReuniteResult) for the [Tokio `Stream` trait](Stream).
61pub type ReuniteResult<S> =
62    crate::error::ReuniteResult<S, <S as Stream>::RecvHalf, <S as Stream>::SendHalf>;