hypercore_protocol/
builder.rs

1use crate::Protocol;
2use crate::{duplex::Duplex, protocol::Options};
3use futures_lite::io::{AsyncRead, AsyncWrite};
4
5/// Build a Protocol instance with options.
6#[derive(Debug)]
7pub struct Builder(Options);
8
9impl Builder {
10    /// Create a protocol builder as initiator (true) or responder (false).
11    pub fn new(initiator: bool) -> Self {
12        Self(Options::new(initiator))
13    }
14
15    /// Set encrypted option. Defaults to true.
16    pub fn encrypted(mut self, encrypted: bool) -> Self {
17        self.0.encrypted = encrypted;
18        self
19    }
20
21    /// Set handshake option. Defaults to true.
22    pub fn handshake(mut self, handshake: bool) -> Self {
23        self.0.noise = handshake;
24        self
25    }
26
27    /// Create the protocol from a stream that implements AsyncRead + AsyncWrite + Clone.
28    pub fn connect<IO>(self, io: IO) -> Protocol<IO>
29    where
30        IO: AsyncRead + AsyncWrite + Send + Unpin + 'static,
31    {
32        Protocol::new(io, self.0)
33    }
34
35    /// Create the protocol from an AsyncRead reader and AsyncWrite writer.
36    pub fn connect_rw<R, W>(self, reader: R, writer: W) -> Protocol<Duplex<R, W>>
37    where
38        R: AsyncRead + Send + Unpin + 'static,
39        W: AsyncWrite + Send + Unpin + 'static,
40    {
41        let io = Duplex::new(reader, writer);
42        Protocol::new(io, self.0)
43    }
44}