26
26
pub trait Read {
27
27
/// The error type returned in Result
28
28
type Error ;
29
+ /// The type to implement limited reads
30
+ type Take ;
29
31
/// see [std::io::Read::read]
30
32
fn read ( & mut self , buf : & mut [ u8 ] ) -> :: core:: result:: Result < usize , Self :: Error > ;
31
33
/// see [std::io::Read::read_exact]
32
34
fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> :: core:: result:: Result < ( ) , Self :: Error > ;
35
+ /// see [std::io::Read::take]
36
+ fn take ( self , limit : u64 ) -> Self :: Take ;
33
37
}
34
38
35
39
/// The Write trait allows to write bytes in the object implementing it.
@@ -50,6 +54,7 @@ mod std_impl {
50
54
51
55
impl < R : :: std:: io:: Read > Read for R {
52
56
type Error = :: std:: io:: Error ;
57
+ type Take = :: std:: io:: Take < R > ;
53
58
54
59
fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
55
60
<Self as :: std:: io:: Read >:: read ( self , buf)
@@ -58,6 +63,11 @@ mod std_impl {
58
63
fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
59
64
<Self as :: std:: io:: Read >:: read_exact ( self , buf)
60
65
}
66
+
67
+ fn take ( self , limit : u64 ) -> Self :: Take {
68
+ <Self as :: std:: io:: Read >:: take ( self , limit)
69
+ }
70
+
61
71
}
62
72
63
73
impl < W : :: std:: io:: Write > Write for W {
@@ -83,6 +93,7 @@ mod core2_impl {
83
93
84
94
impl < R : core2:: io:: Read > Read for R {
85
95
type Error = core2:: io:: Error ;
96
+ type Take = core2:: io:: Take < R > ;
86
97
87
98
fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
88
99
<Self as core2:: io:: Read >:: read ( self , buf)
@@ -91,6 +102,10 @@ mod core2_impl {
91
102
fn read_exact ( & mut self , buf : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
92
103
<Self as core2:: io:: Read >:: read_exact ( self , buf)
93
104
}
105
+
106
+ fn take ( self , limit : u64 ) -> Self :: Take {
107
+ <Self as core2:: io:: Read >:: take ( self , limit)
108
+ }
94
109
}
95
110
96
111
impl < W : core2:: io:: Write > Write for W {
@@ -114,12 +129,14 @@ mod core2_impl {
114
129
mod default_impl {
115
130
use super :: { Read , Write } ;
116
131
132
+ #[ derive( Debug ) ]
117
133
pub enum DefaultError {
118
134
UnexpectedEof ,
119
135
}
120
136
121
- impl Read for & [ u8 ] {
137
+ impl < ' a > Read for & ' a [ u8 ] {
122
138
type Error = DefaultError ;
139
+ type Take = & ' a [ u8 ] ;
123
140
124
141
fn read ( & mut self , buf : & mut [ u8 ] ) -> :: core:: result:: Result < usize , Self :: Error > {
125
142
let amt = :: core:: cmp:: min ( buf. len ( ) , self . len ( ) ) ;
@@ -156,6 +173,10 @@ mod default_impl {
156
173
* self = b;
157
174
Ok ( ( ) )
158
175
}
176
+
177
+ fn take ( self , limit : u64 ) -> Self :: Take {
178
+ & self [ ..limit as usize ]
179
+ }
159
180
}
160
181
161
182
impl Write for :: alloc:: vec:: Vec < u8 > {
0 commit comments