From 1d752f031b116a759a14322debf9faa0a834664c Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Sat, 13 Feb 2021 14:23:29 +0100 Subject: feat(users/Profpatsch/netencode): add dec::Try 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 --- users/Profpatsch/netencode/netencode.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'users') 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 { - 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(pub T); + + impl <'a, Inner> Decoder<'a> for Try + where Inner: Decoder<'a> + { + type A = Option; + fn dec(&self, u: U<'a>) -> Result { + match self.0.dec(u) { + Ok(inner) => Ok(Some(inner)), + Err(err) => Ok(None) + } } } + } -- cgit 1.4.1