diff --git a/src/types.rs b/src/types.rs index 933c7a1e..20e9b915 100644 --- a/src/types.rs +++ b/src/types.rs @@ -211,6 +211,25 @@ impl RedbValue for Option { } } +impl RedbKey for Option { + #[allow(clippy::collapsible_else_if)] + fn compare(data1: &[u8], data2: &[u8]) -> Ordering { + if data1[0] == 0 { + if data2[0] == 0 { + Ordering::Equal + } else { + Ordering::Less + } + } else { + if data2[0] == 0 { + Ordering::Greater + } else { + T::compare(&data1[1..], &data2[1..]) + } + } + } +} + impl RedbValue for &[u8] { type SelfType<'a> = &'a [u8] where diff --git a/tests/basic_tests.rs b/tests/basic_tests.rs index 01bd75a0..8d73c5a4 100644 --- a/tests/basic_tests.rs +++ b/tests/basic_tests.rs @@ -999,20 +999,24 @@ fn option_type() { let tmpfile = create_tempfile(); let db = Database::create(tmpfile.path()).unwrap(); - let definition: TableDefinition> = TableDefinition::new("x"); + let definition: TableDefinition, Option> = TableDefinition::new("x"); let write_txn = db.begin_write().unwrap(); { let mut table = write_txn.open_table(definition).unwrap(); - table.insert(0, None).unwrap(); - table.insert(1, Some(1)).unwrap(); + table.insert(None, None).unwrap(); + table.insert(None, Some(0)).unwrap(); + table.insert(Some(1), Some(1)).unwrap(); } write_txn.commit().unwrap(); let read_txn = db.begin_read().unwrap(); let table = read_txn.open_table(definition).unwrap(); - assert_eq!(table.get(0).unwrap().unwrap().value(), None); - assert_eq!(table.get(1).unwrap().unwrap().value(), Some(1)); + assert_eq!(table.get(None).unwrap().unwrap().value(), Some(0)); + assert_eq!(table.get(Some(1)).unwrap().unwrap().value(), Some(1)); + let mut iter = table.iter().unwrap(); + assert_eq!(iter.next().unwrap().unwrap().0.value(), None); + assert_eq!(iter.next().unwrap().unwrap().0.value(), Some(1)); } #[test]