Skip to content

Commit a063a8f

Browse files
committed
Return Option from decode
1 parent 427c372 commit a063a8f

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

hex-literal/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,22 @@ pub const fn len(strings: &[&[u8]]) -> usize {
5656
///
5757
/// This function is an implementation detail and SHOULD NOT be called directly!
5858
#[doc(hidden)]
59-
pub const fn decode<const LEN: usize>(strings: &[&[u8]]) -> [u8; LEN] {
59+
pub const fn decode<const LEN: usize>(strings: &[&[u8]]) -> Option<[u8; LEN]> {
6060
let mut string_pos = 0;
6161
let mut buf = [0u8; LEN];
6262
let mut buf_pos = 0;
6363
while string_pos < strings.len() {
6464
let mut pos = 0;
6565
let string = &strings[string_pos];
6666
string_pos += 1;
67-
67+
6868
while let Some((byte, new_pos)) = next_byte(string, pos) {
6969
buf[buf_pos] = byte;
7070
buf_pos += 1;
7171
pos = new_pos;
7272
}
7373
}
74-
if LEN != buf_pos {
75-
panic!("Length mismatch. Please report this bug.");
76-
}
77-
buf
74+
if LEN == buf_pos { Some(buf) } else { None }
7875
}
7976

8077
/// Macro for converting sequence of string literals containing hex-encoded data
@@ -83,6 +80,9 @@ pub const fn decode<const LEN: usize>(strings: &[&[u8]]) -> [u8; LEN] {
8380
macro_rules! hex {
8481
($($s:literal)*) => {{
8582
const STRINGS: &[&'static [u8]] = &[$($s.as_bytes(),)*];
86-
const { $crate::decode::<{ $crate::len(STRINGS) }>(STRINGS) }
83+
const {
84+
$crate::decode::<{ $crate::len(STRINGS) }>(STRINGS)
85+
.expect("Output array length should be correct")
86+
}
8787
}};
8888
}

0 commit comments

Comments
 (0)