From 58dac8af4241aa6285a8d1febbe6c01d6e42c9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 23 Nov 2023 14:47:56 +0100 Subject: [PATCH] Remove the panics and unwraps --- milli/src/heed_codec/facet/mod.rs | 2 +- .../src/heed_codec/facet/ordered_f64_codec.rs | 9 +++++---- .../heed_codec/field_id_word_count_codec.rs | 6 ++++-- milli/src/heed_codec/script_language_codec.rs | 6 +++++- milli/src/heed_codec/str_beu32_codec.rs | 8 ++++---- milli/src/heed_codec/str_str_u8_codec.rs | 20 +++++++++++++------ milli/src/search/new/db_cache.rs | 16 +++++++-------- 7 files changed, 41 insertions(+), 26 deletions(-) diff --git a/milli/src/heed_codec/facet/mod.rs b/milli/src/heed_codec/facet/mod.rs index e5ff1820c..7bb874060 100644 --- a/milli/src/heed_codec/facet/mod.rs +++ b/milli/src/heed_codec/facet/mod.rs @@ -63,7 +63,7 @@ where v.extend_from_slice(&value.field_id.to_be_bytes()); v.extend_from_slice(&[value.level]); - let bound = T::bytes_encode(&value.left_bound).unwrap(); + let bound = T::bytes_encode(&value.left_bound)?; v.extend_from_slice(&bound); Ok(Cow::Owned(v)) diff --git a/milli/src/heed_codec/facet/ordered_f64_codec.rs b/milli/src/heed_codec/facet/ordered_f64_codec.rs index cbefe499f..64bb0b0cd 100644 --- a/milli/src/heed_codec/facet/ordered_f64_codec.rs +++ b/milli/src/heed_codec/facet/ordered_f64_codec.rs @@ -12,10 +12,10 @@ impl<'a> BytesDecode<'a> for OrderedF64Codec { fn bytes_decode(bytes: &'a [u8]) -> Result { if bytes.len() < 16 { - panic!() // TODO don't panic + Err(BoxedError::from("invalid slice length")) + } else { + bytes[8..].try_into().map(f64::from_be_bytes).map_err(Into::into) } - let f = bytes[8..].try_into().ok().map(f64::from_be_bytes).unwrap(); - Ok(f) } } @@ -26,7 +26,8 @@ impl heed::BytesEncode<'_> for OrderedF64Codec { let mut buffer = [0u8; 16]; // write the globally ordered float - let bytes = f64_into_bytes(*f).unwrap(); + let bytes = f64_into_bytes(*f) + .ok_or_else(|| BoxedError::from("cannot generate a globally ordered float"))?; buffer[..8].copy_from_slice(&bytes[..]); // Then the f64 value just to be able to read it back let bytes = f.to_be_bytes(); diff --git a/milli/src/heed_codec/field_id_word_count_codec.rs b/milli/src/heed_codec/field_id_word_count_codec.rs index e284a45a2..9e7f044c5 100644 --- a/milli/src/heed_codec/field_id_word_count_codec.rs +++ b/milli/src/heed_codec/field_id_word_count_codec.rs @@ -10,9 +10,11 @@ impl<'a> heed::BytesDecode<'a> for FieldIdWordCountCodec { type DItem = (FieldId, u8); fn bytes_decode(bytes: &'a [u8]) -> Result { - let (field_id_bytes, bytes) = try_split_array_at(bytes).unwrap(); + let (field_id_bytes, bytes) = + try_split_array_at(bytes).ok_or("invalid slice length").map_err(BoxedError::from)?; let field_id = u16::from_be_bytes(field_id_bytes); - let ([word_count], _nothing) = try_split_array_at(bytes).unwrap(); + let ([word_count], _nothing) = + try_split_array_at(bytes).ok_or("invalid slice length").map_err(BoxedError::from)?; Ok((field_id, word_count)) } } diff --git a/milli/src/heed_codec/script_language_codec.rs b/milli/src/heed_codec/script_language_codec.rs index 83f5bf45f..013ec62bb 100644 --- a/milli/src/heed_codec/script_language_codec.rs +++ b/milli/src/heed_codec/script_language_codec.rs @@ -10,7 +10,11 @@ impl<'a> heed::BytesDecode<'a> for ScriptLanguageCodec { type DItem = (Script, Language); fn bytes_decode(bytes: &'a [u8]) -> Result { - let sep = bytes.iter().position(|b| *b == 0).unwrap(); + let sep = bytes + .iter() + .position(|b| *b == 0) + .ok_or("cannot find nul byte") + .map_err(BoxedError::from)?; let (s_bytes, l_bytes) = bytes.split_at(sep); let script = str::from_utf8(s_bytes)?; let script_name = Script::from_name(script); diff --git a/milli/src/heed_codec/str_beu32_codec.rs b/milli/src/heed_codec/str_beu32_codec.rs index aace89d78..c654a1811 100644 --- a/milli/src/heed_codec/str_beu32_codec.rs +++ b/milli/src/heed_codec/str_beu32_codec.rs @@ -14,7 +14,7 @@ impl<'a> heed::BytesDecode<'a> for StrBEU32Codec { let footer_len = size_of::(); if bytes.len() < footer_len { - panic!() // TODO Do not panic + return Err(BoxedError::from("cannot extract footer from bytes")); } let (word, bytes) = bytes.split_at(bytes.len() - footer_len); @@ -48,13 +48,13 @@ impl<'a> heed::BytesDecode<'a> for StrBEU16Codec { let footer_len = size_of::(); if bytes.len() < footer_len + 1 { - panic!() // TODO do not panic + return Err(BoxedError::from("cannot extract footer from bytes")); } let (word_plus_nul_byte, bytes) = bytes.split_at(bytes.len() - footer_len); let (_, word) = word_plus_nul_byte.split_last().unwrap(); - let word = str::from_utf8(word).ok().unwrap(); - let pos = bytes.try_into().map(u16::from_be_bytes).ok().unwrap(); + let word = str::from_utf8(word)?; + let pos = bytes.try_into().map(u16::from_be_bytes)?; Ok((word, pos)) } diff --git a/milli/src/heed_codec/str_str_u8_codec.rs b/milli/src/heed_codec/str_str_u8_codec.rs index c6ba7ebac..743ddb1f7 100644 --- a/milli/src/heed_codec/str_str_u8_codec.rs +++ b/milli/src/heed_codec/str_str_u8_codec.rs @@ -9,12 +9,16 @@ impl<'a> heed::BytesDecode<'a> for U8StrStrCodec { type DItem = (u8, &'a str, &'a str); fn bytes_decode(bytes: &'a [u8]) -> Result { - let (n, bytes) = bytes.split_first().unwrap(); - let s1_end = bytes.iter().position(|b| *b == 0).unwrap(); + let (n, bytes) = bytes.split_first().ok_or("not enough bytes").map_err(BoxedError::from)?; + let s1_end = bytes + .iter() + .position(|b| *b == 0) + .ok_or("cannot find nul byte") + .map_err(BoxedError::from)?; let (s1_bytes, rest) = bytes.split_at(s1_end); let s2_bytes = &rest[1..]; - let s1 = str::from_utf8(s1_bytes).ok().unwrap(); - let s2 = str::from_utf8(s2_bytes).ok().unwrap(); + let s1 = str::from_utf8(s1_bytes)?; + let s2 = str::from_utf8(s2_bytes)?; Ok((*n, s1, s2)) } } @@ -37,8 +41,12 @@ impl<'a> heed::BytesDecode<'a> for UncheckedU8StrStrCodec { type DItem = (u8, &'a [u8], &'a [u8]); fn bytes_decode(bytes: &'a [u8]) -> Result { - let (n, bytes) = bytes.split_first().unwrap(); - let s1_end = bytes.iter().position(|b| *b == 0).unwrap(); + let (n, bytes) = bytes.split_first().ok_or("not enough bytes").map_err(BoxedError::from)?; + let s1_end = bytes + .iter() + .position(|b| *b == 0) + .ok_or("cannot find nul byte") + .map_err(BoxedError::from)?; let (s1_bytes, rest) = bytes.split_at(s1_end); let s2_bytes = &rest[1..]; Ok((*n, s1_bytes, s2_bytes)) diff --git a/milli/src/search/new/db_cache.rs b/milli/src/search/new/db_cache.rs index 5a74bc201..1dd701553 100644 --- a/milli/src/search/new/db_cache.rs +++ b/milli/src/search/new/db_cache.rs @@ -64,13 +64,13 @@ impl<'ctx> DatabaseCache<'ctx> { match cache.get(&cache_key).unwrap() { Some(Cow::Borrowed(bytes)) => DC::bytes_decode_owned(bytes) + .map(Some) .map_err(heed::Error::Decoding) - .map_err(Into::into) - .map(Some), + .map_err(Into::into), Some(Cow::Owned(bytes)) => DC::bytes_decode_owned(bytes) + .map(Some) .map_err(heed::Error::Decoding) - .map_err(Into::into) - .map(Some), + .map_err(Into::into), None => Ok(None), } } @@ -113,13 +113,13 @@ impl<'ctx> DatabaseCache<'ctx> { match cache.get(&cache_key).unwrap() { Some(Cow::Borrowed(bytes)) => DC::bytes_decode_owned(bytes) + .map(Some) .map_err(heed::Error::Decoding) - .map_err(Into::into) - .map(Some), + .map_err(Into::into), Some(Cow::Owned(bytes)) => DC::bytes_decode_owned(bytes) + .map(Some) .map_err(heed::Error::Decoding) - .map_err(Into::into) - .map(Some), + .map_err(Into::into), None => Ok(None), } }