Add Serialize impl for std::ops::Range

This commit is contained in:
Robert O'Callahan 2017-03-10 00:18:56 +13:00
parent 77ee306b57
commit aebe1fddab
2 changed files with 32 additions and 0 deletions

View File

@ -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<T> Serialize for VecDeque<T>
serialize_seq!();
}
///////////////////////////////////////////////////////////////////////////////
#[cfg(feature = "std")]
impl<Idx: Serialize> Serialize for std::ops::Range<Idx> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<A> Serialize for ops::Range<A>
where ops::Range<A>: ExactSizeIterator + iter::Iterator<Item = A> + Clone,

View File

@ -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::<net::Ipv4Addr>().unwrap() => &[Token::Str("1.2.3.4")],
}