From e140c2b5c5bb5abab7fcc6a9d4540aaa3bf566a6 Mon Sep 17 00:00:00 2001 From: Anthony Ramine Date: Sat, 28 Jan 2017 23:52:52 +0100 Subject: Use void for the unreachable serializers --- src/ser/void.rs | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/ser/void.rs (limited to 'src/ser/void.rs') diff --git a/src/ser/void.rs b/src/ser/void.rs new file mode 100644 index 0000000..4765bb7 --- /dev/null +++ b/src/ser/void.rs @@ -0,0 +1,122 @@ +use ser::Error; +use serde::ser; +use std::marker::PhantomData; +use void; + +pub struct VoidSerializer { + void: void::Void, + _marker: PhantomData, +} + +impl ser::SerializeSeq for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_element(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeTuple for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_element(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeTupleStruct for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_field(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeTupleVariant for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_field(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeMap for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_key(&mut self, + _key: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn serialize_value(&mut self, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeStruct for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_field(&mut self, + _key: &'static str, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} + +impl ser::SerializeStructVariant for VoidSerializer { + type Ok = Ok; + type Error = Error; + + fn serialize_field(&mut self, + _key: &'static str, + _value: &T) + -> Result<(), Error> { + void::unreachable(self.void) + } + + fn end(self) -> Result { + void::unreachable(self.void) + } +} -- cgit v1.2.3