rust_extra/powersOfTwo/
PowerOfTwoSixteenBit.rs1#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6#[repr(u16)]
7pub enum PowerOfTwoSixteenBit
8{
9 _1 = 1,
10 _2 = 2,
11 _4 = 4,
12 _8 = 8,
13 _16 = 16,
14 _32 = 32,
15 _64 = 64,
16 _128 = 128,
17 _256 = 256,
18 _512 = 512,
19 _1024 = 1024,
20 _2048 = 2048,
21 _4096 = 4096,
22 _8192 = 8192,
23 _16384 = 16384,
24 _32768 = 32768,
25}
26
27impl PowerOfTwoSixteenBit
28{
29 #[inline(always)]
31 pub fn isPowerOfTwo(value: u16) -> bool
32 {
33 debug_assert!(value != 0, "zero is not a valid power of two");
34
35 (value & (value - 1)) == 0
36 }
37
38 #[inline(always)]
39 pub unsafe fn from_u16_unchecked(value: u16) -> PowerOfTwoSixteenBit
40 {
41 debug_assert!(value != 0, "zero is not a valid power of two");
42
43 transmute(value)
44 }
45
46 #[inline(always)]
47 pub unsafe fn from_u16_panic(value: u16) -> PowerOfTwoSixteenBit
48 {
49 match value
50 {
51 0 => panic!("Zero is not converted"),
52
53 1 => PowerOfTwoSixteenBit::_1,
54 2 => PowerOfTwoSixteenBit::_2,
55 4 => PowerOfTwoSixteenBit::_4,
56 8 => PowerOfTwoSixteenBit::_8,
57 16 => PowerOfTwoSixteenBit::_16,
58 32 => PowerOfTwoSixteenBit::_32,
59 64 => PowerOfTwoSixteenBit::_64,
60 128 => PowerOfTwoSixteenBit::_128,
61 256 => PowerOfTwoSixteenBit::_256,
62 512 => PowerOfTwoSixteenBit::_512,
63 1024 => PowerOfTwoSixteenBit::_1024,
64 2048 => PowerOfTwoSixteenBit::_2048,
65 4096 => PowerOfTwoSixteenBit::_4096,
66 8192 => PowerOfTwoSixteenBit::_8192,
67 16384 => PowerOfTwoSixteenBit::_16384,
68 32768 => PowerOfTwoSixteenBit::_32768,
69
70 _ => panic!("The value '{}' is not a power of two", value),
71 }
72 }
73
74 #[inline(always)]
75 pub unsafe fn from_u16_panic_withZeroAs<F>(value: u16, zeroIs: F) -> PowerOfTwoSixteenBit
76 where F: Fn() -> PowerOfTwoSixteenBit
77 {
78 match value
79 {
80 0 => zeroIs(),
81
82 1 => PowerOfTwoSixteenBit::_1,
83 2 => PowerOfTwoSixteenBit::_2,
84 4 => PowerOfTwoSixteenBit::_4,
85 8 => PowerOfTwoSixteenBit::_8,
86 16 => PowerOfTwoSixteenBit::_16,
87 32 => PowerOfTwoSixteenBit::_32,
88 64 => PowerOfTwoSixteenBit::_64,
89 128 => PowerOfTwoSixteenBit::_128,
90 256 => PowerOfTwoSixteenBit::_256,
91 512 => PowerOfTwoSixteenBit::_512,
92 1024 => PowerOfTwoSixteenBit::_1024,
93 2048 => PowerOfTwoSixteenBit::_2048,
94 4096 => PowerOfTwoSixteenBit::_4096,
95 8192 => PowerOfTwoSixteenBit::_8192,
96 16384 => PowerOfTwoSixteenBit::_16384,
97 32768 => PowerOfTwoSixteenBit::_32768,
98
99 _ => panic!("The value '{}' is not a power of two", value),
100 }
101 }
102
103 #[inline(always)]
104 pub unsafe fn from_u16(value: u16) -> Option<PowerOfTwoSixteenBit>
105 {
106 match value
107 {
108 0 => None,
109
110 1 => Some(PowerOfTwoSixteenBit::_1),
111 2 => Some(PowerOfTwoSixteenBit::_2),
112 4 => Some(PowerOfTwoSixteenBit::_4),
113 8 => Some(PowerOfTwoSixteenBit::_8),
114 16 => Some(PowerOfTwoSixteenBit::_16),
115 32 => Some(PowerOfTwoSixteenBit::_32),
116 64 => Some(PowerOfTwoSixteenBit::_64),
117 128 => Some(PowerOfTwoSixteenBit::_128),
118 256 => Some(PowerOfTwoSixteenBit::_256),
119 512 => Some(PowerOfTwoSixteenBit::_512),
120 1024 => Some(PowerOfTwoSixteenBit::_1024),
121 2048 => Some(PowerOfTwoSixteenBit::_2048),
122 4096 => Some(PowerOfTwoSixteenBit::_4096),
123 8192 => Some(PowerOfTwoSixteenBit::_8192),
124 16384 => Some(PowerOfTwoSixteenBit::_16384),
125 32768 => Some(PowerOfTwoSixteenBit::_32768),
126
127 _ => None,
128 }
129 }
130}