From c790e9b6013cfe86c81f5d8068feefff4339abf7 Mon Sep 17 00:00:00 2001 From: Reiner Herrmann Date: Mon, 25 Mar 2019 01:40:14 +0100 Subject: Simplify error handling in some places --- src/lib.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index d2374ab..f049d05 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,18 +145,14 @@ impl Tftp { } fn get_tftp_str(&self, buf: &[u8]) -> Option { - let mut iter = buf.iter(); - - let len = match iter.position(|&x| x == 0) { - Some(l) => l, - None => return None, - }; - let val = match String::from_utf8(buf[0..len].to_vec()) { - Ok(v) => v, - Err(_) => return None, - }; - - Some(val) + /* make sure the null-terminator exists */ + buf.iter().find(|&x| *x == 0)?; + + /* build string from buffer */ + String::from_utf8(buf.iter() + .take_while(|&x| *x != 0) + .map(|&x| x) + .collect()).ok() } /// Read::read can possibly return less bytes than the requested buffer size, -- cgit v1.2.3