mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-13 16:01:02 +00:00
Allow Option<T> to be used directly as a ser::SeqVisitor
This commit is contained in:
@@ -97,6 +97,26 @@ impl<T> Serialize for Option<T> where T: Serialize {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> SeqVisitor for Option<T> where T: Serialize {
|
||||||
|
#[inline]
|
||||||
|
fn visit<S>(&mut self, serializer: &mut S) -> Result<Option<()>, S::Error>
|
||||||
|
where S: Serializer,
|
||||||
|
{
|
||||||
|
match self.take() {
|
||||||
|
Some(value) => {
|
||||||
|
try!(serializer.visit_seq_elt(value));
|
||||||
|
Ok(Some(()))
|
||||||
|
}
|
||||||
|
None => Ok(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn len(&self) -> Option<usize> {
|
||||||
|
Some(if self.is_some() { 1 } else { 0 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
pub struct SeqIteratorVisitor<Iter> {
|
pub struct SeqIteratorVisitor<Iter> {
|
||||||
|
|||||||
Reference in New Issue
Block a user