about summary refs log tree commit diff
path: root/src/libserialize
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-12-23 23:06:13 +0000
committerbors <bors@rust-lang.org>2017-12-23 23:06:13 +0000
commit8d4da4f4c4f097e6a95264cc527da64bfc133b0f (patch)
tree5d701484cd049f4658932105c3fcf0be54e9b1fd /src/libserialize
parent16992930835ce3376a4aaed42307726e1fc78e45 (diff)
parent44a0522b180805e9ff0d6b5009c031f6d131b4a6 (diff)
downloadrust-8d4da4f4c4f097e6a95264cc527da64bfc133b0f.tar.gz
rust-8d4da4f4c4f097e6a95264cc527da64bfc133b0f.zip
Auto merge of #46881 - michaelwoerister:ensure-coherence, r=nikomatsakis
incr.comp.: Cache check_match and use ensure() for coherence-related queries.

Some minor optimizations.

r? @nikomatsakis
Diffstat (limited to 'src/libserialize')
-rw-r--r--src/libserialize/serialize.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs
index 6d67bbc06cc..854ca4eb3b0 100644
--- a/src/libserialize/serialize.rs
+++ b/src/libserialize/serialize.rs
@@ -618,6 +618,54 @@ impl<T:Decodable> Decodable for Option<T> {
     }
 }
 
+impl<T1: Encodable, T2: Encodable> Encodable for Result<T1, T2> {
+    fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
+        s.emit_enum("Result", |s| {
+            match *self {
+                Ok(ref v) => {
+                    s.emit_enum_variant("Ok", 0, 1, |s| {
+                        s.emit_enum_variant_arg(0, |s| {
+                            v.encode(s)
+                        })
+                    })
+                }
+                Err(ref v) => {
+                    s.emit_enum_variant("Err", 1, 1, |s| {
+                        s.emit_enum_variant_arg(0, |s| {
+                            v.encode(s)
+                        })
+                    })
+                }
+            }
+        })
+    }
+}
+
+impl<T1:Decodable, T2:Decodable> Decodable for Result<T1, T2> {
+    fn decode<D: Decoder>(d: &mut D) -> Result<Result<T1, T2>, D::Error> {
+        d.read_enum("Result", |d| {
+            d.read_enum_variant(&["Ok", "Err"], |d, disr| {
+                match disr {
+                    0 => {
+                        Ok(Ok(d.read_enum_variant_arg(0, |d| {
+                            T1::decode(d)
+                        })?))
+                    }
+                    1 => {
+                        Ok(Err(d.read_enum_variant_arg(0, |d| {
+                            T2::decode(d)
+                        })?))
+                    }
+                    _ => {
+                        panic!("Encountered invalid discriminant while \
+                                decoding `Result`.");
+                    }
+                }
+            })
+        })
+    }
+}
+
 macro_rules! peel {
     ($name:ident, $($other:ident,)*) => (tuple! { $($other,)* })
 }