aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2019-03-25 01:40:14 +0100
committerReiner Herrmann <reiner@reiner-h.de>2019-03-25 01:40:14 +0100
commitc790e9b6013cfe86c81f5d8068feefff4339abf7 (patch)
tree2ff554b692128779cf0bd9f3fdd8aaed0984130d /src/lib.rs
parent01838bd4e8a208ef0d76181e1b5875421b7803a2 (diff)
Simplify error handling in some places
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs20
1 files changed, 8 insertions, 12 deletions
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<String> {
- 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,