use core::ops::Bound;
pub fn is_empty_range(start: &Bound<&[u8]>, end: &Bound<&[u8]>) -> bool {
match (start, end) {
(
Bound::Included(start_key) | Bound::Excluded(start_key),
Bound::Included(end_key) | Bound::Excluded(end_key),
) => {
start_key > end_key
|| (start_key == end_key
&& (matches!(start, Bound::Excluded(_)) || matches!(end, Bound::Excluded(_))))
}
_ => false,
}
}
pub fn next_prefix(prefix: &[u8]) -> Option<Vec<u8>> {
let mut next = prefix.to_vec();
for i in (0..next.len()).rev() {
if next[i] != 0xFF {
next[i] += 1;
next.truncate(i + 1);
return Some(next);
}
}
None
}
pub fn prefix_range(prefix: &[u8]) -> (Bound<Vec<u8>>, Bound<Vec<u8>>) {
let start = Bound::Included(prefix.to_vec());
let end = match next_prefix(prefix) {
Some(next) => Bound::Excluded(next),
None => Bound::Unbounded,
};
(start, end)
}
pub fn bound_to_owned(bound: Bound<&[u8]>) -> Bound<Vec<u8>> {
match bound {
Bound::Included(bytes) => Bound::Included(bytes.to_vec()),
Bound::Excluded(bytes) => Bound::Excluded(bytes.to_vec()),
Bound::Unbounded => Bound::Unbounded,
}
}
pub fn bound_as_slice(bound: &Bound<Vec<u8>>) -> Bound<&[u8]> {
match bound {
Bound::Included(bytes) => Bound::Included(bytes.as_slice()),
Bound::Excluded(bytes) => Bound::Excluded(bytes.as_slice()),
Bound::Unbounded => Bound::Unbounded,
}
}
#[cfg(test)]
mod tests {
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use super::{bound_as_slice, bound_to_owned};
use super::{is_empty_range, next_prefix, prefix_range};
use core::ops::Bound;
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use proptest::prelude::*;
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use crate::tests::{arb_key, bounds_contain};
#[test]
fn next_prefix_increments_last_non_ff_byte() {
assert_eq!(next_prefix(&[0x00]), Some(vec![0x01]));
assert_eq!(next_prefix(&[0x10, 0x20]), Some(vec![0x10, 0x21]));
assert_eq!(next_prefix(&[0xFE]), Some(vec![0xFF]));
}
#[test]
fn next_prefix_carries_and_truncates() {
assert_eq!(next_prefix(&[0x10, 0xFF]), Some(vec![0x11]));
assert_eq!(next_prefix(&[0x10, 0xFF, 0xFF]), Some(vec![0x11]));
assert_eq!(next_prefix(&[0x00, 0xFF, 0xFF]), Some(vec![0x01]));
}
#[test]
fn next_prefix_has_no_successor_for_all_ff_or_empty() {
assert_eq!(next_prefix(&[0xFF]), None);
assert_eq!(next_prefix(&[0xFF, 0xFF, 0xFF]), None);
assert_eq!(next_prefix(&[]), None);
}
#[test]
fn prefix_range_bounds_the_namespace() {
let (start, end) = prefix_range(&[0x10, 0x20]);
assert_eq!(start, Bound::Included(vec![0x10, 0x20]));
assert_eq!(end, Bound::Excluded(vec![0x10, 0x21]));
}
#[test]
fn prefix_range_is_unbounded_above_for_all_ff() {
let (start, end) = prefix_range(&[0xFF, 0xFF]);
assert_eq!(start, Bound::Included(vec![0xFF, 0xFF]));
assert_eq!(end, Bound::Unbounded);
let inside: &[u8] = &[0xFF, 0xFF, 0xFF];
assert!(inside.starts_with(&[0xFF, 0xFF]));
}
#[test]
fn empty_range_covers_inverted_and_equal_exclusive_bounds() {
assert!(is_empty_range(
&Bound::Included(b"z"),
&Bound::Excluded(b"a")
));
assert!(is_empty_range(
&Bound::Excluded(b"z"),
&Bound::Included(b"a")
));
assert!(is_empty_range(
&Bound::Excluded(b"k"),
&Bound::Excluded(b"k")
));
assert!(is_empty_range(
&Bound::Included(b"k"),
&Bound::Excluded(b"k")
));
assert!(is_empty_range(
&Bound::Excluded(b"k"),
&Bound::Included(b"k")
));
assert!(!is_empty_range(
&Bound::Included(b"k"),
&Bound::Included(b"k")
));
assert!(!is_empty_range(&Bound::Unbounded, &Bound::Excluded(b"k")));
assert!(!is_empty_range(&Bound::Included(b"k"), &Bound::Unbounded));
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
fn arb_bound() -> impl Strategy<Value = Bound<Vec<u8>>> {
prop_oneof![
Just(Bound::Unbounded),
arb_key().prop_map(Bound::Included),
arb_key().prop_map(Bound::Excluded),
]
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
fn arb_key_near(prefix: Vec<u8>) -> impl Strategy<Value = Vec<u8>> {
let extension = (Just(prefix.clone()), arb_key()).prop_map(|(mut key, tail)| {
key.extend_from_slice(&tail);
key
});
let successor = (
Just(prefix.clone()),
arb_key(),
any::<prop::sample::Index>(),
)
.prop_map(|(prefix, tail, index)| {
if prefix.is_empty() {
return tail;
}
let at = index.index(prefix.len());
let mut key = prefix[..=at].to_vec();
key[at] = key[at].wrapping_add(1);
key.extend_from_slice(&tail);
key
});
let truncation =
(Just(prefix), any::<prop::sample::Index>()).prop_map(|(prefix, index)| {
if prefix.is_empty() {
return prefix;
}
prefix[..index.index(prefix.len())].to_vec()
});
prop_oneof![
3 => extension,
3 => successor,
2 => truncation,
2 => arb_key(),
]
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
fn arb_prefix_and_key() -> impl Strategy<Value = (Vec<u8>, Vec<u8>)> {
arb_key().prop_flat_map(|prefix| (Just(prefix.clone()), arb_key_near(prefix)))
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
proptest! {
#[test]
fn prop_prefix_range_selects_exactly_the_prefixed_keys(
(prefix, key) in arb_prefix_and_key(),
) {
prop_assert_eq!(
key.starts_with(&prefix),
bounds_contain(&prefix_range(&prefix), &key),
);
}
#[test]
fn prop_prefix_range_contains_every_extension(prefix in arb_key(), tail in arb_key()) {
let mut key = prefix.clone();
key.extend_from_slice(&tail);
prop_assert!(bounds_contain(&prefix_range(&prefix), &key));
}
#[test]
fn prop_next_prefix_strictly_exceeds_every_extension(
prefix in arb_key(),
tail in arb_key(),
) {
let Some(next) = next_prefix(&prefix) else {
return Ok(());
};
let mut key = prefix.clone();
key.extend_from_slice(&tail);
prop_assert!(
key.as_slice() < next.as_slice(),
"{:02x?} must sort before {:02x?}",
key,
next
);
}
#[test]
fn prop_next_prefix_is_none_exactly_for_all_ff(prefix in arb_key()) {
prop_assert_eq!(
next_prefix(&prefix).is_none(),
prefix.iter().all(|byte| *byte == 0xFF),
);
}
#[test]
fn prop_bound_conversions_round_trip(bound in arb_bound()) {
prop_assert_eq!(bound_to_owned(bound_as_slice(&bound)), bound);
}
}
}