11// Common types, constants, error handling, record tag encoding, and common encoding utilities
22
3+ use bytes:: BytesMut ;
4+
35/// Key format version (currently 0x01)
46pub const KEY_VERSION : u8 = 0x01 ;
57
@@ -87,7 +89,7 @@ pub fn decode_record_tag(tag: u8) -> Result<(RecordType, Option<TimeBucketSize>)
8789/// Encode a UTF-8 string
8890///
8991/// Format: `len: u16` (little-endian) + `len` bytes of UTF-8
90- pub fn encode_utf8 ( s : & str , buf : & mut Vec < u8 > ) {
92+ pub fn encode_utf8 ( s : & str , buf : & mut BytesMut ) {
9193 let bytes = s. as_bytes ( ) ;
9294 let len = bytes. len ( ) ;
9395 if len > u16:: MAX as usize {
@@ -130,7 +132,7 @@ pub fn decode_utf8(buf: &mut &[u8]) -> Result<String, EncodingError> {
130132/// Encode an optional non-empty UTF-8 string
131133///
132134/// Format: Same as Utf8, but `len = 0` means `None`
133- pub fn encode_optional_utf8 ( opt : Option < & str > , buf : & mut Vec < u8 > ) {
135+ pub fn encode_optional_utf8 ( opt : Option < & str > , buf : & mut BytesMut ) {
134136 match opt {
135137 Some ( s) => encode_utf8 ( s, buf) ,
136138 None => {
@@ -158,7 +160,7 @@ pub fn decode_optional_utf8(buf: &mut &[u8]) -> Result<Option<String>, EncodingE
158160
159161/// Trait for types that can be encoded to bytes
160162pub trait Encode {
161- fn encode ( & self , buf : & mut Vec < u8 > ) ;
163+ fn encode ( & self , buf : & mut BytesMut ) ;
162164}
163165
164166/// Trait for types that can be decoded from bytes
@@ -169,7 +171,7 @@ pub trait Decode: Sized {
169171/// Encode an array of encodable items
170172///
171173/// Format: `count: u16` (little-endian) + `count` serialized elements
172- pub fn encode_array < T : Encode > ( items : & [ T ] , buf : & mut Vec < u8 > ) {
174+ pub fn encode_array < T : Encode > ( items : & [ T ] , buf : & mut BytesMut ) {
173175 let count = items. len ( ) ;
174176 if count > u16:: MAX as usize {
175177 panic ! ( "Array too long: {} items" , count) ;
@@ -202,7 +204,7 @@ pub fn decode_array<T: Decode>(buf: &mut &[u8]) -> Result<Vec<T>, EncodingError>
202204/// Encode a single array (no count prefix)
203205///
204206/// Format: Serialized elements back-to-back with no additional padding
205- pub fn encode_single_array < T : Encode > ( items : & [ T ] , buf : & mut Vec < u8 > ) {
207+ pub fn encode_single_array < T : Encode > ( items : & [ T ] , buf : & mut BytesMut ) {
206208 for item in items {
207209 item. encode ( buf) ;
208210 }
@@ -269,11 +271,11 @@ mod tests {
269271 fn should_encode_and_decode_utf8 ( ) {
270272 // given
271273 let s = "Hello, 世界!" ;
272- let mut buf = Vec :: new ( ) ;
274+ let mut buf = BytesMut :: new ( ) ;
273275
274276 // when
275277 encode_utf8 ( s, & mut buf) ;
276- let mut slice = buf. as_slice ( ) ;
278+ let mut slice = buf. as_ref ( ) ;
277279 let decoded = decode_utf8 ( & mut slice) . unwrap ( ) ;
278280
279281 // then
@@ -285,11 +287,11 @@ mod tests {
285287 fn should_encode_and_decode_optional_utf8_some ( ) {
286288 // given
287289 let s = Some ( "test" ) ;
288- let mut buf = Vec :: new ( ) ;
290+ let mut buf = BytesMut :: new ( ) ;
289291
290292 // when
291293 encode_optional_utf8 ( s, & mut buf) ;
292- let mut slice = buf. as_slice ( ) ;
294+ let mut slice = buf. as_ref ( ) ;
293295 let decoded = decode_optional_utf8 ( & mut slice) . unwrap ( ) ;
294296
295297 // then
@@ -300,11 +302,11 @@ mod tests {
300302 fn should_encode_and_decode_optional_utf8_none ( ) {
301303 // given
302304 let s: Option < & str > = None ;
303- let mut buf = Vec :: new ( ) ;
305+ let mut buf = BytesMut :: new ( ) ;
304306
305307 // when
306308 encode_optional_utf8 ( s, & mut buf) ;
307- let mut slice = buf. as_slice ( ) ;
309+ let mut slice = buf. as_ref ( ) ;
308310 let decoded = decode_optional_utf8 ( & mut slice) . unwrap ( ) ;
309311
310312 // then
0 commit comments