From 8348971eb72f6ef1ecbe7046aa04dd8fcaad65d3 Mon Sep 17 00:00:00 2001 From: kirk Date: Sun, 7 Apr 2024 06:35:21 +0000 Subject: [PATCH] remove an unwrap from the DeBin impl for String --- src/serde_bin.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/serde_bin.rs b/src/serde_bin.rs index badc83e..93e0a1c 100644 --- a/src/serde_bin.rs +++ b/src/serde_bin.rs @@ -1,9 +1,10 @@ use core::convert::TryInto; use core::hash::Hash; +use alloc::borrow::ToOwned; use alloc::boxed::Box; use alloc::collections::{BTreeSet, LinkedList}; -use alloc::string::{String, ToString}; +use alloc::string::String; use alloc::vec::Vec; #[cfg(feature = "no_std")] @@ -224,9 +225,16 @@ impl DeBin for String { s: d.len(), }); } - let r = core::str::from_utf8(&d[*o..(*o + len)]) - .unwrap() - .to_string(); + let r = match core::str::from_utf8(&d[*o..(*o + len)]) { + Ok(r) => r.to_owned(), + Err(_) => { + return Err(DeBinErr { + o: *o, + l: len, + s: d.len(), + }) + } + }; *o += len; Ok(r) }