Skip to content

Commit c5ae4cb

Browse files
committed
smallint: change to_u8 signature; add into_u8
1 parent d56dce2 commit c5ae4cb

File tree

1 file changed

+63
-8
lines changed

1 file changed

+63
-8
lines changed

num/src/smallint.rs

+63-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use core::ops::{
2222
use crate::error::{DivError, OverflowError};
2323

2424
macro_rules! construct_smallint {
25-
($ty:ident, $inner:ident, $to:ident, $bits:literal, $max:expr, $doc:meta) => {
25+
($ty:ident, $inner:ident, $to:ident, $into:ident, $bits:literal, $max:expr, $doc:meta) => {
2626
#[$doc]
2727
#[derive(PartialEq, Eq, Debug, Copy, Clone, Default, PartialOrd, Ord, Hash)]
2828
#[cfg_attr(
@@ -58,7 +58,12 @@ macro_rules! construct_smallint {
5858
}
5959

6060
/// Returns inner `u8` representation, which is always less or equal to `Self::MAX`
61-
pub const fn $to(self) -> $inner {
61+
pub const fn $to(&self) -> $inner {
62+
self.0 as $inner
63+
}
64+
65+
/// Returns inner `u8` representation, which is always less or equal to `Self::MAX`
66+
pub const fn $into(self) -> $inner {
6267
self.0 as $inner
6368
}
6469
}
@@ -290,21 +295,71 @@ construct_smallint!(
290295
u1,
291296
u8,
292297
to_u8,
298+
into_u8,
293299
1,
294300
2,
295301
doc = "1-bit unsigned integer in the range `0..1`. It can be used instead of `bool` when \
296302
1-bit numeric (and not boolean) arithmetic is required"
297303
);
298-
construct_smallint!(u2, u8, to_u8, 2, 4, doc = "2-bit unsigned integer in the range `0..4`");
299-
construct_smallint!(u3, u8, to_u8, 3, 8, doc = "3-bit unsigned integer in the range `0..8`");
300-
construct_smallint!(u4, u8, to_u8, 4, 16, doc = "4-bit unsigned integer in the range `0..16`");
301-
construct_smallint!(u5, u8, to_u8, 5, 32, doc = "5-bit unsigned integer in the range `0..32`");
302-
construct_smallint!(u6, u8, to_u8, 6, 64, doc = "6-bit unsigned integer in the range `0..64`");
303-
construct_smallint!(u7, u8, to_u8, 7, 128, doc = "7-bit unsigned integer in the range `0..128`");
304+
construct_smallint!(
305+
u2,
306+
u8,
307+
to_u8,
308+
into_u8,
309+
2,
310+
4,
311+
doc = "2-bit unsigned integer in the range `0..4`"
312+
);
313+
construct_smallint!(
314+
u3,
315+
u8,
316+
to_u8,
317+
into_u8,
318+
3,
319+
8,
320+
doc = "3-bit unsigned integer in the range `0..8`"
321+
);
322+
construct_smallint!(
323+
u4,
324+
u8,
325+
to_u8,
326+
into_u8,
327+
4,
328+
16,
329+
doc = "4-bit unsigned integer in the range `0..16`"
330+
);
331+
construct_smallint!(
332+
u5,
333+
u8,
334+
to_u8,
335+
into_u8,
336+
5,
337+
32,
338+
doc = "5-bit unsigned integer in the range `0..32`"
339+
);
340+
construct_smallint!(
341+
u6,
342+
u8,
343+
to_u8,
344+
into_u8,
345+
6,
346+
64,
347+
doc = "6-bit unsigned integer in the range `0..64`"
348+
);
349+
construct_smallint!(
350+
u7,
351+
u8,
352+
to_u8,
353+
into_u8,
354+
7,
355+
128,
356+
doc = "7-bit unsigned integer in the range `0..128`"
357+
);
304358
construct_smallint!(
305359
u24,
306360
u32,
307361
to_u32,
362+
into_u8,
308363
24,
309364
1u32 << 24,
310365
doc = "24-bit unsigned integer in the range `0..16_777_216`"

0 commit comments

Comments
 (0)