diff options
author | Profpatsch <mail@profpatsch.de> | 2021-02-13T13·23+0100 |
---|---|---|
committer | Profpatsch <mail@profpatsch.de> | 2021-02-13T20·00+0000 |
commit | 1d752f031b116a759a14322debf9faa0a834664c (patch) | |
tree | dca4b7e565de95fd0bc20dbb975fe028ba96a3da /users/Profpatsch/netencode/netencode.rs | |
parent | 1b706b5ae3514082f4d64eb5718a9f73bcdd46bd (diff) |
feat(users/Profpatsch/netencode): add dec::Try r/2209
Tries to decode the inner type, turning it into an Option. Change-Id: I29d1286fe873c28d7c4a4b71f220acaf2d23f8e1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2522 Tested-by: BuildkiteCI Reviewed-by: Profpatsch <mail@profpatsch.de>
Diffstat (limited to '')
-rw-r--r-- | users/Profpatsch/netencode/netencode.rs | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/users/Profpatsch/netencode/netencode.rs b/users/Profpatsch/netencode/netencode.rs index f2fef1e109ed..28003260925c 100644 --- a/users/Profpatsch/netencode/netencode.rs +++ b/users/Profpatsch/netencode/netencode.rs @@ -758,11 +758,19 @@ pub mod dec { } } - fn dec_u(b: &[u8]) -> Result<U, DecodeError> { - match parse::u_u(b) { - Ok((b"", u)) => Ok(u), - Ok((rest, _)) => Err(DecodeError(format!("Cannot decode nested U, it contains trailing bytes"))), - Err(err) => Err(DecodeError(format!("Cannot decode nested U bytes: {:?}", err))), + #[derive(Clone)] + pub struct Try<T>(pub T); + + impl <'a, Inner> Decoder<'a> for Try<Inner> + where Inner: Decoder<'a> + { + type A = Option<Inner::A>; + fn dec(&self, u: U<'a>) -> Result<Self::A, DecodeError> { + match self.0.dec(u) { + Ok(inner) => Ok(Some(inner)), + Err(err) => Ok(None) + } } } + } |