#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct HtonCapabilities(u32);
impl HtonCapabilities {
pub const TRANSACTIONS: Self = Self(1 << 0);
pub const XA: Self = Self(1 << 1);
pub const SAVEPOINTS: Self = Self(1 << 2);
pub const SDI: Self = Self(1 << 3);
pub const SECONDARY_ENGINE: Self = Self(1 << 4);
pub const CLONE: Self = Self(1 << 5);
pub const PAGE_TRACKING: Self = Self(1 << 6);
pub const PARTITIONING: Self = Self(1 << 7);
pub const TABLESPACES: Self = Self(1 << 8);
pub const DICT_BACKEND: Self = Self(1 << 9);
pub const ENGINE_LOG: Self = Self(1 << 10);
pub const ENCRYPTION: Self = Self(1 << 11);
#[must_use]
pub const fn empty() -> Self {
Self(0)
}
#[must_use]
pub const fn bits(self) -> u32 {
self.0
}
#[must_use]
pub const fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[must_use]
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl core::ops::BitOr for HtonCapabilities {
type Output = Self;
fn bitor(self, rhs: Self) -> Self {
self.union(rhs)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_contains_only_empty() {
let e = HtonCapabilities::empty();
assert_eq!(e.bits(), 0);
assert!(e.contains(HtonCapabilities::empty()));
assert!(!e.contains(HtonCapabilities::TRANSACTIONS));
}
#[test]
fn union_sets_both_bits() {
let c = HtonCapabilities::TRANSACTIONS | HtonCapabilities::SAVEPOINTS;
assert!(c.contains(HtonCapabilities::TRANSACTIONS));
assert!(c.contains(HtonCapabilities::SAVEPOINTS));
assert!(!c.contains(HtonCapabilities::XA));
}
#[test]
fn each_capability_has_a_distinct_bit() {
let all = [
HtonCapabilities::TRANSACTIONS,
HtonCapabilities::XA,
HtonCapabilities::SAVEPOINTS,
HtonCapabilities::SDI,
HtonCapabilities::SECONDARY_ENGINE,
HtonCapabilities::CLONE,
HtonCapabilities::PAGE_TRACKING,
HtonCapabilities::PARTITIONING,
HtonCapabilities::TABLESPACES,
HtonCapabilities::DICT_BACKEND,
HtonCapabilities::ENGINE_LOG,
HtonCapabilities::ENCRYPTION,
];
for (i, a) in all.iter().enumerate() {
for b in &all[i + 1..] {
assert_ne!(a.bits(), b.bits());
}
}
}
}