@@ -22,7 +22,7 @@ use core::ops::{
22
22
use crate :: error:: { DivError , OverflowError } ;
23
23
24
24
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) => {
26
26
#[ $doc]
27
27
#[ derive( PartialEq , Eq , Debug , Copy , Clone , Default , PartialOrd , Ord , Hash ) ]
28
28
#[ cfg_attr(
@@ -58,7 +58,12 @@ macro_rules! construct_smallint {
58
58
}
59
59
60
60
/// 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 {
62
67
self . 0 as $inner
63
68
}
64
69
}
@@ -290,21 +295,71 @@ construct_smallint!(
290
295
u1,
291
296
u8 ,
292
297
to_u8,
298
+ into_u8,
293
299
1 ,
294
300
2 ,
295
301
doc = "1-bit unsigned integer in the range `0..1`. It can be used instead of `bool` when \
296
302
1-bit numeric (and not boolean) arithmetic is required"
297
303
) ;
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
+ ) ;
304
358
construct_smallint ! (
305
359
u24,
306
360
u32 ,
307
361
to_u32,
362
+ into_u8,
308
363
24 ,
309
364
1u32 << 24 ,
310
365
doc = "24-bit unsigned integer in the range `0..16_777_216`"
0 commit comments