Skip to main content

nautilus_network/transport/
stream.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
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
16//! Backend-agnostic WebSocket transport trait.
17
18use std::pin::Pin;
19
20use futures::{Sink, Stream};
21
22use super::{error::TransportError, message::Message};
23
24/// A backend-agnostic, bidirectional WebSocket transport.
25///
26/// This is the trait that the higher layers in `nautilus-network` (the
27/// reconnecting client, the auth tracker, the subscription manager) consume.
28/// Each transport backend implements it for its own native stream type.
29///
30/// The trait combines [`futures::Stream`] for incoming messages and
31/// [`futures::Sink`] for outgoing messages, both keyed off the neutral
32/// [`Message`] type and the neutral [`TransportError`].
33pub trait WsTransport:
34    Stream<Item = Result<Message, TransportError>>
35    + Sink<Message, Error = TransportError>
36    + Send
37    + Unpin
38{
39}
40
41impl<T> WsTransport for T where
42    T: Stream<Item = Result<Message, TransportError>>
43        + Sink<Message, Error = TransportError>
44        + Send
45        + Unpin
46{
47}
48
49/// Boxed, dynamically-dispatched [`WsTransport`].
50///
51/// Used by the higher layers to hide the concrete backend stream type. The
52/// per-backend `connect` functions return this type so callers don't need to
53/// be generic over the backend.
54pub type BoxedWsTransport = Pin<Box<dyn WsTransport>>;