File tree Expand file tree Collapse file tree 4 files changed +76
-0
lines changed Expand file tree Collapse file tree 4 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+ mod slice_mut;
2
+ mod slice_ref;
Original file line number Diff line number Diff line change
1
+ use crate :: { Io , Write } ;
2
+ use core:: mem;
3
+
4
+ impl Io for & mut [ u8 ] {
5
+ type Error = core:: convert:: Infallible ;
6
+ }
7
+
8
+ /// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
9
+ /// its data.
10
+ ///
11
+ /// Note that writing updates the slice to point to the yet unwritten part.
12
+ /// The slice will be empty when it has been completely overwritten.
13
+ ///
14
+ /// If the number of bytes to be written exceeds the size of the slice, write operations will
15
+ /// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of
16
+ /// kind `ErrorKind::WriteZero`.
17
+ impl Write for & mut [ u8 ] {
18
+ #[ inline]
19
+ fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > {
20
+ let amt = core:: cmp:: min ( buf. len ( ) , self . len ( ) ) ;
21
+ let ( a, b) = mem:: take ( self ) . split_at_mut ( amt) ;
22
+ a. copy_from_slice ( & buf[ ..amt] ) ;
23
+ * self = b;
24
+ Ok ( amt)
25
+ }
26
+
27
+ #[ inline]
28
+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
29
+ Ok ( ( ) )
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ use crate :: { BufRead , Io , Read } ;
2
+
3
+ impl Io for & [ u8 ] {
4
+ type Error = core:: convert:: Infallible ;
5
+ }
6
+
7
+ /// Read is implemented for `&[u8]` by copying from the slice.
8
+ ///
9
+ /// Note that reading updates the slice to point to the yet unread part.
10
+ /// The slice will be empty when EOF is reached.
11
+ impl Read for & [ u8 ] {
12
+ #[ inline]
13
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
14
+ let amt = core:: cmp:: min ( buf. len ( ) , self . len ( ) ) ;
15
+ let ( a, b) = self . split_at ( amt) ;
16
+
17
+ // First check if the amount of bytes we want to read is small:
18
+ // `copy_from_slice` will generally expand to a call to `memcpy`, and
19
+ // for a single byte the overhead is significant.
20
+ if amt == 1 {
21
+ buf[ 0 ] = a[ 0 ] ;
22
+ } else {
23
+ buf[ ..amt] . copy_from_slice ( a) ;
24
+ }
25
+
26
+ * self = b;
27
+ Ok ( amt)
28
+ }
29
+ }
30
+
31
+ impl BufRead for & [ u8 ] {
32
+ #[ inline]
33
+ fn fill_buf ( & mut self ) -> Result < & [ u8 ] , Self :: Error > {
34
+ Ok ( * self )
35
+ }
36
+
37
+ #[ inline]
38
+ fn consume ( & mut self , amt : usize ) {
39
+ * self = & self [ amt..] ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change 4
4
5
5
use core:: fmt;
6
6
7
+ mod impls;
8
+
7
9
/// Enumeration of possible methods to seek within an I/O object.
8
10
///
9
11
/// Semantics are the same as [`std::io::SeekFrom`], check its documentation for details.
You can’t perform that action at this time.
0 commit comments