@@ -12,7 +12,10 @@ use std::task::{Context, Poll};
12
12
const INITIAL_CAPACITY : usize = 8 * 1024 ;
13
13
14
14
/// Encode and decode u32 values.
15
- struct U32Codec ;
15
+ #[ derive( Default ) ]
16
+ struct U32Codec {
17
+ read_bytes : usize ,
18
+ }
16
19
17
20
impl Decoder for U32Codec {
18
21
type Item = u32 ;
@@ -24,6 +27,7 @@ impl Decoder for U32Codec {
24
27
}
25
28
26
29
let n = buf. split_to ( 4 ) . get_u32 ( ) ;
30
+ self . read_bytes += 4 ;
27
31
Ok ( Some ( n) )
28
32
}
29
33
}
@@ -39,6 +43,38 @@ impl Encoder<u32> for U32Codec {
39
43
}
40
44
}
41
45
46
+ /// Encode and decode u64 values.
47
+ #[ derive( Default ) ]
48
+ struct U64Codec {
49
+ read_bytes : usize ,
50
+ }
51
+
52
+ impl Decoder for U64Codec {
53
+ type Item = u64 ;
54
+ type Error = io:: Error ;
55
+
56
+ fn decode ( & mut self , buf : & mut BytesMut ) -> io:: Result < Option < u64 > > {
57
+ if buf. len ( ) < 8 {
58
+ return Ok ( None ) ;
59
+ }
60
+
61
+ let n = buf. split_to ( 8 ) . get_u64 ( ) ;
62
+ self . read_bytes += 8 ;
63
+ Ok ( Some ( n) )
64
+ }
65
+ }
66
+
67
+ impl Encoder < u64 > for U64Codec {
68
+ type Error = io:: Error ;
69
+
70
+ fn encode ( & mut self , item : u64 , dst : & mut BytesMut ) -> io:: Result < ( ) > {
71
+ // Reserve space
72
+ dst. reserve ( 8 ) ;
73
+ dst. put_u64 ( item) ;
74
+ Ok ( ( ) )
75
+ }
76
+ }
77
+
42
78
/// This value should never be used
43
79
struct DontReadIntoThis ;
44
80
@@ -63,18 +99,39 @@ impl tokio::io::AsyncRead for DontReadIntoThis {
63
99
64
100
#[ tokio:: test]
65
101
async fn can_read_from_existing_buf ( ) {
66
- let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec ) ;
102
+ let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec :: default ( ) ) ;
67
103
parts. read_buf = BytesMut :: from ( & [ 0 , 0 , 0 , 42 ] [ ..] ) ;
68
104
69
105
let mut framed = Framed :: from_parts ( parts) ;
70
106
let num = assert_ok ! ( framed. next( ) . await . unwrap( ) ) ;
71
107
72
108
assert_eq ! ( num, 42 ) ;
109
+ assert_eq ! ( framed. codec( ) . read_bytes, 4 ) ;
110
+ }
111
+
112
+ #[ tokio:: test]
113
+ async fn can_read_from_existing_buf_after_codec_changed ( ) {
114
+ let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec :: default ( ) ) ;
115
+ parts. read_buf = BytesMut :: from ( & [ 0 , 0 , 0 , 42 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 84 ] [ ..] ) ;
116
+
117
+ let mut framed = Framed :: from_parts ( parts) ;
118
+ let num = assert_ok ! ( framed. next( ) . await . unwrap( ) ) ;
119
+
120
+ assert_eq ! ( num, 42 ) ;
121
+ assert_eq ! ( framed. codec( ) . read_bytes, 4 ) ;
122
+
123
+ let mut framed = framed. map_codec ( |codec| U64Codec {
124
+ read_bytes : codec. read_bytes ,
125
+ } ) ;
126
+ let num = assert_ok ! ( framed. next( ) . await . unwrap( ) ) ;
127
+
128
+ assert_eq ! ( num, 84 ) ;
129
+ assert_eq ! ( framed. codec( ) . read_bytes, 12 ) ;
73
130
}
74
131
75
132
#[ test]
76
133
fn external_buf_grows_to_init ( ) {
77
- let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec ) ;
134
+ let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec :: default ( ) ) ;
78
135
parts. read_buf = BytesMut :: from ( & [ 0 , 0 , 0 , 42 ] [ ..] ) ;
79
136
80
137
let framed = Framed :: from_parts ( parts) ;
@@ -85,7 +142,7 @@ fn external_buf_grows_to_init() {
85
142
86
143
#[ test]
87
144
fn external_buf_does_not_shrink ( ) {
88
- let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec ) ;
145
+ let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec :: default ( ) ) ;
89
146
parts. read_buf = BytesMut :: from ( & vec ! [ 0 ; INITIAL_CAPACITY * 2 ] [ ..] ) ;
90
147
91
148
let framed = Framed :: from_parts ( parts) ;
0 commit comments