File tree Expand file tree Collapse file tree 3 files changed +17
-14
lines changed Expand file tree Collapse file tree 3 files changed +17
-14
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ macro_rules! checked_add {
10
10
} ;
11
11
}
12
12
13
- /// `const fn`-friendly checked addition helper.
13
+ /// `const fn`-friendly checked subtraction helper.
14
14
macro_rules! checked_sub {
15
15
( $a: expr, $b: expr) => {
16
16
match $a. checked_sub( $b) {
@@ -19,3 +19,13 @@ macro_rules! checked_sub {
19
19
}
20
20
} ;
21
21
}
22
+
23
+ /// `const fn`-friendly checked multiplication helper.
24
+ macro_rules! checked_mul {
25
+ ( $a: expr, $b: expr) => {
26
+ match $a. checked_mul( $b) {
27
+ Some ( n) => n,
28
+ None => return Err ( Error :: Overflow ) ,
29
+ }
30
+ } ;
31
+ }
Original file line number Diff line number Diff line change @@ -51,7 +51,6 @@ impl<const MAX_SIZE: usize> Encoder<MAX_SIZE> {
51
51
}
52
52
53
53
/// Encode an [`Arc`] as base 128 into the internal buffer.
54
- #[ allow( clippy:: panic_in_result_fn) ]
55
54
pub ( crate ) const fn arc ( mut self , arc : Arc ) -> Result < Self > {
56
55
match self . state {
57
56
State :: Initial => {
@@ -68,18 +67,10 @@ impl<const MAX_SIZE: usize> Encoder<MAX_SIZE> {
68
67
}
69
68
70
69
self . state = State :: Body ;
71
- self . bytes [ 0 ] = match ( ARC_MAX_SECOND + 1 ) . checked_mul ( first_arc) {
72
- // TODO(tarcieri): use `and_then` when const traits are stable
73
- Some ( n) => match n. checked_add ( arc) {
74
- Some ( byte) => byte as u8 ,
75
- None => {
76
- // TODO(tarcieri): use `unreachable!`
77
- panic ! ( "overflow prevented by ARC_MAX_SECOND check" )
78
- }
79
- } ,
80
- // TODO(tarcieri): use `unreachable!`
81
- None => panic ! ( "overflow prevented by ARC_MAX_SECOND check" ) ,
82
- } ;
70
+ self . bytes [ 0 ] = checked_add ! (
71
+ checked_mul!( checked_add!( ARC_MAX_SECOND , 1 ) , first_arc) ,
72
+ arc
73
+ ) as u8 ;
83
74
self . cursor = 1 ;
84
75
Ok ( self )
85
76
}
Original file line number Diff line number Diff line change @@ -38,6 +38,8 @@ pub enum Error {
38
38
Length ,
39
39
40
40
/// Arithmetic overflow (or underflow) errors.
41
+ ///
42
+ /// These generally indicate a bug in the `const-oid` crate.
41
43
Overflow ,
42
44
43
45
/// Repeated `..` characters in input data.
You can’t perform that action at this time.
0 commit comments