blob: 16a3dcbdfb268a7b679d8307a5af2f7baa8f90d2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
//! An implementation of a trait describing valid skipfield integer types.
//!
//! Only `u16`s and `u8`s are supported by default. However, other integral
//! types may be used by implementing this trait on them; the trait is not
//! sealed.
use core::{
cmp,
ops::{Add, AddAssign, Sub, SubAssign},
};
/// Trait describing integral types in a generic way suitable for use as the
/// element type of a skipfield.
pub trait SkipfieldType:
Add + AddAssign + Sub + SubAssign + Ord + PartialOrd + Copy + Sized
{
/// The maximum attainable value of this type.
const MAXIMUM: Self;
/// The zero element of this type.
const ZERO: Self;
/// The one element of this type.
const ONE: Self;
/// Conversion method from `usize` using `as` or an equivalent
///
/// Caps the value of the input by the maximum of `Self`.
fn from_usize(u: usize) -> Self;
/// Conversion method from `isize` using `as` or an equivalent
///
/// Caps the value of the input by the maximum of `Self`.
fn from_isize(i: isize) -> Self;
}
impl SkipfieldType for u16 {
const MAXIMUM: Self = u16::MAX;
const ZERO: Self = 0;
const ONE: Self = 1;
#[inline(always)]
fn from_usize(u: usize) -> Self {
cmp::min(u, Self::MAXIMUM as usize) as u16
}
#[inline(always)]
fn from_isize(i: isize) -> Self {
cmp::min(i, Self::MAXIMUM as isize) as u16
}
}
impl SkipfieldType for u8 {
const MAXIMUM: Self = u8::MAX;
const ZERO: Self = 0;
const ONE: Self = 1;
#[inline(always)]
fn from_usize(u: usize) -> Self {
cmp::min(u, Self::MAXIMUM as usize) as u8
}
#[inline(always)]
fn from_isize(i: isize) -> Self {
cmp::min(i, Self::MAXIMUM as isize) as u8
}
}
|