11//! Provides the default stream type for WebSocket connections.
22
33use std:: ops:: Deref ;
4+ use std:: fmt:: Arguments ;
45use std:: io:: { self , Read , Write } ;
56pub use std:: net:: TcpStream ;
67pub use std:: net:: Shutdown ;
78#[ cfg( feature="ssl" ) ]
89pub use openssl:: ssl:: { SslStream , SslContext } ;
910
10- pub trait Splittable {
11- type Reader : Read ;
12- type Writer : Write ;
13-
14- fn split ( self ) -> io:: Result < ( Self :: Reader , Self :: Writer ) > ;
15- }
16-
1711/// Represents a stream that can be read from, and written to.
1812/// This is an abstraction around readable and writable things to be able
1913/// to speak websockets over ssl, tcp, unix sockets, etc.
20- pub trait Stream {
14+ pub trait Stream : Read + Write { }
15+
16+ impl < S > Stream for S where S : Read + Write { }
17+
18+ pub trait NetworkStream : Read + Write + AsTcpStream { }
19+
20+ impl < S > NetworkStream for S where S : Read + Write + AsTcpStream { }
21+
22+ pub trait Splittable {
2123 type Reader : Read ;
2224 type Writer : Write ;
2325
24- /// Get a mutable borrow to the reading component of this stream
25- fn reader ( & mut self ) -> & mut Self :: Reader ;
26-
27- /// Get a mutable borrow to the writing component of this stream
28- fn writer ( & mut self ) -> & mut Self :: Writer ;
26+ fn split ( self ) -> io:: Result < ( Self :: Reader , Self :: Writer ) > ;
2927}
3028
31- pub struct ReadWritePair < R , W > ( pub R , pub W )
32- where R : Read ,
33- W : Write ;
34-
3529impl < R , W > Splittable for ReadWritePair < R , W >
3630 where R : Read ,
3731 W : Write
@@ -44,70 +38,6 @@ impl<R, W> Splittable for ReadWritePair<R, W>
4438 }
4539}
4640
47- impl < R , W > Stream for ReadWritePair < R , W >
48- where R : Read ,
49- W : Write
50- {
51- type Reader = R ;
52- type Writer = W ;
53-
54- #[ inline]
55- fn reader ( & mut self ) -> & mut R {
56- & mut self . 0
57- }
58-
59- #[ inline]
60- fn writer ( & mut self ) -> & mut W {
61- & mut self . 1
62- }
63- }
64-
65- pub trait ReadWrite : Read + Write { }
66- impl < S > ReadWrite for S where S : Read + Write { }
67-
68- pub struct BoxedStream ( pub Box < ReadWrite > ) ;
69-
70- impl Stream for BoxedStream {
71- type Reader = Box < ReadWrite > ;
72- type Writer = Box < ReadWrite > ;
73-
74- #[ inline]
75- fn reader ( & mut self ) -> & mut Self :: Reader {
76- & mut self . 0
77- }
78-
79- #[ inline]
80- fn writer ( & mut self ) -> & mut Self :: Writer {
81- & mut self . 0
82- }
83- }
84-
85- pub trait NetworkStream : Read + Write + AsTcpStream { }
86- impl < S > NetworkStream for S where S : Read + Write + AsTcpStream { }
87-
88- pub struct BoxedNetworkStream ( pub Box < NetworkStream > ) ;
89-
90- impl AsTcpStream for BoxedNetworkStream {
91- fn as_tcp ( & self ) -> & TcpStream {
92- self . 0 . deref ( ) . as_tcp ( )
93- }
94- }
95-
96- impl Stream for BoxedNetworkStream {
97- type Reader = Box < NetworkStream > ;
98- type Writer = Box < NetworkStream > ;
99-
100- #[ inline]
101- fn reader ( & mut self ) -> & mut Self :: Reader {
102- & mut self . 0
103- }
104-
105- #[ inline]
106- fn writer ( & mut self ) -> & mut Self :: Writer {
107- & mut self . 0
108- }
109- }
110-
11141impl Splittable for TcpStream {
11242 type Reader = TcpStream ;
11343 type Writer = TcpStream ;
@@ -117,23 +47,6 @@ impl Splittable for TcpStream {
11747 }
11848}
11949
120- impl < S > Stream for S
121- where S : Read + Write
122- {
123- type Reader = Self ;
124- type Writer = Self ;
125-
126- #[ inline]
127- fn reader ( & mut self ) -> & mut S {
128- self
129- }
130-
131- #[ inline]
132- fn writer ( & mut self ) -> & mut S {
133- self
134- }
135- }
136-
13750pub trait AsTcpStream {
13851 fn as_tcp ( & self ) -> & TcpStream ;
13952}
@@ -158,3 +71,51 @@ impl<T> AsTcpStream for Box<T>
15871 self . deref ( ) . as_tcp ( )
15972 }
16073}
74+
75+ pub struct ReadWritePair < R , W > ( pub R , pub W )
76+ where R : Read ,
77+ W : Write ;
78+
79+ impl < R , W > Read for ReadWritePair < R , W >
80+ where R : Read ,
81+ W : Write
82+ {
83+ #[ inline( always) ]
84+ fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
85+ self . 0 . read ( buf)
86+ }
87+ #[ inline( always) ]
88+ fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
89+ self . 0 . read_to_end ( buf)
90+ }
91+ #[ inline( always) ]
92+ fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
93+ self . 0 . read_to_string ( buf)
94+ }
95+ #[ inline( always) ]
96+ fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < ( ) > {
97+ self . 0 . read_exact ( buf)
98+ }
99+ }
100+
101+ impl < R , W > Write for ReadWritePair < R , W >
102+ where R : Read ,
103+ W : Write
104+ {
105+ #[ inline( always) ]
106+ fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
107+ self . 1 . write ( buf)
108+ }
109+ #[ inline( always) ]
110+ fn flush ( & mut self ) -> io:: Result < ( ) > {
111+ self . 1 . flush ( )
112+ }
113+ #[ inline( always) ]
114+ fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
115+ self . 1 . write_all ( buf)
116+ }
117+ #[ inline( always) ]
118+ fn write_fmt ( & mut self , fmt : Arguments ) -> io:: Result < ( ) > {
119+ self . 1 . write_fmt ( fmt)
120+ }
121+ }
0 commit comments