File tree Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Expand file tree Collapse file tree 3 files changed +78
-0
lines changed Original file line number Diff line number Diff line change @@ -463,6 +463,42 @@ where
463
463
}
464
464
}
465
465
466
+ impl Encoder for & str {
467
+ fn write_size ( & self , _version : Version ) -> usize {
468
+ 2 + self . len ( )
469
+ }
470
+
471
+ fn encode < T > ( & self , dest : & mut T , _version : Version ) -> Result < ( ) , Error >
472
+ where
473
+ T : BufMut ,
474
+ {
475
+ if dest. remaining_mut ( ) < 2 + self . len ( ) {
476
+ return Err ( Error :: new (
477
+ ErrorKind :: UnexpectedEof ,
478
+ "not enough capacity for string" ,
479
+ ) ) ;
480
+ }
481
+
482
+ dest. put_u16 ( self . len ( ) as u16 ) ;
483
+
484
+ let mut writer = dest. writer ( ) ;
485
+ let bytes_written = writer. write ( <str >:: as_bytes ( self ) ) ?;
486
+
487
+ if bytes_written != self . len ( ) {
488
+ return Err ( Error :: new (
489
+ ErrorKind :: UnexpectedEof ,
490
+ format ! (
491
+ "out of {} bytes, {} not written" ,
492
+ self . len( ) ,
493
+ self . len( ) - bytes_written
494
+ ) ,
495
+ ) ) ;
496
+ }
497
+
498
+ Ok ( ( ) )
499
+ }
500
+ }
501
+
466
502
#[ cfg( test) ]
467
503
mod test {
468
504
Original file line number Diff line number Diff line change @@ -80,6 +80,21 @@ impl RecordKey {
80
80
}
81
81
}
82
82
83
+ impl From < RecordData > for RecordKey {
84
+ fn from ( k : RecordData ) -> Self {
85
+ Self ( RecordKeyInner :: Key ( k) )
86
+ }
87
+ }
88
+
89
+ impl From < RecordKey > for Option < RecordData > {
90
+ fn from ( k : RecordKey ) -> Self {
91
+ match k. 0 {
92
+ RecordKeyInner :: Key ( data) => Some ( data) ,
93
+ RecordKeyInner :: Null => None ,
94
+ }
95
+ }
96
+ }
97
+
83
98
#[ derive( Hash ) ]
84
99
enum RecordKeyInner {
85
100
Null ,
@@ -808,4 +823,15 @@ mod test {
808
823
} ;
809
824
assert_eq ! ( record. timestamp( ) , 1_000_000_800 ) ;
810
825
}
826
+
827
+ #[ test]
828
+ fn test_key_conversion ( ) {
829
+ let null_key = RecordKey :: NULL ;
830
+ let data: Option < RecordData > = null_key. into ( ) ;
831
+ assert_eq ! ( data, None ) ;
832
+ let data = RecordData :: from ( "test" ) ;
833
+ let key = RecordKey :: from ( data. clone ( ) ) ;
834
+ let data2: Option < RecordData > = key. into ( ) ;
835
+ assert_eq ! ( data2, Some ( data) ) ;
836
+ }
811
837
}
Original file line number Diff line number Diff line change
1
+ use std:: io:: Cursor ;
2
+
3
+ use fluvio_protocol:: { Decoder , Encoder } ;
4
+
5
+ #[ test]
6
+ fn test_encode_str ( ) {
7
+ let value = "hello" ;
8
+ assert_eq ! ( value. write_size( 0 ) , 7 ) ; // 7 bytes for the string "hello"
9
+ let mut dest = Vec :: new ( ) ;
10
+ value. encode ( & mut dest, 0 ) . expect ( "Failed to encode string" ) ;
11
+
12
+ // decode the string back to its original form
13
+ let mut buf = Cursor :: new ( dest) ;
14
+ let decoded = String :: decode_from ( & mut buf, 0 ) . expect ( "Failed to decode string" ) ;
15
+ assert_eq ! ( decoded, "hello" ) ;
16
+ }
You can’t perform that action at this time.
0 commit comments