1
- #![ doc = include_str ! ( "../README.md" ) ]
2
1
#![ no_std]
2
+ #![ doc = include_str ! ( "../README.md" ) ]
3
3
#![ doc(
4
4
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg" ,
5
5
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
@@ -56,23 +56,22 @@ pub const fn len(strings: &[&[u8]]) -> usize {
56
56
///
57
57
/// This function is an implementation detail and SHOULD NOT be called directly!
58
58
#[ doc( hidden) ]
59
- pub const fn decode < const LEN : usize > ( strings : & [ & [ u8 ] ] ) -> [ u8 ; LEN ] {
60
- let mut i = 0 ;
59
+ pub const fn decode < const LEN : usize > ( strings : & [ & [ u8 ] ] ) -> Option < [ u8 ; LEN ] > {
60
+ let mut string_pos = 0 ;
61
61
let mut buf = [ 0u8 ; LEN ] ;
62
62
let mut buf_pos = 0 ;
63
- while i < strings. len ( ) {
63
+ while string_pos < strings. len ( ) {
64
64
let mut pos = 0 ;
65
- while let Some ( ( byte, new_pos) ) = next_byte ( strings[ i] , pos) {
65
+ let string = & strings[ string_pos] ;
66
+ string_pos += 1 ;
67
+
68
+ while let Some ( ( byte, new_pos) ) = next_byte ( string, pos) {
66
69
buf[ buf_pos] = byte;
67
70
buf_pos += 1 ;
68
71
pos = new_pos;
69
72
}
70
- i += 1 ;
71
73
}
72
- if LEN != buf_pos {
73
- panic ! ( "Length mismatch. Please report this bug." ) ;
74
- }
75
- buf
74
+ if LEN == buf_pos { Some ( buf) } else { None }
76
75
}
77
76
78
77
/// Macro for converting sequence of string literals containing hex-encoded data
@@ -81,8 +80,9 @@ pub const fn decode<const LEN: usize>(strings: &[&[u8]]) -> [u8; LEN] {
81
80
macro_rules! hex {
82
81
( $( $s: literal) * ) => { {
83
82
const STRINGS : & [ & ' static [ u8 ] ] = & [ $( $s. as_bytes( ) , ) * ] ;
84
- const LEN : usize = $crate:: len( STRINGS ) ;
85
- const RES : [ u8 ; LEN ] = $crate:: decode( STRINGS ) ;
86
- RES
83
+ const {
84
+ $crate:: decode:: <{ $crate:: len( STRINGS ) } >( STRINGS )
85
+ . expect( "Output array length should be correct" )
86
+ }
87
87
} } ;
88
88
}
0 commit comments