zbus/connection/socket/
split.rs

1use super::{ReadHalf, Socket, WriteHalf};
2
3/// A pair of socket read and write halves.
4#[derive(Debug)]
5pub struct Split<R: ReadHalf, W: WriteHalf> {
6    pub(super) read: R,
7    pub(super) write: W,
8}
9
10impl<R: ReadHalf, W: WriteHalf> Split<R, W> {
11    /// Create split from read and write halves.
12    pub fn new(read: R, write: W) -> Self {
13        Self { read, write }
14    }
15
16    /// Reference to the read half.
17    pub fn read(&self) -> &R {
18        &self.read
19    }
20
21    /// Mutable reference to the read half.
22    pub fn read_mut(&mut self) -> &mut R {
23        &mut self.read
24    }
25
26    /// Reference to the write half.
27    pub fn write(&self) -> &W {
28        &self.write
29    }
30
31    /// Mutable reference to the write half.
32    pub fn write_mut(&mut self) -> &mut W {
33        &mut self.write
34    }
35
36    /// Take the read and write halves.
37    pub fn take(self) -> (R, W) {
38        (self.read, self.write)
39    }
40}
41
42/// A boxed `Split`.
43pub type BoxedSplit = Split<Box<dyn ReadHalf>, Box<dyn WriteHalf>>;
44
45impl<S: Socket> From<S> for BoxedSplit {
46    fn from(socket: S) -> Self {
47        let split = socket.split();
48
49        Split {
50            read: Box::new(split.read),
51            write: Box::new(split.write),
52        }
53    }
54}