Skip to main content

rw_builder/
tcp_stream.rs

1use std::net::{TcpStream, ToSocketAddrs};
2
3use crate::Result;
4
5use crate::RwBuilder;
6
7/// Type for building readers and writers on top of a connected TCP socket.
8/// It is itself an `RwBuilder`, but can't be created through one.
9/// This is why we call it a source.
10#[derive(Debug)]
11#[must_use]
12pub struct Builder<A>
13where
14    A: ToSocketAddrs,
15{
16    /// The address to connect to
17    addr: A,
18}
19
20impl<A> Builder<A>
21where
22    A: ToSocketAddrs,
23{
24    /// Factory function to create a builder holding on to a socket address
25    pub const fn new(addr: A) -> Self {
26        Self { addr }
27    }
28}
29
30impl<A> RwBuilder for Builder<A>
31where
32    A: ToSocketAddrs,
33{
34    type Reader = TcpStream;
35    type Writer = TcpStream;
36
37    fn reader(&self) -> Result<Self::Reader> {
38        let stream = TcpStream::connect(&self.addr)?;
39        Ok(stream)
40    }
41
42    fn writer(&self) -> Result<Self::Writer> {
43        let stream = TcpStream::connect(&self.addr)?;
44        Ok(stream)
45    }
46}