diff options
Diffstat (limited to 'ecs/src/util.rs')
| -rw-r--r-- | ecs/src/util.rs | 155 |
1 files changed, 0 insertions, 155 deletions
diff --git a/ecs/src/util.rs b/ecs/src/util.rs deleted file mode 100644 index 0273b18..0000000 --- a/ecs/src/util.rs +++ /dev/null @@ -1,155 +0,0 @@ -use std::ops::BitAnd; - -pub trait Array<Item>: - AsRef<[Item]> - + AsMut<[Item]> - + IntoIterator<Item = Item> - + Into<Vec<Item>> - + Sortable<Item = Item> - + sealed::Sealed -{ -} - -impl<Item, const CNT: usize> Array<Item> for [Item; CNT] {} - -impl<Item, const CNT: usize> sealed::Sealed for [Item; CNT] {} - -pub trait Sortable -{ - type Item; - - fn sort_by_key_b<Key, Func>(&mut self, func: Func) - where - Func: FnMut(&Self::Item) -> Key, - Key: Ord; -} - -impl<Item> Sortable for [Item] -{ - type Item = Item; - - fn sort_by_key_b<Key, Func>(&mut self, func: Func) - where - Func: FnMut(&Self::Item) -> Key, - Key: Ord, - { - self.sort_by_key(func); - } -} - -impl<Item, const LENGTH: usize> Sortable for [Item; LENGTH] -{ - type Item = Item; - - fn sort_by_key_b<Key, Func>(&mut self, func: Func) - where - Func: FnMut(&Self::Item) -> Key, - Key: Ord, - { - self.sort_by_key(func); - } -} - -impl<Item> Sortable for Vec<Item> -{ - type Item = Item; - - fn sort_by_key_b<Key, Func>(&mut self, func: Func) - where - Func: FnMut(&Self::Item) -> Key, - Key: Ord, - { - self.sort_by_key(func); - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct BitMask<Value> -{ - mask: Value, -} - -impl BitMask<u64> -{ - #[must_use] - pub const fn new(mask: u64) -> Self - { - Self { mask } - } - - pub const fn value(self) -> u64 - { - self.mask - } - - /// Prepares a bitfield value in the range of bits specified by this `BitMask`. - #[must_use] - pub const fn field_prep(self, field_value: u64) -> u64 - { - ((field_value) << self.mask.trailing_zeros()) & (self.mask) - } -} - -impl BitAnd<u64> for BitMask<u64> -{ - type Output = u64; - - fn bitand(self, rhs: u64) -> Self::Output - { - self.mask & rhs - } -} - -pub trait NumberExt: Sized -{ - /// Returns a range of bits (field) specified by the provided [`BitMask`]. - fn field_get(self, field_mask: BitMask<Self>) -> Self; -} - -impl NumberExt for u64 -{ - fn field_get(self, field_mask: BitMask<Self>) -> Self - { - (field_mask & self) >> field_mask.value().trailing_zeros() - } -} - -macro_rules! gen_mask_64 { - ($low: literal..=$high: literal) => { - const { - if $high <= $low { - panic!("High bit index cannot be less than or equal to low bit index"); - } - - (((!0u64) - (1u64 << ($low)) + 1) - & (!0u64 >> (u64::BITS as u64 - 1 - ($high)))) - } - }; -} - -pub(crate) use gen_mask_64; - -mod sealed -{ - pub trait Sealed {} -} - -#[cfg(test)] -mod tests -{ - - use super::BitMask; - use crate::util::NumberExt; - - #[test] - fn field_get_works() - { - assert_eq!(0b11011u64.field_get(BitMask::new(0b11100)), 0b00110); - } - - #[test] - fn bitmask_field_prep_works() - { - assert_eq!(BitMask::new(0b11000).field_prep(3), 0b11000); - } -} |
