Skip to main content

reifydb_core/util/encoding/keycode/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4// This file includes and modifies code from the toydb project (https://github.com/erikgrinaker/toydb),
5// originally licensed under the Apache License, Version 2.0.
6// Original copyright:
7//   Copyright (c) 2024 Erik Grinaker
8//
9// The original Apache License can be found at:
10//   http://www.apache.org/licenses/LICENSE-2.0
11
12//! Order-preserving codec used to turn typed keys into the bytes that go on disk.
13//!
14//! Encoded byte sequences sort lexicographically in the same order as the logical keys they represent, so range scans
15//! over the storage tier produce results in natural key order without any decode pass. Submodules cover the
16//! catalog-specific key encodings, the generic `Serializer` and `Deserializer` pair, and per-type encoders for
17//! booleans, floats, and signed and unsigned integers.
18//!
19//! Invariant: the codec is order-preserving. For any two values `a < b` in their natural ordering, their encoded bytes
20//! must satisfy `encode(a) < encode(b)` lexicographically. Storage range queries, CDC, and replication all rely on this
21//! property; breaking it silently corrupts every range-scan-based operation in the workspace.
22
23use serde::{Deserialize, Serialize};
24
25pub mod catalog;
26pub mod deserialize;
27pub mod deserializer;
28pub mod serialize;
29pub mod serializer;
30pub(crate) mod varint;
31
32use std::{f32, f64};
33
34use reifydb_value::{
35	Result,
36	error::{Error, TypeError},
37};
38
39use crate::{
40	encoded::key::EncodedKey,
41	util::encoding::keycode::{deserialize::Deserializer, serialize::Serializer},
42};
43
44pub trait ByteSink {
45	fn push(&mut self, byte: u8);
46	fn extend_from_slice(&mut self, slice: &[u8]);
47}
48
49impl ByteSink for Vec<u8> {
50	fn push(&mut self, byte: u8) {
51		Vec::push(self, byte);
52	}
53	fn extend_from_slice(&mut self, slice: &[u8]) {
54		Vec::extend_from_slice(self, slice);
55	}
56}
57
58impl ByteSink for EncodedKey {
59	fn push(&mut self, byte: u8) {
60		EncodedKey::push(self, byte);
61	}
62	fn extend_from_slice(&mut self, slice: &[u8]) {
63		EncodedKey::extend_from_slice(self, slice);
64	}
65}
66
67pub fn encode_bool(value: bool) -> u8 {
68	if value {
69		0x00
70	} else {
71		0x01
72	}
73}
74
75pub fn encode_f32(value: f32) -> [u8; 4] {
76	let bits = value.to_bits();
77	if value.is_sign_negative() {
78		bits.to_be_bytes()
79	} else {
80		(!(bits ^ 0x80000000)).to_be_bytes()
81	}
82}
83
84pub fn encode_f64(value: f64) -> [u8; 8] {
85	let bits = value.to_bits();
86	if value.is_sign_negative() {
87		bits.to_be_bytes()
88	} else {
89		(!(bits ^ 0x8000000000000000)).to_be_bytes()
90	}
91}
92
93pub fn encode_i8(value: i8) -> [u8; 1] {
94	(!(value as u8 ^ 0x80)).to_be_bytes()
95}
96
97pub fn encode_i16(value: i16) -> [u8; 2] {
98	(!(value as u16 ^ 0x8000)).to_be_bytes()
99}
100
101pub fn encode_i32(value: i32) -> [u8; 4] {
102	(!(value as u32 ^ 0x80000000)).to_be_bytes()
103}
104
105pub fn encode_i64(value: i64) -> [u8; 8] {
106	(!(value as u64 ^ 0x8000000000000000)).to_be_bytes()
107}
108
109pub fn encode_i64_varint<B: ByteSink>(value: i64, output: &mut B) {
110	if value >= 0 {
111		if value < 64 {
112			output.push(!(0x80 | value as u8));
113		} else if value < 8192 + 64 {
114			let v = (value - 64) as u16;
115			output.push(!(0xc0 | (v >> 8) as u8));
116			output.push(!(v as u8));
117		} else {
118			output.push(!0xfe);
119			let inv = !(value as u64);
120			output.extend_from_slice(&inv.to_be_bytes());
121		}
122	} else if value >= -64 {
123		output.push(!(0x40 | (value + 64) as u8));
124	} else if value >= -8192 - 64 {
125		let v = (value + 64 + 8192) as u16;
126		output.push(!(0x20 | (v >> 8) as u8));
127		output.push(!(v as u8));
128	} else {
129		output.push(!0x01);
130		let inv = !(value as u64);
131		output.extend_from_slice(&inv.to_be_bytes());
132	}
133}
134
135pub fn encode_i128(value: i128) -> [u8; 16] {
136	(!(value as u128 ^ 0x80000000000000000000000000000000)).to_be_bytes()
137}
138
139pub fn encode_u8(value: u8) -> u8 {
140	!value
141}
142
143pub fn encode_u16(value: u16) -> [u8; 2] {
144	(!value).to_be_bytes()
145}
146
147pub fn encode_u32(value: u32) -> [u8; 4] {
148	(!value).to_be_bytes()
149}
150
151pub fn encode_u32_varint<B: ByteSink>(value: u32, output: &mut B) {
152	encode_u64_varint(value as u64, output);
153}
154
155pub fn encode_u64(value: u64) -> [u8; 8] {
156	(!value).to_be_bytes()
157}
158
159pub fn encode_u64_varint<B: ByteSink>(value: u64, output: &mut B) {
160	if value < (1 << 7) {
161		output.push(!(value as u8));
162	} else if value < (1 << 14) {
163		output.push(!(0x80 | (value >> 8) as u8));
164		output.push(!(value as u8));
165	} else if value < (1 << 21) {
166		output.push(!(0xc0 | (value >> 16) as u8));
167		output.push(!((value >> 8) as u8));
168		output.push(!(value as u8));
169	} else if value < (1 << 28) {
170		output.push(!(0xe0 | (value >> 24) as u8));
171		output.push(!((value >> 16) as u8));
172		output.push(!((value >> 8) as u8));
173		output.push(!(value as u8));
174	} else if value < (1 << 35) {
175		output.push(!(0xf0 | (value >> 32) as u8));
176		output.push(!((value >> 24) as u8));
177		output.push(!((value >> 16) as u8));
178		output.push(!((value >> 8) as u8));
179		output.push(!(value as u8));
180	} else if value < (1 << 42) {
181		output.push(!(0xf8 | (value >> 40) as u8));
182		output.push(!((value >> 32) as u8));
183		output.push(!((value >> 24) as u8));
184		output.push(!((value >> 16) as u8));
185		output.push(!((value >> 8) as u8));
186		output.push(!(value as u8));
187	} else if value < (1 << 49) {
188		output.push(!(0xfc | (value >> 48) as u8));
189		output.push(!((value >> 40) as u8));
190		output.push(!((value >> 32) as u8));
191		output.push(!((value >> 24) as u8));
192		output.push(!((value >> 16) as u8));
193		output.push(!((value >> 8) as u8));
194		output.push(!(value as u8));
195	} else if value < (1 << 56) {
196		output.push(!(0xfe | (value >> 56) as u8));
197		output.push(!((value >> 48) as u8));
198		output.push(!((value >> 40) as u8));
199		output.push(!((value >> 32) as u8));
200		output.push(!((value >> 24) as u8));
201		output.push(!((value >> 16) as u8));
202		output.push(!((value >> 8) as u8));
203		output.push(!(value as u8));
204	} else {
205		output.push(!0xff);
206		let inv = !value;
207		output.extend_from_slice(&inv.to_be_bytes());
208	}
209}
210
211pub fn decode_i64_varint(input: &mut &[u8]) -> Result<i64> {
212	if input.is_empty() {
213		return Err(Error::from(TypeError::SerdeKeycode {
214			message: "unexpected end of key while decoding i64 varint".to_string(),
215		}));
216	}
217	let first = !input[0];
218	let len = if first >= 0x80 {
219		if first < 0xc0 {
220			1
221		} else if first < 0xfe {
222			2
223		} else {
224			9
225		}
226	} else if first >= 0x40 {
227		1
228	} else if first >= 0x20 {
229		2
230	} else {
231		9
232	};
233
234	if input.len() < len {
235		return Err(Error::from(TypeError::SerdeKeycode {
236			message: "unexpected end of key while decoding i64 varint".to_string(),
237		}));
238	}
239
240	let mut buf = [0u8; 9];
241	for (dst, &src) in buf[..len].iter_mut().zip(&input[..len]) {
242		*dst = !src;
243	}
244	let mut slice = &buf[..len];
245	let v = varint::decode_i64_varint(&mut slice).ok_or_else(|| {
246		Error::from(TypeError::SerdeKeycode {
247			message: "failed to decode signed varint".to_string(),
248		})
249	})?;
250	*input = &input[len..];
251	Ok(v)
252}
253
254pub fn decode_u64_varint(input: &mut &[u8]) -> Result<u64> {
255	if input.is_empty() {
256		return Err(Error::from(TypeError::SerdeKeycode {
257			message: "unexpected end of key while decoding varint".to_string(),
258		}));
259	}
260	let first = !input[0];
261	let prefix = first.leading_ones() as usize;
262	let len = if prefix == 0 {
263		1
264	} else if prefix < 8 {
265		prefix + 1
266	} else {
267		9
268	};
269
270	if input.len() < len {
271		return Err(Error::from(TypeError::SerdeKeycode {
272			message: "unexpected end of key while decoding varint".to_string(),
273		}));
274	}
275
276	let mut buf = [0u8; 9];
277	for (dst, &src) in buf[..len].iter_mut().zip(&input[..len]) {
278		*dst = !src;
279	}
280	let mut slice = &buf[..len];
281	let v = varint::decode_u64_varint(&mut slice).unwrap();
282	*input = &input[len..];
283	Ok(v)
284}
285
286pub fn encode_u128(value: u128) -> [u8; 16] {
287	(!value).to_be_bytes()
288}
289
290pub fn encode_u128_varint<B: ByteSink>(value: u128, output: &mut B) {
291	if value < (1 << 56) {
292		encode_u64_varint(value as u64, output);
293	} else {
294		output.push(!0xff);
295		let bytes = value.to_be_bytes();
296		let start = bytes.iter().position(|&b| b != 0).unwrap_or(bytes.len() - 1);
297		let sig = &bytes[start..];
298		output.push(!(sig.len() as u8));
299		for &b in sig {
300			output.push(!b);
301		}
302	}
303}
304
305pub fn decode_u128_varint(input: &mut &[u8]) -> Result<u128> {
306	if input.is_empty() {
307		return Err(Error::from(TypeError::SerdeKeycode {
308			message: "unexpected end of key while decoding u128 varint".to_string(),
309		}));
310	}
311	let first = !input[0];
312	let prefix = first.leading_ones() as usize;
313	if prefix < 8 {
314		let len = prefix + 1;
315		if input.len() < len {
316			return Err(Error::from(TypeError::SerdeKeycode {
317				message: "unexpected end of key while decoding u128 varint".to_string(),
318			}));
319		}
320		let mut buf = [0u8; 9];
321		for (dst, &src) in buf[..len].iter_mut().zip(&input[..len]) {
322			*dst = !src;
323		}
324		let mut slice = &buf[..len];
325		let v = varint::decode_u64_varint(&mut slice).ok_or_else(|| {
326			Error::from(TypeError::SerdeKeycode {
327				message: "failed to decode u128 varint".to_string(),
328			})
329		})?;
330		*input = &input[len..];
331		Ok(v as u128)
332	} else {
333		if input.len() < 2 {
334			return Err(Error::from(TypeError::SerdeKeycode {
335				message: "unexpected end of key while decoding u128 varint length".to_string(),
336			}));
337		}
338		let len = (!input[1]) as usize;
339		if len == 0 || len > 16 || input.len() < 2 + len {
340			return Err(Error::from(TypeError::SerdeKeycode {
341				message: "invalid u128 varint length".to_string(),
342			}));
343		}
344		let mut bytes = [0u8; 16];
345		for (i, &src) in input[2..2 + len].iter().enumerate() {
346			bytes[16 - len + i] = !src;
347		}
348		*input = &input[2 + len..];
349		Ok(u128::from_be_bytes(bytes))
350	}
351}
352
353pub fn encode_bytes<B: ByteSink>(bytes: &[u8], output: &mut B) {
354	let mut start = 0;
355	while let Some(pos) = bytes[start..].iter().position(|&b| b == 0xff) {
356		let end = start + pos;
357		output.extend_from_slice(&bytes[start..end]);
358		output.extend_from_slice(&[0xff, 0x00]);
359		start = end + 1;
360	}
361	output.extend_from_slice(&bytes[start..]);
362	output.extend_from_slice(&[0xff, 0xff]);
363}
364
365#[macro_export]
366macro_rules! key_prefix {
367    ($($arg:tt)*) => {
368        &EncodedKey::new((&format!($($arg)*)).as_bytes().to_vec())
369    };
370}
371
372pub fn serialize<T: Serialize>(key: &T) -> Vec<u8> {
373	let mut serializer = Serializer {
374		output: Vec::new(),
375	};
376
377	key.serialize(&mut serializer).expect("key must be serializable");
378	serializer.output
379}
380
381pub fn deserialize<'a, T: Deserialize<'a>>(input: &'a [u8]) -> Result<T> {
382	let mut deserializer = Deserializer::from_bytes(input);
383	let t = T::deserialize(&mut deserializer)?;
384	if !deserializer.input.is_empty() {
385		return Err(Error::from(TypeError::SerdeKeycode {
386			message: format!(
387				"unexpected trailing bytes {:x?} at end of key {input:x?}",
388				deserializer.input,
389			),
390		}));
391	}
392	Ok(t)
393}
394
395#[cfg(test)]
396pub mod tests {
397	use std::borrow::Cow;
398
399	const PI_F32: f32 = f32::consts::PI;
400	const PI_F64: f64 = f64::consts::PI;
401
402	use reifydb_value::{
403		util::hex::encode,
404		value::{Value, ordered_f32::OrderedF32, ordered_f64::OrderedF64},
405	};
406	use serde_bytes::ByteBuf;
407
408	use super::*;
409	use crate::util::encoding::keycode::serializer::KeySerializer;
410
411	#[test]
412	fn test_u128_varint_roundtrip_and_descending_order() {
413		let values: Vec<u128> = vec![
414			0,
415			1,
416			2,
417			126,
418			127,
419			128,
420			129,
421			(1 << 14) - 1,
422			1 << 14,
423			(1 << 21) - 1,
424			1 << 21,
425			(1 << 28) - 1,
426			1 << 28,
427			(1 << 35) - 1,
428			1 << 35,
429			(1 << 42) - 1,
430			1 << 42,
431			(1 << 49) - 1,
432			1 << 49,
433			(1 << 56) - 1,
434			1 << 56,
435			(1u128 << 63) - 1,
436			1u128 << 63,
437			u64::MAX as u128 - 1,
438			u64::MAX as u128,
439			u64::MAX as u128 + 1,
440			1u128 << 100,
441			u128::MAX - 1,
442			u128::MAX,
443		];
444
445		for &v in &values {
446			let mut buf = Vec::new();
447			encode_u128_varint(v, &mut buf);
448			let mut slice = buf.as_slice();
449			let decoded = decode_u128_varint(&mut slice).unwrap();
450			assert_eq!(decoded, v, "roundtrip failed for {}", v);
451			assert!(slice.is_empty(), "trailing bytes after decoding {}", v);
452		}
453
454		// keycode is descending: a strictly larger value must encode to a
455		// lexicographically smaller key, so a forward scan yields the max id first.
456		let mut sorted = values.clone();
457		sorted.sort();
458		sorted.dedup();
459		let mut prev: Option<(u128, Vec<u8>)> = None;
460		for &v in &sorted {
461			let mut buf = Vec::new();
462			encode_u128_varint(v, &mut buf);
463			if let Some((pv, pe)) = &prev {
464				assert!(*pv < v);
465				assert!(
466					buf < *pe,
467					"not descending: {} -> {:?} should sort before {} -> {:?}",
468					v,
469					buf,
470					pv,
471					pe
472				);
473			}
474			prev = Some((v, buf));
475		}
476	}
477
478	#[derive(Debug, Deserialize, Serialize, PartialEq)]
479	enum Key<'a> {
480		Unit,
481		NewType(String),
482		Tuple(bool, #[serde(with = "serde_bytes")] Vec<u8>, u64),
483		Cow(
484			#[serde(with = "serde_bytes")]
485			#[serde(borrow)]
486			Cow<'a, [u8]>,
487			bool,
488			#[serde(borrow)] Cow<'a, str>,
489		),
490	}
491
492	macro_rules! test_serde {
493        ( $( $name:ident: $input:expr => $expect:literal, )* ) => {
494        $(
495            #[test]
496            fn $name(){
497                let mut input = $input;
498                let expect = $expect;
499                let output = serialize(&input);
500                assert_eq!(encode(&output), expect, "encode failed");
501
502                let expect = input;
503                input = deserialize(&output).unwrap();
504                assert_eq!(input, expect, "decode failed");
505            }
506        )*
507        };
508    }
509
510	test_serde! {
511	bool_false: false => "01",
512	bool_true: true => "00",
513
514	f32_min: f32::MIN => "ff7fffff",
515	f32_neg_inf: f32::NEG_INFINITY => "ff800000",
516	f32_neg_pi: -PI_F32 => "c0490fdb",
517	f32_neg_zero: -0f32 => "80000000",
518	f32_zero: 0f32 => "7fffffff",
519	f32_pi: PI_F32 => "3fb6f024",
520	f32_max: f32::MAX => "00800000",
521	f32_inf: f32::INFINITY => "007fffff",
522
523	f64_min: f64::MIN => "ffefffffffffffff",
524	f64_neg_inf: f64::NEG_INFINITY => "fff0000000000000",
525	f64_neg_pi: -PI_F64 => "c00921fb54442d18",
526	f64_neg_zero: -0f64 => "8000000000000000",
527	f64_zero: 0f64 => "7fffffffffffffff",
528	f64_pi: PI_F64 => "3ff6de04abbbd2e7",
529	f64_max: f64::MAX => "0010000000000000",
530	f64_inf: f64::INFINITY => "000fffffffffffff",
531
532	i8_min: i8::MIN => "ff",
533	i8_neg_1: -1i8 => "80",
534	i8_0: 0i8 => "7f",
535	i8_1: 1i8 => "7e",
536	i8_max: i8::MAX => "00",
537
538	i16_min: i16::MIN => "ffff",
539	i16_neg_1: -1i16 => "8000",
540	i16_0: 0i16 => "7fff",
541	i16_1: 1i16 => "7ffe",
542	i16_max: i16::MAX => "0000",
543
544	i32_min: i32::MIN => "ffffffff",
545	i32_neg_1: -1i32 => "80000000",
546	i32_0: 0i32 => "7fffffff",
547	i32_1: 1i32 => "7ffffffe",
548	i32_max: i32::MAX => "00000000",
549
550	i64_min: i64::MIN => "fe7fffffffffffffff",
551	i64_neg_65535: -65535i64 => "fe000000000000fffe",
552	i64_neg_1: -1i64 => "80",
553	i64_0: 0i64 => "7f",
554	i64_1: 1i64 => "7e",
555	i64_65535: 65535i64 => "01ffffffffffff0000",
556	i64_max: i64::MAX => "018000000000000000",
557
558	i128_min: i128::MIN => "ffffffffffffffffffffffffffffffff",
559	i128_neg_1: -1i128 => "80000000000000000000000000000000",
560	i128_0: 0i128 => "7fffffffffffffffffffffffffffffff",
561	i128_1: 1i128 => "7ffffffffffffffffffffffffffffffe",
562	i128_max: i128::MAX => "00000000000000000000000000000000",
563
564	u8_min: u8::MIN => "ff",
565	u8_1: 1_u8 => "fe",
566	u8_255: 255_u8 => "00",
567
568	u16_min: u16::MIN => "ffff",
569	u16_1: 1_u16 => "fffe",
570	u16_255: 255_u16 => "ff00",
571	u16_65535: u16::MAX => "0000",
572
573	u32_min: u32::MIN => "ff",
574	u32_1: 1_u32 => "fe",
575	u32_65535: 65535_u32 => "3f0000",
576	u32_max: u32::MAX => "0f00000000",
577
578	u64_min: u64::MIN => "ff",
579	u64_1: 1_u64 => "fe",
580	u64_65535: 65535_u64 => "3f0000",
581	u64_max: u64::MAX => "000000000000000000",
582
583	u128_min: u128::MIN => "ffffffffffffffffffffffffffffffff",
584	u128_1: 1_u128 => "fffffffffffffffffffffffffffffffe",
585	u128_65535: 65535_u128 => "ffffffffffffffffffffffffffff0000",
586	u128_max: u128::MAX => "00000000000000000000000000000000",
587
588	bytes: ByteBuf::from(vec![0x01, 0xff]) => "01ff00ffff",
589	bytes_empty: ByteBuf::new() => "ffff",
590	bytes_escape: ByteBuf::from(vec![0x00, 0x01, 0x02]) => "000102ffff",
591
592	string: "foo".to_string() => "666f6fffff",
593	string_empty: "".to_string() => "ffff",
594	string_escape: "foo\x00bar".to_string() => "666f6f00626172ffff",
595	string_utf8: "👋".to_string() => "f09f918bffff",
596
597	tuple: (true, u64::MAX, ByteBuf::from(vec![0x00, 0x01])) => "000000000000000000000001ffff",
598	array_bool: [false, true, false] => "010001",
599	vec_bool: vec![false, true, false] => "010001",
600	vec_u64: vec![u64::MIN, u64::MAX, 65535_u64] => "ff0000000000000000003f0000",
601
602	enum_unit: Key::Unit => "00",
603	enum_newtype: Key::NewType("foo".to_string()) => "01666f6fffff",
604	enum_tuple: Key::Tuple(false, vec![0x00, 0x01], u64::MAX) => "02010001ffff000000000000000000",
605	enum_cow: Key::Cow(vec![0x00, 0x01].into(), false, String::from("foo").into()) => "030001ffff01666f6fffff",
606	enum_cow_borrow: Key::Cow([0x00, 0x01].as_slice().into(), false, "foo".into()) => "030001ffff01666f6fffff",
607
608	value_none: Value::none() => "00",
609	value_bool: Value::Boolean(true) => "0100",
610	value_float4: Value::Float4(OrderedF32::try_from(PI_F32).unwrap()) => "023fb6f024",
611	value_float8: Value::Float8(OrderedF64::try_from(PI_F64).unwrap()) => "033ff6de04abbbd2e7",
612	value_int1: Value::Int1(-1) => "0480",
613	value_int4: Value::Int4(123456) => "067ffe1dbf",
614	value_int8: Value::Int8(31415926) => "0701fffffffffe20a189",
615	value_int16: Value::Int16(-123456789012345678901234567890i128) => "08800000018ee90ff6c373e0ee4e3f0ad1",
616	value_string: Value::Utf8("foo".to_string()) => "09666f6fffff",
617	value_uint1: Value::Uint1(255) => "0a00",
618	value_uint2: Value::Uint2(65535) => "0b0000",
619	value_uint4: Value::Uint4(4294967295) => "0c0f00000000",
620	value_uint8: Value::Uint8(18446744073709551615) => "0d000000000000000000",
621	value_uint16: Value::Uint16(340282366920938463463374607431768211455u128) => "0e00000000000000000000000000000000",
622
623	// Option<bool>
624	option_none_bool: None::<bool> => "00",
625	option_some_true: Some(true) => "0100",
626	option_some_false: Some(false) => "0101",
627
628	// Option<f32>
629	option_none_f32: None::<f32> => "00",
630	option_some_f32: Some(PI_F32) => "013fb6f024",
631
632	// Option<f64>
633	option_none_f64: None::<f64> => "00",
634	option_some_f64: Some(PI_F64) => "013ff6de04abbbd2e7",
635
636	// Option<i8>
637	option_none_i8: None::<i8> => "00",
638	option_some_i8: Some(0i8) => "017f",
639
640	// Option<i16>
641	option_none_i16: None::<i16> => "00",
642	option_some_i16: Some(0i16) => "017fff",
643
644	// Option<i32>
645	option_none_i32: None::<i32> => "00",
646	option_some_i32: Some(0i32) => "017fffffff",
647
648	// Option<i64>
649	option_none_i64: None::<i64> => "00",
650	option_some_i64: Some(0i64) => "017f",
651
652	// Option<i128>
653	option_none_i128: None::<i128> => "00",
654	option_some_i128: Some(0i128) => "017fffffffffffffffffffffffffffffff",
655
656	// Option<u8>
657	option_none_u8: None::<u8> => "00",
658	option_some_u8: Some(0u8) => "01ff",
659
660	// Option<u16>
661	option_none_u16: None::<u16> => "00",
662	option_some_u16: Some(0u16) => "01ffff",
663
664	// Option<u32>
665	option_none_u32: None::<u32> => "00",
666	option_some_u32: Some(0u32) => "01ff",
667
668	// Option<u64>
669	option_none_u64: None::<u64> => "00",
670	option_some_u64: Some(0u64) => "01ff",
671
672	// Option<u128>
673	option_none_u128: None::<u128> => "00",
674	option_some_u128: Some(0u128) => "01ffffffffffffffffffffffffffffffff",
675
676	// Option<String>
677	option_none_string: None::<String> => "00",
678	option_some_string: Some("foo".to_string()) => "01666f6fffff",
679	option_some_empty_string: Some("".to_string()) => "01ffff",
680
681	// Option<ByteBuf>
682	option_none_bytes: None::<ByteBuf> => "00",
683	option_some_bytes: Some(ByteBuf::from(vec![0x01, 0xff])) => "0101ff00ffff",
684
685	// Nested Option<Option<bool>>
686	option_nested_none: None::<Option<bool>> => "00",
687	option_nested_some_none: Some(None::<bool>) => "0100",
688	option_nested_some_some_true: Some(Some(true)) => "010100",
689	option_nested_some_some_false: Some(Some(false)) => "010101",
690
691	// Nested Option<Option<i32>>
692	option_nested_none_i32: None::<Option<i32>> => "00",
693	option_nested_some_none_i32: Some(None::<i32>) => "0100",
694	option_nested_some_some_i32: Some(Some(0i32)) => "01017fffffff",
695
696	// Nested Option<Option<String>>
697	option_nested_some_some_string: Some(Some("foo".to_string())) => "0101666f6fffff",
698
699	// Triple nested Option<Option<Option<bool>>>
700	option_triple_none: None::<Option<Option<bool>>> => "00",
701	option_triple_some_none: Some(None::<Option<bool>>) => "0100",
702	option_triple_some_some_none: Some(Some(None::<bool>)) => "010100",
703	option_triple_some_some_some: Some(Some(Some(true))) => "01010100",}
704
705	#[test]
706	fn test_option_ordering() {
707		// Descending: None > Some(MAX) > Some(0) > Some(MIN)
708		// Byte order: None < Some(MAX) < Some(0) < Some(MIN)
709		let none = serialize(&None::<i32>);
710		let some_max = serialize(&Some(i32::MAX));
711		let some_zero = serialize(&Some(0i32));
712		let some_min = serialize(&Some(i32::MIN));
713		assert!(none < some_max);
714		assert!(some_max < some_zero);
715		assert!(some_zero < some_min);
716	}
717
718	#[test]
719	fn test_nested_option_ordering() {
720		let none = serialize(&None::<Option<bool>>);
721		let some_none = serialize(&Some(None::<bool>));
722		let some_some_true = serialize(&Some(Some(true)));
723		let some_some_false = serialize(&Some(Some(false)));
724		assert!(none < some_none);
725		assert!(some_none < some_some_true);
726		assert!(some_some_true < some_some_false);
727	}
728
729	#[test]
730	fn test_key_serializer() {
731		// Test bool
732		let mut s = KeySerializer::new();
733		s.extend_bool(true);
734		assert_eq!(s.finish(), vec![0x00]);
735
736		let mut s = KeySerializer::new();
737		s.extend_bool(false);
738		assert_eq!(s.finish(), vec![0x01]);
739
740		// Test u64
741		let mut s = KeySerializer::new();
742		s.extend_u64(0u64);
743		assert_eq!(s.finish(), vec![0xff]);
744
745		// Test i64
746		let mut s = KeySerializer::new();
747		s.extend_i64(0i64);
748		assert_eq!(s.finish(), vec![0x7f]);
749
750		// Test f32
751		let mut s = KeySerializer::new();
752		s.extend_f32(0.0f32);
753		assert_eq!(s.finish(), vec![0x7f, 0xff, 0xff, 0xff]);
754
755		// Test f64
756		let mut s = KeySerializer::new();
757		s.extend_f64(0.0f64);
758		assert_eq!(s.finish(), vec![0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]);
759
760		// Test bytes
761		let mut s = KeySerializer::new();
762		s.extend_bytes(b"foo");
763		assert_eq!(s.finish(), vec![0x66, 0x6f, 0x6f, 0xff, 0xff]);
764
765		// Test chaining
766		let mut s = KeySerializer::with_capacity(32);
767		s.extend_bool(true).extend_u32(1u32).extend_i16(-1i16).extend_bytes(b"test");
768		let result = s.finish();
769		assert!(!result.is_empty());
770		assert!(result.len() >= 10); // Should have all the encoded values
771	}
772}