@@ -12,7 +12,10 @@ use std::task::{Context, Poll};
1212const INITIAL_CAPACITY : usize = 8 * 1024 ;
1313
1414/// Encode and decode u32 values.
15- struct U32Codec ;
15+ #[ derive( Default ) ]
16+ struct U32Codec {
17+ read_bytes : usize ,
18+ }
1619
1720impl Decoder for U32Codec {
1821 type Item = u32 ;
@@ -24,6 +27,7 @@ impl Decoder for U32Codec {
2427 }
2528
2629 let n = buf. split_to ( 4 ) . get_u32 ( ) ;
30+ self . read_bytes += 4 ;
2731 Ok ( Some ( n) )
2832 }
2933}
@@ -39,6 +43,38 @@ impl Encoder<u32> for U32Codec {
3943 }
4044}
4145
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+
4278/// This value should never be used
4379struct DontReadIntoThis ;
4480
@@ -63,18 +99,39 @@ impl tokio::io::AsyncRead for DontReadIntoThis {
6399
64100#[ tokio:: test]
65101async fn can_read_from_existing_buf ( ) {
66- let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec ) ;
102+ let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec :: default ( ) ) ;
67103 parts. read_buf = BytesMut :: from ( & [ 0 , 0 , 0 , 42 ] [ ..] ) ;
68104
69105 let mut framed = Framed :: from_parts ( parts) ;
70106 let num = assert_ok ! ( framed. next( ) . await . unwrap( ) ) ;
71107
72108 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 ) ;
73130}
74131
75132#[ test]
76133fn external_buf_grows_to_init ( ) {
77- let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec ) ;
134+ let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec :: default ( ) ) ;
78135 parts. read_buf = BytesMut :: from ( & [ 0 , 0 , 0 , 42 ] [ ..] ) ;
79136
80137 let framed = Framed :: from_parts ( parts) ;
@@ -85,7 +142,7 @@ fn external_buf_grows_to_init() {
85142
86143#[ test]
87144fn external_buf_does_not_shrink ( ) {
88- let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec ) ;
145+ let mut parts = FramedParts :: new ( DontReadIntoThis , U32Codec :: default ( ) ) ;
89146 parts. read_buf = BytesMut :: from ( & vec ! [ 0 ; INITIAL_CAPACITY * 2 ] [ ..] ) ;
90147
91148 let framed = Framed :: from_parts ( parts) ;
0 commit comments