mysql_handler/hton/flags.rs
1// Copyright (C) 2026 ren-yamanashi
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of this program hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, see <https://www.gnu.org/licenses/>.
22
23//! Engine-level `handlerton` flags (the `HTON_*` bits).
24
25use crate::sys;
26
27/// A set of `handlerton` flags (the `HTON_*` bits from `sql/handler.h`).
28///
29/// Returned from [`Handlerton::flags`](crate::hton::Handlerton::flags). The
30/// zero-config engine sets [`HtonFlags::CAN_RECREATE`], which is therefore the
31/// trait default; return [`HtonFlags::NONE`] to clear it.
32///
33/// ```
34/// use mysql_handler::hton::HtonFlags;
35///
36/// let f = HtonFlags::NONE | HtonFlags::CAN_RECREATE;
37/// assert!(f.contains(HtonFlags::CAN_RECREATE));
38/// ```
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
40#[non_exhaustive]
41pub struct HtonFlags(u32);
42
43impl HtonFlags {
44 /// No flags set (`HTON_NO_FLAGS`)
45 pub const NONE: Self = Self(0);
46 /// `HTON_CAN_RECREATE`: the engine implements `TRUNCATE` by recreating the
47 /// table. The flag the zero-config engine sets today.
48 pub const CAN_RECREATE: Self = Self(sys::HTON_CAN_RECREATE);
49
50 /// An empty flag set
51 #[must_use]
52 pub const fn empty() -> Self {
53 Self(0)
54 }
55
56 /// The raw bits, for handing the set across the FFI boundary
57 #[must_use]
58 pub const fn bits(self) -> u32 {
59 self.0
60 }
61
62 /// Whether every flag in `other` is set in `self`
63 #[must_use]
64 pub const fn contains(self, other: Self) -> bool {
65 self.0 & other.0 == other.0
66 }
67
68 /// The union of two flag sets
69 #[must_use]
70 pub const fn union(self, other: Self) -> Self {
71 Self(self.0 | other.0)
72 }
73}
74
75impl core::ops::BitOr for HtonFlags {
76 type Output = Self;
77
78 fn bitor(self, rhs: Self) -> Self {
79 self.union(rhs)
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn can_recreate_matches_sys_constant() {
89 assert_eq!(HtonFlags::CAN_RECREATE.bits(), sys::HTON_CAN_RECREATE);
90 }
91
92 #[test]
93 fn none_is_empty() {
94 assert_eq!(HtonFlags::NONE, HtonFlags::empty());
95 assert_eq!(HtonFlags::empty().bits(), 0);
96 }
97
98 #[test]
99 fn union_and_contains() {
100 let f = HtonFlags::NONE | HtonFlags::CAN_RECREATE;
101 assert!(f.contains(HtonFlags::CAN_RECREATE));
102 assert!(HtonFlags::NONE.contains(HtonFlags::NONE));
103 assert!(!HtonFlags::NONE.contains(HtonFlags::CAN_RECREATE));
104 }
105}