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    /// Reference to the read half.
12    pub fn read(&self) -> &R {
13        &self.read
14    }
15
16    /// Mutable reference to the read half.
17    pub fn read_mut(&mut self) -> &mut R {
18        &mut self.read
19    }
20
21    /// Reference to the write half.
22    pub fn write(&self) -> &W {
23        &self.write
24    }
25
26    /// Mutable reference to the write half.
27    pub fn write_mut(&mut self) -> &mut W {
28        &mut self.write
29    }
30
31    /// Take the read and write halves.
32    pub fn take(self) -> (R, W) {
33        (self.read, self.write)
34    }
35}
36
37/// A boxed `Split`.
38pub type BoxedSplit = Split<Box<dyn ReadHalf>, Box<dyn WriteHalf>>;
39
40impl<S: Socket> From<S> for BoxedSplit {
41    fn from(socket: S) -> Self {
42        let split = socket.split();
43
44        Split {
45            read: Box::new(split.read),
46            write: Box::new(split.write),
47        }
48    }
49}