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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::io::{IoSlice, Read, Write, Result};
use crate::session::Session;
pub struct Stream<'a, S: 'a + Session + ?Sized, T: 'a + Read + Write + ?Sized> {
pub sess: &'a mut S,
pub sock: &'a mut T,
}
impl<'a, S, T> Stream<'a, S, T> where S: 'a + Session, T: 'a + Read + Write {
pub fn new(sess: &'a mut S, sock: &'a mut T) -> Stream<'a, S, T> {
Stream { sess, sock }
}
fn complete_prior_io(&mut self) -> Result<()> {
if self.sess.is_handshaking() {
self.sess.complete_io(self.sock)?;
}
if self.sess.wants_write() {
self.sess.complete_io(self.sock)?;
}
Ok(())
}
}
impl<'a, S, T> Read for Stream<'a, S, T> where S: 'a + Session, T: 'a + Read + Write {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.complete_prior_io()?;
while
self.sess.wants_read() &&
self.sess.complete_io(self.sock)?.0 != 0
{ }
self.sess.read(buf)
}
}
impl<'a, S, T> Write for Stream<'a, S, T> where S: 'a + Session, T: 'a + Read + Write {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.complete_prior_io()?;
let len = self.sess.write(buf)?;
let _ = self.sess.complete_io(self.sock);
Ok(len)
}
fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize> {
self.complete_prior_io()?;
let len = self.sess.write_vectored(bufs)?;
let _ = self.sess.complete_io(self.sock);
Ok(len)
}
fn flush(&mut self) -> Result<()> {
self.complete_prior_io()?;
self.sess.flush()?;
if self.sess.wants_write() {
self.sess.complete_io(self.sock)?;
}
Ok(())
}
}
pub struct StreamOwned<S: Session + Sized, T: Read + Write + Sized> {
pub sess: S,
pub sock: T,
}
impl<S, T> StreamOwned<S, T> where S: Session, T: Read + Write {
pub fn new(sess: S, sock: T) -> StreamOwned<S, T> {
StreamOwned { sess, sock }
}
pub fn get_ref(&self) -> &T {
&self.sock
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.sock
}
}
impl<'a, S, T> StreamOwned<S, T> where S: Session, T: Read + Write {
fn as_stream(&'a mut self) -> Stream<'a, S, T> {
Stream { sess: &mut self.sess, sock: &mut self.sock }
}
}
impl<S, T> Read for StreamOwned<S, T> where S: Session, T: Read + Write {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
self.as_stream().read(buf)
}
}
impl<S, T> Write for StreamOwned<S, T> where S: Session, T: Read + Write {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
self.as_stream().write(buf)
}
fn flush(&mut self) -> Result<()> {
self.as_stream().flush()
}
}
#[cfg(test)]
mod tests {
use super::{Stream, StreamOwned};
use crate::session::Session;
use crate::client::ClientSession;
use crate::server::ServerSession;
use std::net::TcpStream;
#[test]
fn stream_can_be_created_for_session_and_tcpstream() {
type _Test<'a> = Stream<'a, dyn Session, TcpStream>;
}
#[test]
fn streamowned_can_be_created_for_client_and_tcpstream() {
type _Test = StreamOwned<ClientSession, TcpStream>;
}
#[test]
fn streamowned_can_be_created_for_server_and_tcpstream() {
type _Test = StreamOwned<ServerSession, TcpStream>;
}
}