From aebe1fddab8f43ab4708f6f37ca75f859d110832 Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Fri, 10 Mar 2017 00:18:56 +1300 Subject: [PATCH] Add Serialize impl for std::ops::Range --- serde/src/ser/impls.rs | 19 +++++++++++++++++++ test_suite/tests/test_ser.rs | 13 +++++++++++++ 2 files changed, 32 insertions(+) diff --git a/serde/src/ser/impls.rs b/serde/src/ser/impls.rs index 4c7a263f..409ec470 100644 --- a/serde/src/ser/impls.rs +++ b/serde/src/ser/impls.rs @@ -29,6 +29,8 @@ use std::rc::Rc; use alloc::rc::Rc; #[cfg(feature = "std")] use std::time::Duration; +#[cfg(feature = "std")] +use std; #[cfg(feature = "std")] use std::sync::Arc; @@ -262,6 +264,23 @@ impl Serialize for VecDeque serialize_seq!(); } +/////////////////////////////////////////////////////////////////////////////// + +#[cfg(feature = "std")] +impl Serialize for std::ops::Range { + fn serialize(&self, serializer: S) -> Result + where S: Serializer + { + use super::SerializeStruct; + let mut state = try!(serializer.serialize_struct("Range", 2)); + try!(state.serialize_field("start", &self.start)); + try!(state.serialize_field("end", &self.end)); + state.end() + } +} + +/////////////////////////////////////////////////////////////////////////////// + #[cfg(feature = "unstable")] impl Serialize for ops::Range where ops::Range: ExactSizeIterator + iter::Iterator + Clone, diff --git a/test_suite/tests/test_ser.rs b/test_suite/tests/test_ser.rs index 940f463d..d58fdfe7 100644 --- a/test_suite/tests/test_ser.rs +++ b/test_suite/tests/test_ser.rs @@ -366,6 +366,19 @@ declare_tests! { Token::StructEnd, ], } + test_range { + 1u32..2u32 => &[ + Token::StructStart("Range", 2), + Token::StructSep, + Token::Str("start"), + Token::U32(1), + + Token::StructSep, + Token::Str("end"), + Token::U32(2), + Token::StructEnd, + ], + } test_net_ipv4addr { "1.2.3.4".parse::().unwrap() => &[Token::Str("1.2.3.4")], }