1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use std::io::{Write, Read, Error};

use tokio_io::{AsyncRead, AsyncWrite};
use futures::Poll;

/// Implements both (Async)Read and (Async)Write by delegating to an (Async)Read
/// and an (Async)Write, taking ownership of both.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Duplex<R, W> {
    r: R,
    w: W,
}

impl<R, W> Duplex<R, W> {
    /// Takes ownership of a Read and a Write and creates a new Duplex.
    pub fn new(r: R, w: W) -> Duplex<R, W> {
        Duplex { r, w }
    }

    /// Gets a reference to the underlying reader.
    pub fn get_reader_ref(&self) -> &R {
        &self.r
    }

    /// Gets a mutable reference to the underlying reader.
    pub fn get_reader_mut(&mut self) -> &mut R {
        &mut self.r
    }

    /// Gets a reference to the underlying writer.
    pub fn get_writer_ref(&self) -> &W {
        &self.w
    }

    /// Gets a mutable reference to the underlying writer.
    pub fn get_writer_mut(&mut self) -> &mut W {
        &mut self.w
    }

    /// Unwraps this `Duplex`, returning the underlying reader and writer.
    pub fn into_inner(self) -> (R, W) {
        (self.r, self.w)
    }
}

impl<R: Read, W> Read for Duplex<R, W> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
        self.r.read(buf)
    }
}

impl<R: AsyncRead, W> AsyncRead for Duplex<R, W> {}

impl<R, W: Write> Write for Duplex<R, W> {
    fn write(&mut self, buf: &[u8]) -> Result<usize, Error> {
        self.w.write(buf)
    }

    fn flush(&mut self) -> Result<(), Error> {
        self.w.flush()
    }
}

impl<R, W: AsyncWrite> AsyncWrite for Duplex<R, W> {
    fn shutdown(&mut self) -> Poll<(), Error> {
        self.w.shutdown()
    }
}