forked from ordinals/ord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use enums for runestone tags and flags (ordinals#2956)
- Loading branch information
Showing
5 changed files
with
367 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
pub(super) enum Flag { | ||
Etch = 0, | ||
#[allow(unused)] | ||
Burn = 127, | ||
} | ||
|
||
impl Flag { | ||
pub(super) fn mask(self) -> u128 { | ||
1 << self as u128 | ||
} | ||
|
||
pub(super) fn take(self, flags: &mut u128) -> bool { | ||
let mask = self.mask(); | ||
let set = *flags & mask != 0; | ||
*flags &= !mask; | ||
set | ||
} | ||
|
||
pub(super) fn set(self, flags: &mut u128) { | ||
*flags |= self.mask() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn mask() { | ||
assert_eq!(Flag::Etch.mask(), 0b1); | ||
assert_eq!(Flag::Burn.mask(), 1 << 127); | ||
} | ||
|
||
#[test] | ||
fn take() { | ||
let mut flags = 1; | ||
assert!(Flag::Etch.take(&mut flags)); | ||
assert_eq!(flags, 0); | ||
|
||
let mut flags = 0; | ||
assert!(!Flag::Etch.take(&mut flags)); | ||
assert_eq!(flags, 0); | ||
} | ||
|
||
#[test] | ||
fn set() { | ||
let mut flags = 0; | ||
Flag::Etch.set(&mut flags); | ||
assert_eq!(flags, 1); | ||
} | ||
} |
Oops, something went wrong.