bytevec/impls/mod.rs
1mod collections;
2mod primitives;
3
4/// Represents the generic integral type of the structure size indicators
5pub trait BVSize: Sized {
6 /// Returns a `Self` value casted from an `usize` value
7 fn from_usize(val: usize) -> Self;
8 /// Returns an `usize` value casted from a `Self` value
9 fn as_usize(&self) -> usize;
10 /// Returns the max value for `Self`
11 fn max_value() -> Self;
12 /// Calls the `checked_add` method of `self` passing `rhs`
13 fn checked_add(self, rhs: Self) -> Option<Self>;
14 /// Returns the returned value of [`std::mem::size_of`][1] for `Self`
15 /// [1]: http://doc.rust-lang.org/stable/std/mem/fn.size_of.html
16 fn get_size_of() -> Self;
17}
18
19macro_rules! def_BVSize {
20 ($($t:ty),*) => {
21 $(
22 impl BVSize for $t {
23 fn from_usize(val: usize) -> $t {
24 val as $t
25 }
26
27 fn as_usize(&self) -> usize {
28 *self as usize
29 }
30
31 fn max_value() -> Self {
32 <$t>::max_value()
33 }
34
35 fn checked_add(self, rhs: Self) -> Option<$t> {
36 self.checked_add(rhs)
37 }
38
39 fn get_size_of() -> Self {
40 <$t>::from_usize(::std::mem::size_of::<$t>())
41 }
42 }
43 )*
44 }
45}
46
47def_BVSize!(u8, u16, u32, u64);