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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
/// Removes the trailing zeroes in the payload
///
/// # Note:
///
/// There must always be at least one remaining byte even if it is a
/// zero byte.
pub fn remove_trailing_zeroes(data: &[u8]) -> usize {
let mut len = data.len();
for b in data[1..].iter().rev() {
if *b != 0 {
break;
}
len -= 1;
}
len
}
/// A trait very similar to `Default` but is only implemented for the equivalent Rust types to
/// `MavType`s.
///
/// This is only needed because rust doesn't currently implement `Default` for arrays
/// of all sizes. In particular this trait is only ever used when the "serde" feature is enabled.
/// For more information, check out [this issue](https://users.rust-lang.org/t/issue-for-derives-for-arrays-greater-than-size-32/59055/3).
pub trait RustDefault: Copy {
fn rust_default() -> Self;
}
impl<T: RustDefault, const N: usize> RustDefault for [T; N] {
#[inline(always)]
fn rust_default() -> Self {
let val: T = RustDefault::rust_default();
[val; N]
}
}
impl RustDefault for u8 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}
impl RustDefault for i8 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}
impl RustDefault for u16 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}
impl RustDefault for i16 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}
impl RustDefault for u32 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}
impl RustDefault for i32 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}
impl RustDefault for u64 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}
impl RustDefault for i64 {
#[inline(always)]
fn rust_default() -> Self {
0
}
}
impl RustDefault for char {
#[inline(always)]
fn rust_default() -> Self {
'\0'
}
}
impl RustDefault for f32 {
#[inline(always)]
fn rust_default() -> Self {
0.0
}
}
impl RustDefault for f64 {
#[inline(always)]
fn rust_default() -> Self {
0.0
}
}