1+ use core:: fmt;
12use std:: {
2- cmp:: min,
33 io:: { self , Read , Write } ,
44 net:: TcpStream ,
55 time:: Duration ,
66} ;
77
8+ pub trait ReadWrite : Read + Write { }
9+ impl < T : Read + Write > ReadWrite for T { }
10+
811#[ derive( Debug ) ]
912struct StringStream ( Vec < u8 > , usize ) ;
1013
@@ -27,24 +30,31 @@ impl Read for StringStream {
2730}
2831
2932impl StringStream {
30- fn peek ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
31- if buf. is_empty ( ) || self . 1 >= self . 0 . len ( ) {
32- return Ok ( 0 ) ;
33- }
34- let src = & self . 0 [ self . 1 ..] ;
35- let n = min ( buf. len ( ) , src. len ( ) ) ;
36- buf[ ..n] . copy_from_slice ( src) ;
37- Ok ( n)
33+ pub fn peek ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
34+ let min = usize:: min ( buf. len ( ) , self . 0 . len ( ) - self . 1 ) ;
35+ buf[ ..min] . copy_from_slice ( & self . 0 [ self . 1 ..self . 1 + min] ) ;
36+ Ok ( min)
3837 }
3938}
4039
41- #[ derive( Debug ) ]
4240enum HttpStreamInner {
4341 Tcp ( TcpStream ) ,
4442 String ( StringStream , Vec < u8 > ) ,
43+ Dyn ( Box < dyn ReadWrite > ) ,
4544 Dummy ,
4645}
4746
47+ impl fmt:: Debug for HttpStreamInner {
48+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
49+ match self {
50+ Self :: Tcp ( arg0) => f. debug_tuple ( "Tcp" ) . field ( arg0) . finish ( ) ,
51+ Self :: String ( arg0, arg1) => f. debug_tuple ( "String" ) . field ( arg0) . field ( arg1) . finish ( ) ,
52+ Self :: Dyn ( _) => write ! ( f, "Dyn" ) ,
53+ Self :: Dummy => write ! ( f, "Dummy" ) ,
54+ }
55+ }
56+ }
57+
4858/// Holds a "stream" for a [`request`]
4959/// This is an object where the http request can read and write to.
5060///
@@ -86,32 +96,27 @@ impl HttpStream {
8696 pub fn set_read_timeout ( & self , d : Option < Duration > ) -> io:: Result < ( ) > {
8797 match & self . inner {
8898 HttpStreamInner :: Tcp ( tcp_stream) => tcp_stream. set_read_timeout ( d) ,
89- HttpStreamInner :: Dummy | HttpStreamInner :: String ( ..) => Ok ( ( ) ) ,
99+ HttpStreamInner :: Dyn ( _) | HttpStreamInner :: Dummy | HttpStreamInner :: String ( ..) => {
100+ Ok ( ( ) )
101+ }
90102 }
91103 }
92- pub fn peek ( & self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
104+
105+ pub fn peek ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
93106 match & self . inner {
94- HttpStreamInner :: Tcp ( tcp_stream ) => tcp_stream . peek ( buf) ,
95- HttpStreamInner :: Dummy => Ok ( 0 ) ,
96- HttpStreamInner :: String ( read , _) => read . peek ( buf ) ,
107+ HttpStreamInner :: Tcp ( tcp ) => tcp . peek ( buf) ,
108+ HttpStreamInner :: String ( input , _ ) => input . peek ( buf ) ,
109+ HttpStreamInner :: Dyn ( _) | HttpStreamInner :: Dummy => Ok ( 0 ) ,
97110 }
98111 }
99- pub fn is_ready ( & self ) -> std:: io:: Result < bool > {
100- let mut buf = [ 0_u8 ; 1 ] ;
101- let n = match & self . inner {
102- HttpStreamInner :: Tcp ( tcp_stream) => tcp_stream. peek ( & mut buf) ?,
103- HttpStreamInner :: String ( string_stream, _) => string_stream. peek ( & mut buf) ?,
104- HttpStreamInner :: Dummy => 0 ,
105- } ;
106- Ok ( n > 0 )
107- }
108112}
109113
110114impl Read for HttpStream {
111115 fn read ( & mut self , buf : & mut [ u8 ] ) -> std:: io:: Result < usize > {
112116 match & mut self . inner {
113117 HttpStreamInner :: Tcp ( tcp_stream) => tcp_stream. read ( buf) ,
114118 HttpStreamInner :: Dummy => Ok ( 0 ) ,
119+ HttpStreamInner :: Dyn ( d) => d. read ( buf) ,
115120 HttpStreamInner :: String ( buf_reader, _) => buf_reader. read ( buf) ,
116121 }
117122 }
@@ -122,13 +127,15 @@ impl Write for HttpStream {
122127 match & mut self . inner {
123128 HttpStreamInner :: Tcp ( tcp_stream) => tcp_stream. write ( buf) ,
124129 HttpStreamInner :: Dummy => Ok ( 0 ) ,
130+ HttpStreamInner :: Dyn ( d) => d. write ( buf) ,
125131 HttpStreamInner :: String ( _, w) => w. write ( buf) ,
126132 }
127133 }
128134
129135 fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
130136 match & mut self . inner {
131137 HttpStreamInner :: Tcp ( tcp_stream) => tcp_stream. flush ( ) ,
138+ HttpStreamInner :: Dyn ( d) => d. flush ( ) ,
132139 HttpStreamInner :: Dummy | HttpStreamInner :: String ( ..) => Ok ( ( ) ) ,
133140 }
134141 }
@@ -165,4 +172,11 @@ impl HttpStream {
165172 inner : HttpStreamInner :: Dummy ,
166173 }
167174 }
175+
176+ #[ must_use]
177+ pub fn from_boxed_dyn ( d : Box < dyn ReadWrite > ) -> Self {
178+ Self {
179+ inner : HttpStreamInner :: Dyn ( d) ,
180+ }
181+ }
168182}
0 commit comments