1
+ use crate :: fmt;
1
2
use crate :: io:: prelude:: * ;
2
- use crate :: io:: { BorrowedBuf , Empty , Repeat , SeekFrom , Sink , empty, repeat, sink} ;
3
+ use crate :: io:: {
4
+ BorrowedBuf , Empty , ErrorKind , IoSlice , IoSliceMut , Repeat , SeekFrom , Sink , empty, repeat, sink,
5
+ } ;
3
6
use crate :: mem:: MaybeUninit ;
4
7
8
+ struct ErrorDisplay ;
9
+
10
+ impl fmt:: Display for ErrorDisplay {
11
+ fn fmt ( & self , _f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
12
+ Err ( fmt:: Error )
13
+ }
14
+ }
15
+
16
+ struct PanicDisplay ;
17
+
18
+ impl fmt:: Display for PanicDisplay {
19
+ fn fmt ( & self , _f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
20
+ panic ! ( )
21
+ }
22
+ }
23
+
24
+ #[ track_caller]
25
+ fn test_sinking < W : Write > ( mut w : W ) {
26
+ assert_eq ! ( w. write( & [ ] ) . unwrap( ) , 0 ) ;
27
+ assert_eq ! ( w. write( & [ 0 ] ) . unwrap( ) , 1 ) ;
28
+ assert_eq ! ( w. write( & [ 0 ; 1024 ] ) . unwrap( ) , 1024 ) ;
29
+ w. write_all ( & [ ] ) . unwrap ( ) ;
30
+ w. write_all ( & [ 0 ] ) . unwrap ( ) ;
31
+ w. write_all ( & [ 0 ; 1024 ] ) . unwrap ( ) ;
32
+ let mut bufs =
33
+ [ IoSlice :: new ( & [ ] ) , IoSlice :: new ( & [ 0 ] ) , IoSlice :: new ( & [ 0 ; 1024 ] ) , IoSlice :: new ( & [ ] ) ] ;
34
+ assert ! ( w. is_write_vectored( ) ) ;
35
+ assert_eq ! ( w. write_vectored( & [ ] ) . unwrap( ) , 0 ) ;
36
+ assert_eq ! ( w. write_vectored( & bufs) . unwrap( ) , 1025 ) ;
37
+ w. write_all_vectored ( & mut [ ] ) . unwrap ( ) ;
38
+ w. write_all_vectored ( & mut bufs) . unwrap ( ) ;
39
+ assert ! ( w. flush( ) . is_ok( ) ) ;
40
+ assert_eq ! ( w. by_ref( ) . write( & [ 0 ; 1024 ] ) . unwrap( ) , 1024 ) ;
41
+ // Ignores fmt arguments
42
+ w. write_fmt ( format_args ! ( "{}" , ErrorDisplay ) ) . unwrap ( ) ;
43
+ w. write_fmt ( format_args ! ( "{}" , PanicDisplay ) ) . unwrap ( ) ;
44
+ }
45
+
5
46
#[ test]
6
47
fn sink_sinks ( ) {
7
- let mut s = sink ( ) ;
8
- assert_eq ! ( s. write( & [ ] ) . unwrap( ) , 0 ) ;
9
- assert_eq ! ( s. write( & [ 0 ] ) . unwrap( ) , 1 ) ;
10
- assert_eq ! ( s. write( & [ 0 ; 1024 ] ) . unwrap( ) , 1024 ) ;
11
- assert_eq ! ( s. by_ref( ) . write( & [ 0 ; 1024 ] ) . unwrap( ) , 1024 ) ;
48
+ test_sinking ( sink ( ) ) ;
12
49
}
13
50
14
51
#[ test]
@@ -19,6 +56,21 @@ fn empty_reads() {
19
56
assert_eq ! ( e. read( & mut [ 0 ; 1024 ] ) . unwrap( ) , 0 ) ;
20
57
assert_eq ! ( Read :: by_ref( & mut e) . read( & mut [ 0 ; 1024 ] ) . unwrap( ) , 0 ) ;
21
58
59
+ e. read_exact ( & mut [ ] ) . unwrap ( ) ;
60
+ assert_eq ! ( e. read_exact( & mut [ 0 ] ) . unwrap_err( ) . kind( ) , ErrorKind :: UnexpectedEof ) ;
61
+ assert_eq ! ( e. read_exact( & mut [ 0 ; 1024 ] ) . unwrap_err( ) . kind( ) , ErrorKind :: UnexpectedEof ) ;
62
+
63
+ assert ! ( e. is_read_vectored( ) ) ;
64
+ assert_eq ! ( e. read_vectored( & mut [ ] ) . unwrap( ) , 0 ) ;
65
+ let ( mut buf1, mut buf1024) = ( [ 0 ] , [ 0 ; 1024 ] ) ;
66
+ let bufs = & mut [
67
+ IoSliceMut :: new ( & mut [ ] ) ,
68
+ IoSliceMut :: new ( & mut buf1) ,
69
+ IoSliceMut :: new ( & mut buf1024) ,
70
+ IoSliceMut :: new ( & mut [ ] ) ,
71
+ ] ;
72
+ assert_eq ! ( e. read_vectored( bufs) . unwrap( ) , 0 ) ;
73
+
22
74
let buf: & mut [ MaybeUninit < _ > ] = & mut [ ] ;
23
75
let mut buf: BorrowedBuf < ' _ > = buf. into ( ) ;
24
76
e. read_buf ( buf. unfilled ( ) ) . unwrap ( ) ;
@@ -42,6 +94,47 @@ fn empty_reads() {
42
94
Read :: by_ref ( & mut e) . read_buf ( buf. unfilled ( ) ) . unwrap ( ) ;
43
95
assert_eq ! ( buf. len( ) , 0 ) ;
44
96
assert_eq ! ( buf. init_len( ) , 0 ) ;
97
+
98
+ let buf: & mut [ MaybeUninit < _ > ] = & mut [ ] ;
99
+ let mut buf: BorrowedBuf < ' _ > = buf. into ( ) ;
100
+ e. read_buf_exact ( buf. unfilled ( ) ) . unwrap ( ) ;
101
+ assert_eq ! ( buf. len( ) , 0 ) ;
102
+ assert_eq ! ( buf. init_len( ) , 0 ) ;
103
+
104
+ let buf: & mut [ _ ] = & mut [ MaybeUninit :: uninit ( ) ] ;
105
+ let mut buf: BorrowedBuf < ' _ > = buf. into ( ) ;
106
+ assert_eq ! ( e. read_buf_exact( buf. unfilled( ) ) . unwrap_err( ) . kind( ) , ErrorKind :: UnexpectedEof ) ;
107
+ assert_eq ! ( buf. len( ) , 0 ) ;
108
+ assert_eq ! ( buf. init_len( ) , 0 ) ;
109
+
110
+ let buf: & mut [ _ ] = & mut [ MaybeUninit :: uninit ( ) ; 1024 ] ;
111
+ let mut buf: BorrowedBuf < ' _ > = buf. into ( ) ;
112
+ assert_eq ! ( e. read_buf_exact( buf. unfilled( ) ) . unwrap_err( ) . kind( ) , ErrorKind :: UnexpectedEof ) ;
113
+ assert_eq ! ( buf. len( ) , 0 ) ;
114
+ assert_eq ! ( buf. init_len( ) , 0 ) ;
115
+
116
+ let buf: & mut [ _ ] = & mut [ MaybeUninit :: uninit ( ) ; 1024 ] ;
117
+ let mut buf: BorrowedBuf < ' _ > = buf. into ( ) ;
118
+ assert_eq ! (
119
+ Read :: by_ref( & mut e) . read_buf_exact( buf. unfilled( ) ) . unwrap_err( ) . kind( ) ,
120
+ ErrorKind :: UnexpectedEof ,
121
+ ) ;
122
+ assert_eq ! ( buf. len( ) , 0 ) ;
123
+ assert_eq ! ( buf. init_len( ) , 0 ) ;
124
+
125
+ let mut buf = Vec :: new ( ) ;
126
+ assert_eq ! ( e. read_to_end( & mut buf) . unwrap( ) , 0 ) ;
127
+ assert_eq ! ( buf, vec![ ] ) ;
128
+ let mut buf = vec ! [ 1 , 2 , 3 ] ;
129
+ assert_eq ! ( e. read_to_end( & mut buf) . unwrap( ) , 0 ) ;
130
+ assert_eq ! ( buf, vec![ 1 , 2 , 3 ] ) ;
131
+
132
+ let mut buf = String :: new ( ) ;
133
+ assert_eq ! ( e. read_to_string( & mut buf) . unwrap( ) , 0 ) ;
134
+ assert_eq ! ( buf, "" ) ;
135
+ let mut buf = "hello" . to_owned ( ) ;
136
+ assert_eq ! ( e. read_to_string( & mut buf) . unwrap( ) , 0 ) ;
137
+ assert_eq ! ( buf, "hello" ) ;
45
138
}
46
139
47
140
#[ test]
@@ -66,11 +159,7 @@ fn empty_seeks() {
66
159
67
160
#[ test]
68
161
fn empty_sinks ( ) {
69
- let mut e = empty ( ) ;
70
- assert_eq ! ( e. write( & [ ] ) . unwrap( ) , 0 ) ;
71
- assert_eq ! ( e. write( & [ 0 ] ) . unwrap( ) , 1 ) ;
72
- assert_eq ! ( e. write( & [ 0 ; 1024 ] ) . unwrap( ) , 1024 ) ;
73
- assert_eq ! ( Write :: by_ref( & mut e) . write( & [ 0 ; 1024 ] ) . unwrap( ) , 1024 ) ;
162
+ test_sinking ( empty ( ) ) ;
74
163
}
75
164
76
165
#[ test]
0 commit comments