diff options
author | superwhiskers <[email protected]> | 2025-07-26 23:02:34 -0400 |
---|---|---|
committer | superwhiskers <[email protected]> | 2025-09-15 10:55:06 -0500 |
commit | 386279ce28a54002fa91f436d5b60815c537e910 (patch) | |
tree | 26515281a22c13780db13c977b585f02d86c6df6 /crates/core/src/hive/skipfield.rs | |
download | azimuth-386279ce28a54002fa91f436d5b60815c537e910.tar.gz azimuth-386279ce28a54002fa91f436d5b60815c537e910.tar.bz2 azimuth-386279ce28a54002fa91f436d5b60815c537e910.zip |
initial commit
Change-Id: I6a6a69640e8a301a692a5455d1261816280d9cde
Diffstat (limited to 'crates/core/src/hive/skipfield.rs')
-rw-r--r-- | crates/core/src/hive/skipfield.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/crates/core/src/hive/skipfield.rs b/crates/core/src/hive/skipfield.rs new file mode 100644 index 0000000..0fb2ab4 --- /dev/null +++ b/crates/core/src/hive/skipfield.rs @@ -0,0 +1,60 @@ +//! An implementation of a trait describing valid skipfield integer types. +//! +//! Only `u16`s and `u8`s are supported by default. However, other integral +//! types may be used by implementing this trait on them; the trait is not +//! sealed. + +use core::ops::{Add, AddAssign, Sub, SubAssign}; + +/// Trait describing integral types in a generic way suitable for use as the +/// element type of a skipfield. +pub trait SkipfieldType: + Add + AddAssign + Sub + SubAssign + Ord + PartialOrd + Copy + Sized +{ + /// The maximum attainable value of this type. + const MAXIMUM: Self; + + /// The zero element of this type. + const ZERO: Self; + + /// The one element of this type. + const ONE: Self; + + /// Conversion method from `usize` using `as` or an equivalent + fn from_usize(u: usize) -> Self; + + /// Conversion method from `isize` using `as` or an equivalent + fn from_isize(i: isize) -> Self; +} + +impl SkipfieldType for u16 { + const MAXIMUM: Self = u16::MAX; + const ZERO: Self = 0; + const ONE: Self = 1; + + #[inline(always)] + fn from_usize(u: usize) -> Self { + u as u16 + } + + #[inline(always)] + fn from_isize(i: isize) -> Self { + i as u16 + } +} + +impl SkipfieldType for u8 { + const MAXIMUM: Self = u8::MAX; + const ZERO: Self = 0; + const ONE: Self = 1; + + #[inline(always)] + fn from_usize(u: usize) -> Self { + u as u8 + } + + #[inline(always)] + fn from_isize(i: isize) -> Self { + i as u8 + } +} |