• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kaidokert / fixed-bigint-rs / 21933903310

12 Feb 2026 04:45AM UTC coverage: 94.407% (+0.7%) from 93.728%
21933903310

push

github

web-flow
Refactor for clippy cleanup (#57)

* Refactor for clippy cleanup

1 of 1 new or added line in 1 file covered. (100.0%)

2 existing lines in 1 file now uncovered.

1823 of 1931 relevant lines covered (94.41%)

1436.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.72
/src/fixeduint/to_from_bytes.rs
1
use super::MachineWord;
2

3
use core::borrow::{Borrow, BorrowMut};
4

5
use core::hash::Hash;
6

7
use super::FixedUInt;
8

9
// Helper, holds an owned copy of returned bytes
10
#[derive(Eq, PartialEq, Clone, Copy, PartialOrd, Ord, Debug)]
11
pub struct BytesHolder<T: MachineWord, const N: usize> {
12
    array: [T; N],
13
}
14

15
impl<T: MachineWord, const N: usize> Default for BytesHolder<T, N> {
16
    fn default() -> Self {
3✔
17
        Self {
18
            array: core::array::from_fn(|_| T::default()),
7✔
19
        }
20
    }
3✔
21
}
22

23
impl<T: MachineWord, const N: usize> BytesHolder<T, N> {
24
    // Converts internal storage to a mutable byte slice
25
    fn as_byte_slice_mut(&mut self) -> &mut [u8] {
12✔
26
        // SAFETY: This is safe because the size of the array is the same as the size of the slice
27
        unsafe {
12✔
28
            core::slice::from_raw_parts_mut(
12✔
29
                &mut self.array as *mut T as *mut u8,
12✔
30
                N * core::mem::size_of::<T>(),
12✔
31
            )
12✔
32
        }
12✔
33
    }
12✔
34
    fn as_byte_slice(&self) -> &[u8] {
12✔
35
        // SAFETY: This is safe because the size of the array is the same as the size of the slice
36
        unsafe {
37
            core::slice::from_raw_parts(
12✔
38
                &self.array as *const T as *const u8,
12✔
39
                N * core::mem::size_of::<T>(),
12✔
40
            )
12✔
41
        }
42
    }
12✔
43
}
44

45
impl<T: MachineWord, const N: usize> Borrow<[u8]> for BytesHolder<T, N> {
UNCOV
46
    fn borrow(&self) -> &[u8] {
×
47
        self.as_byte_slice()
×
48
    }
×
49
}
50
impl<T: MachineWord, const N: usize> BorrowMut<[u8]> for BytesHolder<T, N> {
UNCOV
51
    fn borrow_mut(&mut self) -> &mut [u8] {
×
52
        self.as_byte_slice_mut()
×
53
    }
×
54
}
55
impl<T: MachineWord, const N: usize> AsRef<[u8]> for BytesHolder<T, N> {
56
    fn as_ref(&self) -> &[u8] {
12✔
57
        self.as_byte_slice()
12✔
58
    }
12✔
59
}
60
impl<T: MachineWord, const N: usize> AsMut<[u8]> for BytesHolder<T, N> {
61
    fn as_mut(&mut self) -> &mut [u8] {
6✔
62
        self.as_byte_slice_mut()
6✔
63
    }
6✔
64
}
65
impl<T: MachineWord, const N: usize> Hash for BytesHolder<T, N> {
66
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
×
67
        self.array.hash(state)
×
68
    }
×
69
}
70

71
impl<T: MachineWord, const N: usize> num_traits::ToBytes for FixedUInt<T, N>
72
where
73
    T: core::fmt::Debug,
74
{
75
    type Bytes = BytesHolder<T, N>;
76

77
    fn to_be_bytes(&self) -> Self::Bytes {
3✔
78
        let mut ret = Self::Bytes { array: self.array };
3✔
79
        let _ = self.to_be_bytes(ret.as_byte_slice_mut());
3✔
80
        ret
3✔
81
    }
3✔
82

83
    fn to_le_bytes(&self) -> Self::Bytes {
3✔
84
        let mut ret = Self::Bytes { array: self.array };
3✔
85
        let _ = self.to_le_bytes(ret.as_byte_slice_mut());
3✔
86
        ret
3✔
87
    }
3✔
88
}
89

90
impl<T: MachineWord, const N: usize> num_traits::FromBytes for FixedUInt<T, N>
91
where
92
    T: core::fmt::Debug,
93
{
94
    type Bytes = BytesHolder<T, N>;
95

96
    fn from_be_bytes(bytes: &Self::Bytes) -> Self {
3✔
97
        Self::from_be_bytes(bytes.as_ref())
3✔
98
    }
3✔
99

100
    fn from_le_bytes(bytes: &Self::Bytes) -> Self {
3✔
101
        Self::from_le_bytes(bytes.as_ref())
3✔
102
    }
3✔
103
}
104

105
#[cfg(test)]
106
mod tests {
107
    use super::*;
108
    use num_traits::FromPrimitive;
109

110
    fn test_helper<T: num_traits::ToBytes>(input: &T, expected_be: &[u8]) {
5✔
111
        let mut buffer = [0u8; 256];
5✔
112
        buffer[..expected_be.len()].copy_from_slice(expected_be);
5✔
113
        let expected_le = &mut buffer[..expected_be.len()];
5✔
114
        expected_le.reverse();
5✔
115

116
        let out = input.to_be_bytes();
5✔
117
        assert_eq!(out.as_ref(), expected_be);
5✔
118
        let out = input.to_le_bytes();
5✔
119
        assert_eq!(out.as_ref(), expected_le);
5✔
120
    }
5✔
121

122
    #[test]
123
    fn test_to_bytes() {
1✔
124
        test_helper(&0xAB_u8, &[0xAB_u8]);
1✔
125
        test_helper(&0xABCD_u16, &[0xAB, 0xCD]);
1✔
126
        test_helper(
1✔
127
            &FixedUInt::<u8, 4>::from_u32(0x12345678).unwrap(),
1✔
128
            &[0x12, 0x34, 0x56, 0x78],
1✔
129
        );
130
        test_helper(
1✔
131
            &FixedUInt::<u16, 2>::from_u32(0x12345678).unwrap(),
1✔
132
            &[0x12, 0x34, 0x56, 0x78],
1✔
133
        );
134
        test_helper(
1✔
135
            &FixedUInt::<u32, 1>::from_u32(0x12345678).unwrap(),
1✔
136
            &[0x12, 0x34, 0x56, 0x78],
1✔
137
        );
138
    }
1✔
139

140
    fn from_helper<T>(input: &[u8], expected: T)
6✔
141
    where
6✔
142
        T: num_traits::FromBytes + core::fmt::Debug + core::cmp::PartialEq,
6✔
143
        T::Bytes: num_traits::ops::bytes::NumBytes + Default + core::fmt::Debug,
6✔
144
    {
145
        let mut bytes = T::Bytes::default();
6✔
146
        bytes.as_mut().copy_from_slice(input);
6✔
147
        let result = T::from_be_bytes(&bytes);
6✔
148
        assert_eq!(result, expected);
6✔
149
        bytes.as_mut().reverse();
6✔
150
        let result = T::from_le_bytes(&bytes);
6✔
151
        assert_eq!(result, expected);
6✔
152
    }
6✔
153

154
    #[test]
155
    fn test_from_bytes() {
1✔
156
        from_helper(&[0xAB_u8], 0xAB_u8);
1✔
157
        from_helper(&[0xAB, 0xCD], 0xABCD_u16);
1✔
158
        from_helper(&[0x12, 0x34, 0x56, 0x78], 0x12345678_u32);
1✔
159
        from_helper(
1✔
160
            &[0x12, 0x34, 0x56, 0x78],
1✔
161
            FixedUInt::<u8, 4>::from_u32(0x12345678).unwrap(),
1✔
162
        );
163
        from_helper(
1✔
164
            &[0x12, 0x34, 0x56, 0x78],
1✔
165
            FixedUInt::<u16, 2>::from_u32(0x12345678).unwrap(),
1✔
166
        );
167
        from_helper(
1✔
168
            &[0x12, 0x34, 0x56, 0x78],
1✔
169
            FixedUInt::<u32, 1>::from_u32(0x12345678).unwrap(),
1✔
170
        );
171
    }
1✔
172
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc