frozen_core/utils.rs
1//! Common utilities used in [`frozen_core`]
2
3/// Returns `true` if `x` is a power of two.
4///
5/// ## Example
6///
7/// ```
8/// use frozen_core::utils::is_power_of_2;
9///
10/// assert!(is_power_of_2(1u16));
11/// assert!(is_power_of_2(2u32));
12/// assert!(is_power_of_2(4u64));
13/// assert!(is_power_of_2(1024usize));
14///
15/// assert!(!is_power_of_2(0u16));
16/// assert!(!is_power_of_2(3u32));
17/// assert!(!is_power_of_2(5u64));
18/// assert!(!is_power_of_2(1023usize));
19/// ```
20#[inline]
21pub fn is_power_of_2<T>(x: T) -> bool
22where
23 T: Copy + Eq + From<u8> + core::ops::Sub<Output = T> + core::ops::BitAnd<Output = T>,
24{
25 let zero = T::from(0);
26 x != zero && (x & (x - T::from(1))) == zero
27}
28
29/// Size of a single IO buffer.
30///
31/// All variants are powers of two, which enables efficient alignment, masking, indexing, and allocator-friendly
32/// memory layouts throughout the storage engine.
33///
34/// The value of each variant is the buffer size in bytes.
35///
36/// *NOTE:* The [`BufferSize::S16384`] is an upper limit for available buffer sizes, as it is large enough to never
37/// create any sort of inconvience for the user, and small enough to never create overflow issues in arithmatic
38/// operation across storage engine/s.
39///
40/// ## Example
41///
42/// ```
43/// let buf_size = frozen_core::utils::BufferSize::S128.bytes();
44/// assert!(frozen_core::utils::is_power_of_2(buf_size));
45/// ```
46#[repr(usize)]
47#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
48pub enum BufferSize {
49 /// 8 bytes
50 S8 = 0x08,
51
52 /// 16 bytes
53 S16 = 0x10,
54
55 /// 32 bytes
56 S32 = 0x20,
57
58 /// 64 bytes
59 S64 = 0x40,
60
61 /// 128 bytes
62 S128 = 0x80,
63
64 /// 256 bytes
65 S256 = 0x100,
66
67 /// 512 bytes
68 S512 = 0x200,
69
70 /// 1 KiB (1024 bytes)
71 S1024 = 0x400,
72
73 /// 2 KiB (2048 bytes)
74 S2048 = 0x800,
75
76 /// 4 KiB (4096 bytes)
77 S4096 = 0x1000,
78
79 /// 8 KiB (8192 bytes)
80 S8192 = 0x2000,
81
82 /// 16 KiB (16384 bytes)
83 S16384 = 0x4000,
84}
85
86impl BufferSize {
87 /// Returns the buffer size in bytes
88 #[inline]
89 pub const fn bytes(self) -> usize {
90 self as usize
91 }
92}