@@ -24,77 +24,117 @@ use vec::Vec;
24
24
25
25
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
26
26
impl < ' a , R : Read + ?Sized > Read for & ' a mut R {
27
+ #[ inline]
27
28
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
28
29
( * * self ) . read ( buf)
29
30
}
31
+
32
+ #[ inline]
30
33
fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
31
34
( * * self ) . read_to_end ( buf)
32
35
}
36
+
37
+ #[ inline]
33
38
fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
34
39
( * * self ) . read_to_string ( buf)
35
40
}
36
41
}
37
42
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
38
43
impl < ' a , W : Write + ?Sized > Write for & ' a mut W {
44
+ #[ inline]
39
45
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > { ( * * self ) . write ( buf) }
46
+
47
+ #[ inline]
40
48
fn flush ( & mut self ) -> io:: Result < ( ) > { ( * * self ) . flush ( ) }
49
+
50
+ #[ inline]
41
51
fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
42
52
( * * self ) . write_all ( buf)
43
53
}
54
+
55
+ #[ inline]
44
56
fn write_fmt ( & mut self , fmt : fmt:: Arguments ) -> io:: Result < ( ) > {
45
57
( * * self ) . write_fmt ( fmt)
46
58
}
47
59
}
48
60
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
49
61
impl < ' a , S : Seek + ?Sized > Seek for & ' a mut S {
62
+ #[ inline]
50
63
fn seek ( & mut self , pos : SeekFrom ) -> io:: Result < u64 > { ( * * self ) . seek ( pos) }
51
64
}
52
65
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
53
66
impl < ' a , B : BufRead + ?Sized > BufRead for & ' a mut B {
67
+ #[ inline]
54
68
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > { ( * * self ) . fill_buf ( ) }
69
+
70
+ #[ inline]
55
71
fn consume ( & mut self , amt : usize ) { ( * * self ) . consume ( amt) }
72
+
73
+ #[ inline]
56
74
fn read_until ( & mut self , byte : u8 , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
57
75
( * * self ) . read_until ( byte, buf)
58
76
}
77
+
78
+ #[ inline]
59
79
fn read_line ( & mut self , buf : & mut String ) -> io:: Result < usize > {
60
80
( * * self ) . read_line ( buf)
61
81
}
62
82
}
63
83
64
84
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
65
85
impl < R : Read + ?Sized > Read for Box < R > {
86
+ #[ inline]
66
87
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
67
88
( * * self ) . read ( buf)
68
89
}
90
+
91
+ #[ inline]
69
92
fn read_to_end ( & mut self , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
70
93
( * * self ) . read_to_end ( buf)
71
94
}
95
+
96
+ #[ inline]
72
97
fn read_to_string ( & mut self , buf : & mut String ) -> io:: Result < usize > {
73
98
( * * self ) . read_to_string ( buf)
74
99
}
75
100
}
76
101
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
77
102
impl < W : Write + ?Sized > Write for Box < W > {
103
+ #[ inline]
78
104
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > { ( * * self ) . write ( buf) }
105
+
106
+ #[ inline]
79
107
fn flush ( & mut self ) -> io:: Result < ( ) > { ( * * self ) . flush ( ) }
108
+
109
+ #[ inline]
80
110
fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
81
111
( * * self ) . write_all ( buf)
82
112
}
113
+
114
+ #[ inline]
83
115
fn write_fmt ( & mut self , fmt : fmt:: Arguments ) -> io:: Result < ( ) > {
84
116
( * * self ) . write_fmt ( fmt)
85
117
}
86
118
}
87
119
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
88
120
impl < S : Seek + ?Sized > Seek for Box < S > {
121
+ #[ inline]
89
122
fn seek ( & mut self , pos : SeekFrom ) -> io:: Result < u64 > { ( * * self ) . seek ( pos) }
90
123
}
91
124
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
92
125
impl < B : BufRead + ?Sized > BufRead for Box < B > {
126
+ #[ inline]
93
127
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > { ( * * self ) . fill_buf ( ) }
128
+
129
+ #[ inline]
94
130
fn consume ( & mut self , amt : usize ) { ( * * self ) . consume ( amt) }
131
+
132
+ #[ inline]
95
133
fn read_until ( & mut self , byte : u8 , buf : & mut Vec < u8 > ) -> io:: Result < usize > {
96
134
( * * self ) . read_until ( byte, buf)
97
135
}
136
+
137
+ #[ inline]
98
138
fn read_line ( & mut self , buf : & mut String ) -> io:: Result < usize > {
99
139
( * * self ) . read_line ( buf)
100
140
}
@@ -105,6 +145,7 @@ impl<B: BufRead + ?Sized> BufRead for Box<B> {
105
145
106
146
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
107
147
impl < ' a > Read for & ' a [ u8 ] {
148
+ #[ inline]
108
149
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
109
150
let amt = cmp:: min ( buf. len ( ) , self . len ( ) ) ;
110
151
let ( a, b) = self . split_at ( amt) ;
@@ -116,12 +157,16 @@ impl<'a> Read for &'a [u8] {
116
157
117
158
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
118
159
impl < ' a > BufRead for & ' a [ u8 ] {
160
+ #[ inline]
119
161
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > { Ok ( * self ) }
162
+
163
+ #[ inline]
120
164
fn consume ( & mut self , amt : usize ) { * self = & self [ amt..] ; }
121
165
}
122
166
123
167
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
124
168
impl < ' a > Write for & ' a mut [ u8 ] {
169
+ #[ inline]
125
170
fn write ( & mut self , data : & [ u8 ] ) -> io:: Result < usize > {
126
171
let amt = cmp:: min ( data. len ( ) , self . len ( ) ) ;
127
172
let ( a, b) = mem:: replace ( self , & mut [ ] ) . split_at_mut ( amt) ;
@@ -130,6 +175,7 @@ impl<'a> Write for &'a mut [u8] {
130
175
Ok ( amt)
131
176
}
132
177
178
+ #[ inline]
133
179
fn write_all ( & mut self , data : & [ u8 ] ) -> io:: Result < ( ) > {
134
180
if try!( self . write ( data) ) == data. len ( ) {
135
181
Ok ( ( ) )
@@ -138,20 +184,87 @@ impl<'a> Write for &'a mut [u8] {
138
184
}
139
185
}
140
186
187
+ #[ inline]
141
188
fn flush ( & mut self ) -> io:: Result < ( ) > { Ok ( ( ) ) }
142
189
}
143
190
144
191
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
145
192
impl Write for Vec < u8 > {
193
+ #[ inline]
146
194
fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
147
195
self . push_all ( buf) ;
148
196
Ok ( buf. len ( ) )
149
197
}
150
198
199
+ #[ inline]
151
200
fn write_all ( & mut self , buf : & [ u8 ] ) -> io:: Result < ( ) > {
152
201
self . push_all ( buf) ;
153
202
Ok ( ( ) )
154
203
}
155
204
205
+ #[ inline]
156
206
fn flush ( & mut self ) -> io:: Result < ( ) > { Ok ( ( ) ) }
157
207
}
208
+
209
+ #[ cfg( test) ]
210
+ mod tests {
211
+ use io:: prelude:: * ;
212
+ use vec:: Vec ;
213
+ use test;
214
+
215
+ #[ bench]
216
+ fn bench_read_slice ( b : & mut test:: Bencher ) {
217
+ let buf = [ 5 ; 1024 ] ;
218
+ let mut dst = [ 0 ; 128 ] ;
219
+
220
+ b. iter ( || {
221
+ let mut rd = & buf[ ..] ;
222
+ for _ in ( 0 .. 8 ) {
223
+ let _ = rd. read ( & mut dst) ;
224
+ test:: black_box ( & dst) ;
225
+ }
226
+ } )
227
+ }
228
+
229
+ #[ bench]
230
+ fn bench_write_slice ( b : & mut test:: Bencher ) {
231
+ let mut buf = [ 0 ; 1024 ] ;
232
+ let src = [ 5 ; 128 ] ;
233
+
234
+ b. iter ( || {
235
+ let mut wr = & mut buf[ ..] ;
236
+ for _ in ( 0 .. 8 ) {
237
+ let _ = wr. write_all ( & src) ;
238
+ test:: black_box ( & wr) ;
239
+ }
240
+ } )
241
+ }
242
+
243
+ #[ bench]
244
+ fn bench_read_vec ( b : & mut test:: Bencher ) {
245
+ let buf = vec ! [ 5 ; 1024 ] ;
246
+ let mut dst = [ 0 ; 128 ] ;
247
+
248
+ b. iter ( || {
249
+ let mut rd = & buf[ ..] ;
250
+ for _ in ( 0 .. 8 ) {
251
+ let _ = rd. read ( & mut dst) ;
252
+ test:: black_box ( & dst) ;
253
+ }
254
+ } )
255
+ }
256
+
257
+ #[ bench]
258
+ fn bench_write_vec ( b : & mut test:: Bencher ) {
259
+ let mut buf = Vec :: with_capacity ( 1024 ) ;
260
+ let src = [ 5 ; 128 ] ;
261
+
262
+ b. iter ( || {
263
+ let mut wr = & mut buf[ ..] ;
264
+ for _ in ( 0 .. 8 ) {
265
+ let _ = wr. write_all ( & src) ;
266
+ test:: black_box ( & wr) ;
267
+ }
268
+ } )
269
+ }
270
+ }
0 commit comments