11#[ cfg( all( feature = "std" , feature = "use-core2" ) ) ]
22compile_error ! ( "feature \" std\" and \" use-core2\" cannot be enabled together." ) ;
33
4-
5- pub trait Read {
4+ pub trait Read {
65 type Error ;
76 fn read ( & mut self , buf : & mut [ u8 ] ) -> :: core:: result:: Result < usize , Self :: Error > ;
7+ fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> :: core:: result:: Result < ( ) , Self :: Error > ;
88}
99
1010pub trait Write {
1111 type Error ;
1212 fn write ( & mut self , buf : & [ u8 ] ) -> :: core:: result:: Result < usize , Self :: Error > ;
13+ fn write_all ( & mut self , buf : & [ u8 ] ) -> :: core:: result:: Result < ( ) , Self :: Error > ;
1314 fn flush ( & mut self ) -> :: core:: result:: Result < ( ) , Self :: Error > ;
14- fn write_all ( & mut self , mut buf : & [ u8 ] ) -> :: core:: result:: Result < ( ) , Self :: Error > {
15- while !buf. is_empty ( ) {
16- match self . write ( buf) {
17- /*Ok(0) => {
18- return Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"));
19- }*/
20- Ok ( n) => buf = & buf[ n..] ,
21- //Err(ref e) if e.kind() == ErrorKind::Interrupted => {}
22- Err ( e) => return Err ( e) ,
23- }
24- }
25- Ok ( ( ) )
26- }
15+
2716}
2817
2918#[ cfg( feature = "std" ) ]
@@ -36,6 +25,10 @@ mod std_impl {
3625 fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
3726 Ok ( <Self as :: std:: io:: Read >:: read ( self , buf) ?)
3827 }
28+
29+ fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
30+ Ok ( <Self as :: std:: io:: Read >:: read_exact ( self , buf) ?)
31+ }
3932 }
4033
4134 impl < W : :: std:: io:: Write > Write for W {
@@ -45,6 +38,10 @@ mod std_impl {
4538 <Self as :: std:: io:: Write >:: write ( self , buf)
4639 }
4740
41+ fn write_all ( & mut self , buf : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
42+ <Self as :: std:: io:: Write >:: write_all ( self , buf)
43+ }
44+
4845 fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
4946 <Self as :: std:: io:: Write >:: flush ( self )
5047 }
@@ -61,6 +58,10 @@ mod core2_impl {
6158 fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
6259 Ok ( <Self as core2:: io:: Read >:: read ( self , buf) ?)
6360 }
61+
62+ fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
63+ Ok ( <Self as core2:: io:: Read >:: read_exact ( self , buf) ?)
64+ }
6465 }
6566
6667 impl < W : core2:: io:: Write > Write for W {
@@ -70,12 +71,84 @@ mod core2_impl {
7071 Ok ( <Self as core2:: io:: Write >:: write ( self , buf) ?)
7172 }
7273
74+ fn write_all ( & mut self , buf : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
75+ <Self as core2:: io:: Write >:: write_all ( self , buf)
76+ }
77+
7378 fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
7479 Ok ( <Self as core2:: io:: Write >:: flush ( self ) ?)
7580 }
7681 }
7782}
7883
84+ #[ cfg( all( not( feature = "use-core2" ) , not( feature = "std" ) ) ) ]
85+ mod default_impl {
86+ use super :: { Read , Write } ;
87+
88+ pub enum DefaultError {
89+ UnexpectedEof ,
90+ }
91+
92+ impl Read for & [ u8 ] {
93+ type Error = DefaultError ;
94+
95+ fn read ( & mut self , buf : & mut [ u8 ] ) -> :: core:: result:: Result < usize , Self :: Error > {
96+ let amt = :: core:: cmp:: min ( buf. len ( ) , self . len ( ) ) ;
97+ let ( a, b) = self . split_at ( amt) ;
98+
99+ // First check if the amount of bytes we want to read is small:
100+ // `copy_from_slice` will generally expand to a call to `memcpy`, and
101+ // for a single byte the overhead is significant.
102+ if amt == 1 {
103+ buf[ 0 ] = a[ 0 ] ;
104+ } else {
105+ buf[ ..amt] . copy_from_slice ( a) ;
106+ }
107+
108+ * self = b;
109+ Ok ( amt)
110+ }
111+
112+ fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> :: core:: result:: Result < ( ) , Self :: Error > {
113+ if buf. len ( ) > self . len ( ) {
114+ return Err ( Self :: Error :: UnexpectedEof ) ;
115+ }
116+ let ( a, b) = self . split_at ( buf. len ( ) ) ;
117+
118+ // First check if the amount of bytes we want to read is small:
119+ // `copy_from_slice` will generally expand to a call to `memcpy`, and
120+ // for a single byte the overhead is significant.
121+ if buf. len ( ) == 1 {
122+ buf[ 0 ] = a[ 0 ] ;
123+ } else {
124+ buf. copy_from_slice ( a) ;
125+ }
126+
127+ * self = b;
128+ Ok ( ( ) )
129+ }
130+ }
131+
132+ impl Write for :: alloc:: vec:: Vec < u8 > {
133+ type Error = DefaultError ;
134+
135+ fn write ( & mut self , buf : & [ u8 ] ) -> :: core:: result:: Result < usize , Self :: Error > {
136+ self . extend_from_slice ( buf) ;
137+ Ok ( buf. len ( ) )
138+ }
139+
140+ fn write_all ( & mut self , buf : & [ u8 ] ) -> :: core:: result:: Result < ( ) , Self :: Error > {
141+ self . extend_from_slice ( buf) ;
142+ Ok ( ( ) )
143+ }
144+
145+ fn flush ( & mut self ) -> :: core:: result:: Result < ( ) , Self :: Error > {
146+ Ok ( ( ) )
147+ }
148+ }
149+ }
150+
151+
79152#[ cfg( test) ]
80153mod tests {
81154
0 commit comments