about summary refs log tree commit diff
path: root/users/Profpatsch/netencode/netencode.rs
diff options
context:
space:
mode:
Diffstat (limited to 'users/Profpatsch/netencode/netencode.rs')
-rw-r--r--users/Profpatsch/netencode/netencode.rs18
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)
+            }
         }
     }
+
 }