From c7513277fab4b0a03eaec483593fefe267f381cb Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 10 Nov 2023 14:42:50 -0800 Subject: [PATCH] Implement RedbValue and RedbKey for char --- src/types.rs | 35 +++++++++++++++++++++++++++++++++++ tests/basic_tests.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/types.rs b/src/types.rs index 01f334ec..3aae5cb6 100644 --- a/src/types.rs +++ b/src/types.rs @@ -393,6 +393,41 @@ impl RedbKey for &str { } } +impl RedbValue for char { + type SelfType<'a> = char; + type AsBytes<'a> = [u8; 3] where Self: 'a; + + fn fixed_width() -> Option { + Some(3) + } + + fn from_bytes<'a>(data: &'a [u8]) -> char + where + Self: 'a, + { + char::from_u32(u32::from_le_bytes([data[0], data[1], data[2], 0])).unwrap() + } + + fn as_bytes<'a, 'b: 'a>(value: &'a Self::SelfType<'b>) -> [u8; 3] + where + Self: 'a, + Self: 'b, + { + let bytes = u32::from(*value).to_le_bytes(); + [bytes[0], bytes[1], bytes[2]] + } + + fn type_name() -> TypeName { + TypeName::internal(stringify!(char)) + } +} + +impl RedbKey for char { + fn compare(data1: &[u8], data2: &[u8]) -> Ordering { + Self::from_bytes(data1).cmp(&Self::from_bytes(data2)) + } +} + macro_rules! le_value { ($t:ty) => { impl RedbValue for $t { diff --git a/tests/basic_tests.rs b/tests/basic_tests.rs index 28a8fcaf..ecc3d593 100644 --- a/tests/basic_tests.rs +++ b/tests/basic_tests.rs @@ -1606,3 +1606,29 @@ fn generic_signature_lifetimes() { read_key_generic(table, key, db); } } + +#[test] +fn char_type() { + let tmpfile = create_tempfile(); + let db = Database::create(tmpfile.path()).unwrap(); + + let definition: TableDefinition = TableDefinition::new("x"); + + let write_txn = db.begin_write().unwrap(); + { + let mut table = write_txn.open_table(definition).unwrap(); + table.insert('a', &'b').unwrap(); + table.insert(&'b', 'a').unwrap(); + } + write_txn.commit().unwrap(); + + let read_txn = db.begin_read().unwrap(); + let table = read_txn.open_table(definition).unwrap(); + assert_eq!('a', table.get(&'b').unwrap().unwrap().value()); + assert_eq!('b', table.get(&'a').unwrap().unwrap().value()); + + let mut iter = table.iter().unwrap(); + assert_eq!(iter.next().unwrap().unwrap().0.value(), 'a'); + assert_eq!(iter.next().unwrap().unwrap().0.value(), 'b'); + assert!(iter.next().is_none()); +}