diff options
| author | Reiner Herrmann <reiner@reiner-h.de> | 2019-03-25 01:40:14 +0100 |
|---|---|---|
| committer | Reiner Herrmann <reiner@reiner-h.de> | 2019-03-25 01:40:14 +0100 |
| commit | c790e9b6013cfe86c81f5d8068feefff4339abf7 (patch) | |
| tree | 2ff554b692128779cf0bd9f3fdd8aaed0984130d /src/lib.rs | |
| parent | 01838bd4e8a208ef0d76181e1b5875421b7803a2 (diff) | |
Simplify error handling in some places
Diffstat (limited to 'src/lib.rs')
| -rw-r--r-- | src/lib.rs | 20 |
1 files changed, 8 insertions, 12 deletions
@@ -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, |
