From 80d4518a6771b6fd46d1e25af58b502dc3aff044 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sat, 17 May 2014 22:42:48 -0700 Subject: [PATCH] Clean up the APIs, assuming some things land in rust HEAD --- de.rs | 83 +++++++++++++++++------------------------------------------ 1 file changed, 23 insertions(+), 60 deletions(-) diff --git a/de.rs b/de.rs index a1870f6c..26a534f1 100644 --- a/de.rs +++ b/de.rs @@ -1,6 +1,7 @@ extern crate collections; use std::hash::Hash; +use std::result; use collections::HashMap; #[deriving(Clone, Eq)] @@ -79,46 +80,32 @@ pub trait Deserializer: Iterator> { >(&mut self) -> Result { try!(self.expect_collection_start()); - let mut err = None; + let iter = self.by_ref().batch(|d| { + let d = d.iter(); - let values: C = FromIterator::from_iter( - Batch::new(self, |d| { - let token = match d.next() { - Some(token) => token, - None => { return None; } - }; + let token = match d.next() { + Some(token) => token, + None => { return None; } + }; - match token { - Ok(CollectionSep) => { - let value: Result = Deserializable::deserialize(d); - match value { - Ok(value) => Some(value), - Err(e) => { - err = Some(e); - None - } - } - } - Ok(CollectionEnd) => { - None - } - Ok(_) => { - err = Some(d.syntax_error()); - None - } - Err(e) => { - err = Some(e); - None - } + match token { + Ok(CollectionSep) => { + let value: Result = Deserializable::deserialize(d); + Some(value) } + Ok(CollectionEnd) => { + None + } + Ok(_) => { + Some(Err(d.syntax_error())) + } + Err(e) => { + Some(Err(e)) + } + } + }); - }) - ); - - match err { - None => Ok(values), - Some(err) => Err(err), - } + result::collect(iter) } #[inline] @@ -312,30 +299,6 @@ impl< ////////////////////////////////////////////////////////////////////////////// -struct Batch<'a, A, B, T> { - iter: &'a mut T, - f: |&mut T|: 'a -> Option, -} - -impl<'a, A, B, T: Iterator> Batch<'a, A, B, T> { - #[inline] - fn new(iter: &'a mut T, f: |&mut T|: 'a -> Option) -> Batch<'a, A, B, T> { - Batch { - iter: iter, - f: f, - } - } -} - -impl<'a, A, B, T: Iterator> Iterator for Batch<'a, A, B, T> { - #[inline] - fn next(&mut self) -> Option { - (self.f)(self.iter) - } -} - -////////////////////////////////////////////////////////////////////////////// - #[cfg(test)] mod tests { extern crate serialize;