Skip to content

Commit cfe3000

Browse files
authored
Merge pull request #50 from sile/remove-bitflags
Remove bitflags dependency and replace with custom implementation
2 parents c624487 + 5d0e8e6 commit cfe3000

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ edition = "2024"
1313
rust-version = "1.85.0"
1414

1515
[dependencies]
16-
bitflags = { version = "2", default-features = false }
1716
serde = { version = "1", optional = true }
1817

1918
[dev-dependencies]

src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,9 @@
2323
//! assert_eq!(map.get("baz"), Some(&3));
2424
//! ```
2525
#![warn(missing_docs)]
26-
#![allow(clippy::cast_ptr_alignment)]
26+
#![expect(clippy::cast_ptr_alignment)]
2727
#![cfg_attr(not(feature = "std"), no_std)]
2828

29-
#[macro_use]
30-
extern crate bitflags;
31-
#[cfg(test)]
32-
extern crate rand;
33-
3429
#[macro_use]
3530
extern crate alloc;
3631

src/node.rs

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,57 @@ macro_rules! assert_some {
1515
};
1616
}
1717

18-
bitflags! {
19-
#[derive(Clone, Copy)]
20-
pub (crate) struct Flags: u8 {
21-
const VALUE_ALLOCATED = 0b0000_0001;
22-
const VALUE_INITIALIZED = 0b0000_0010;
18+
#[derive(Debug, Clone, Copy)]
19+
pub(crate) struct Flags(u8);
2320

24-
const CHILD_ALLOCATED = 0b0000_0100;
25-
const CHILD_INITIALIZED = 0b0000_1000;
21+
impl Flags {
22+
pub(crate) const VALUE_ALLOCATED: Flags = Flags(0b0000_0001);
23+
pub(crate) const VALUE_INITIALIZED: Flags = Flags(0b0000_0010);
24+
pub(crate) const CHILD_ALLOCATED: Flags = Flags(0b0000_0100);
25+
pub(crate) const CHILD_INITIALIZED: Flags = Flags(0b0000_1000);
26+
pub(crate) const SIBLING_ALLOCATED: Flags = Flags(0b0001_0000);
27+
pub(crate) const SIBLING_INITIALIZED: Flags = Flags(0b0010_0000);
2628

27-
const SIBLING_ALLOCATED = 0b0001_0000;
28-
const SIBLING_INITIALIZED = 0b0010_0000;
29+
const VALID_BITS_MASK: u8 = 0b0011_1111; // Mask of all valid flag bits.
30+
31+
const fn empty() -> Self {
32+
Flags(0)
33+
}
34+
35+
pub(crate) const fn from_bits_truncate(bits: u8) -> Self {
36+
Flags(bits & Self::VALID_BITS_MASK)
37+
}
38+
39+
pub(crate) const fn bits(self) -> u8 {
40+
self.0
41+
}
42+
43+
pub(crate) const fn contains(self, other: Flags) -> bool {
44+
(self.0 & other.0) == other.0
45+
}
46+
47+
const fn intersects(self, other: Flags) -> bool {
48+
(self.0 & other.0) != 0
49+
}
50+
51+
fn insert(&mut self, other: Flags) {
52+
self.0 |= other.0;
53+
}
54+
55+
fn set(&mut self, other: Flags, value: bool) {
56+
if value {
57+
self.0 |= other.0;
58+
} else {
59+
self.0 &= !other.0;
60+
}
61+
}
62+
}
63+
64+
impl core::ops::BitOr for Flags {
65+
type Output = Self;
66+
67+
fn bitor(self, other: Self) -> Self {
68+
Flags(self.0 | other.0)
2969
}
3070
}
3171

0 commit comments

Comments
 (0)