Skip to main content

devela/sys/mem/size/bit/
impls.rs

1// devela::sys::mem::size::bit::impls
2//
3// TOC
4// - imports
5// - macro _impl_bit_sized!
6// - trait impls
7
8#[cfg(feature = "grapheme")]
9use crate::GraphemeNonul;
10#[cfg(feature = "term")]
11use crate::{Ansi, AnsiColor, AnsiColor3, AnsiColor8};
12#[cfg(feature = "std")]
13use crate::{Arc, HashMap, HashSet, Mutex, Rc, SystemInstant, SystemTime};
14use crate::{
15    BareBox, BitSized, CharAscii, Duration, Infallible, NonZeroI8, NonZeroI16, NonZeroI32,
16    NonZeroI64, NonZeroI128, NonZeroIsize, NonZeroU8, NonZeroU16, NonZeroU32, NonZeroU64,
17    NonZeroU128, NonZeroUsize, Ordering, PhantomData, PhantomPinned, StringNonul,
18};
19
20// WAIT: [generic_const_exprs](https://github.com/rust-lang/rust/issues/76560#issuecomment-1202124275)
21// use crate::{StringU16, StringU32, GraphemeU8, StringU8};
22#[cfg(all(feature = "alloc", feature = "grapheme"))]
23use crate::GraphemeString;
24#[cfg(feature = "alloc")]
25use crate::{BTreeMap, BTreeSet, BinaryHeap, LinkedList, String, Vec, VecDeque};
26
27#[cfg(feature = "dep_portable_atomic")]
28use crate::_dep::portable_atomic::{AtomicF32, AtomicF64, AtomicI128, AtomicU128};
29#[cfg(feature = "work")]
30use crate::{AtomicBool, AtomicOrdering};
31#[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "8")))]
32use crate::{AtomicI8, AtomicU8};
33#[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "16")))]
34use crate::{AtomicI16, AtomicU16};
35#[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "32")))]
36use crate::{AtomicI32, AtomicU32};
37#[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "64")))]
38use crate::{AtomicI64, AtomicU64};
39#[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "ptr")))]
40use crate::{AtomicIsize, AtomicPtr, AtomicUsize};
41
42/// Helper macro to implement [`BitSized`].
43macro_rules! _impl_bit_sized {
44    /* primitives */
45
46    (= $bits:expr; for $($t:ty),+) => { $( impl $crate::BitSized<$bits> for $t {} )+ };
47
48    /* primitives generic on $T */
49
50    (<$T:ident> = $bits:expr; for $($t:ty),+) => {
51        $( impl<$T> $crate::BitSized<$bits> for $t {} )+
52    };
53    (<const $T:ident: $Tt:ty> = $bits:expr; for $($t:ty),+) => {
54        $( impl<const $T: $Tt> $crate::BitSized<$bits> for $t {} )+
55    };
56
57    /* primitives generic on $K, $V */
58
59    (<$K:ident, $V:ident> = $bits:expr; for $($t:ty),+) => {
60        $( impl<$K, $V> $crate::BitSized<$bits> for $t {} )+
61    };
62    (<const $K:ident: $Kt:ty, const $V:ident: $Vt:ty> = $bits:expr; for $($t:ty),+) => {
63        $( impl<const $K: $Kt, const $V: $Vt> $crate::BitSized<$bits> for $t {} )+
64    };
65
66    /* pointer primitives */
67
68    // implements BitSized<$PTR_BITS> for pointer-sized related types.
69    (pointer = $PTR_BITS:literal) => {
70        _impl_bit_sized![= $PTR_BITS; for isize, usize];
71
72        _impl_bit_sized![= $PTR_BITS; for NonZeroIsize, NonZeroUsize];
73
74        #[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "ptr")))]
75        _impl_bit_sized![= $PTR_BITS; for AtomicIsize, AtomicUsize];
76        #[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "ptr")))]
77        _impl_bit_sized![<T> = $PTR_BITS; for AtomicPtr<T>];
78
79        #[cfg(feature = "std")]
80        _impl_bit_sized![<T> = {$PTR_BITS * 1}; for Rc<T>, Arc<T>];
81
82        _impl_bit_sized![= {$PTR_BITS * 2}; for &str];
83        _impl_bit_sized![<T> = {$PTR_BITS * 2}; for &[T], &mut [T]];
84
85        #[cfg(feature = "alloc")]
86        _impl_bit_sized![= {$PTR_BITS * 3}; for String];
87
88        #[cfg(all(feature = "alloc", feature = "grapheme"))]
89        _impl_bit_sized![= {$PTR_BITS * 3}; for GraphemeString];
90
91        #[cfg(feature = "alloc")]
92        _impl_bit_sized![<T> = {$PTR_BITS * 3};
93            for Vec<T>, LinkedList<T>, VecDeque<T>, BTreeSet<T>, BinaryHeap<T>];
94        #[cfg(feature = "std")]
95        _impl_bit_sized![<T> = {$PTR_BITS * 3}; for HashSet<T>, Mutex<T>];
96
97        // K, V
98        #[cfg(feature = "alloc")]
99        _impl_bit_sized![<K, V> = {$PTR_BITS * 3}; for BTreeMap<K, V>];
100        #[cfg(feature = "std")]
101        _impl_bit_sized![<K, V> = {$PTR_BITS * 3}; for HashMap<K, V>];
102    };
103
104    /* arrays */
105
106    (array = $bits:literal * len for T: $tsize:literal * len: $($len:literal),+) => {
107        $(
108        impl<T: $crate::BitSized<$tsize>> $crate::BitSized<{$bits*$len}> for [T; $len] {}
109        )+
110    };
111}
112
113/* impl BitSized */
114
115impl<T: BitSized<LEN>, const LEN: usize> BitSized<LEN> for BareBox<T> {}
116
117_impl_bit_sized![<T> = 0; for PhantomData<T>];
118_impl_bit_sized![= 0; for (), Infallible, PhantomPinned];
119_impl_bit_sized![= 1; for bool];
120_impl_bit_sized![= 8; for i8, u8, Ordering];
121_impl_bit_sized![= 16; for i16, u16];
122_impl_bit_sized![= 32; for i32, u32, f32, char];
123_impl_bit_sized![= 64; for i64, u64, f64];
124_impl_bit_sized![= 128; for i128, u128, Duration];
125#[cfg(feature = "std")]
126_impl_bit_sized![= 128; for SystemInstant, SystemTime];
127
128_impl_bit_sized![= 7; for crate::char7];
129_impl_bit_sized![= 8; for crate::char8];
130_impl_bit_sized![= 16; for crate::char16];
131
132_impl_bit_sized![= 8; for NonZeroI8, NonZeroU8];
133_impl_bit_sized![= 16; for NonZeroI16, NonZeroU16];
134_impl_bit_sized![= 32; for NonZeroI32, NonZeroU32];
135_impl_bit_sized![= 64; for NonZeroI64, NonZeroU64];
136_impl_bit_sized![= 128; for NonZeroI128, NonZeroU128];
137
138// NOTE: NonValue* types have their implementation in /src/num/grain/niche/impls.rs
139
140#[cfg(feature = "work")]
141_impl_bit_sized![= 1; for AtomicBool];
142#[cfg(feature = "work")]
143_impl_bit_sized![= 8; for AtomicOrdering];
144#[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "8")))]
145_impl_bit_sized![= 8; for AtomicI8, AtomicU8];
146#[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "16")))]
147_impl_bit_sized![= 16; for AtomicI16, AtomicU16];
148#[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "32")))]
149_impl_bit_sized![= 32; for AtomicI32, AtomicU32];
150#[cfg(all(feature = "work", any(feature = "dep_portable_atomic", target_has_atomic = "64")))]
151_impl_bit_sized![= 64; for AtomicI64, AtomicU64];
152#[cfg(feature = "dep_portable_atomic")]
153_impl_bit_sized![= 32; for AtomicF32];
154#[cfg(feature = "dep_portable_atomic")]
155_impl_bit_sized![= 64; for AtomicF64];
156#[cfg(feature = "dep_portable_atomic")]
157_impl_bit_sized![= 128; for AtomicI128, AtomicU128];
158
159_impl_bit_sized![= 7; for CharAscii];
160#[cfg(feature = "grapheme")]
161_impl_bit_sized![<const LEN: usize> = LEN; for GraphemeNonul<LEN>];
162_impl_bit_sized![<const LEN: usize> = LEN; for StringNonul<LEN>];
163// WAIT: [generic_const_exprs](https://github.com/rust-lang/rust/issues/76560#issuecomment-1202124275)
164// _impl_bit_sized![<const LEN: usize> = { LEN + 8 }; for StringU8<LEN>, GraphemeU8<LEN>];
165// _impl_bit_sized![<const LEN: usize> = { LEN + 16 }; for StringU16<LEN>];
166// _impl_bit_sized![<const LEN: usize> = { LEN + 32 }; for StringU32<LEN>];
167
168#[cfg(target_pointer_width = "8")]
169_impl_bit_sized![pointer = 8];
170#[cfg(target_pointer_width = "16")]
171_impl_bit_sized![pointer = 16];
172#[cfg(target_pointer_width = "32")]
173_impl_bit_sized![pointer = 32];
174#[cfg(target_pointer_width = "64")]
175_impl_bit_sized![pointer = 64];
176#[cfg(target_pointer_width = "128")]
177_impl_bit_sized![pointer = 128];
178
179// THINK: IMPROVE use const generics?
180_impl_bit_sized![array = 8 * len for T: 8 * len: 1, 2, 4, 8, 16];
181_impl_bit_sized![array = 16 * len for T: 16 * len: 1, 2, 4, 8, 16];
182_impl_bit_sized![array = 24 * len for T: 24 * len: 1, 2, 4, 8, 16]; // *
183_impl_bit_sized![array = 32 * len for T: 32 * len: 1, 2, 4, 8, 16];
184_impl_bit_sized![array = 40 * len for T: 40 * len: 1, 2, 4, 8, 16]; // *
185_impl_bit_sized![array = 48 * len for T: 48 * len: 1, 2, 4, 8, 16]; // *
186_impl_bit_sized![array = 56 * len for T: 56 * len: 1, 2, 4, 8, 16]; // *
187_impl_bit_sized![array = 64 * len for T: 64 * len: 1, 2, 4, 8, 16];
188_impl_bit_sized![array = 72 * len for T: 72 * len: 1, 2, 4, 8, 16]; // *
189_impl_bit_sized![array = 80 * len for T: 80 * len: 1, 2, 4, 8, 16]; // *
190_impl_bit_sized![array = 88 * len for T: 88 * len: 1, 2, 4, 8, 16]; // *
191_impl_bit_sized![array = 96 * len for T: 96 * len: 1, 2, 4, 8, 16]; // *
192_impl_bit_sized![array = 104 * len for T: 104 * len: 1, 2, 4, 8, 16]; // *
193_impl_bit_sized![array = 112 * len for T: 112 * len: 1, 2, 4, 8, 16]; // *
194_impl_bit_sized![array = 120 * len for T: 120 * len: 1, 2, 4, 8, 16]; // *
195_impl_bit_sized![array = 128 * len for T: 128 * len: 1, 2, 4, 8, 16];
196
197#[cfg(feature = "term")]
198crate::items! {
199    _impl_bit_sized![= 0; for Ansi];
200    _impl_bit_sized![= 3; for AnsiColor3];
201    _impl_bit_sized![= 8; for AnsiColor8];
202    _impl_bit_sized![= 24; for AnsiColor]; // NOTE: 24 as union, 32 with discriminant
203}