diff --git a/proxmox-schema/src/const_test_utils.rs b/proxmox-schema/src/const_test_utils.rs new file mode 100644 index 00000000..3613ac35 --- /dev/null +++ b/proxmox-schema/src/const_test_utils.rs @@ -0,0 +1,25 @@ +/// Note: this only compares *bytes* and is not strictly speaking equivalent to str::cmp! +pub const fn byte_string_cmp(a: &[u8], b: &[u8]) -> std::cmp::Ordering { + use std::cmp::Ordering::*; + + // const-version of `min(a.len(), b.len())` while simultaneously remembering + // `cmp(a.len(), b.len())`. + let (end, len_result) = if a.len() < b.len() { + (a.len(), Less) + } else if a.len() > b.len() { + (b.len(), Greater) + } else { + (a.len(), Equal) + }; + + let mut i = 0; + while i != end { + if a[i] < b[i] { + return Less; + } else if a[i] > b[i] { + return Greater; + } + i += 1; + } + len_result +} diff --git a/proxmox-schema/src/lib.rs b/proxmox-schema/src/lib.rs index 161f53ce..1647e8a9 100644 --- a/proxmox-schema/src/lib.rs +++ b/proxmox-schema/src/lib.rs @@ -31,3 +31,5 @@ pub mod upid; #[cfg(feature = "api-types")] pub mod api_types; + +pub(crate) mod const_test_utils;