Merge pull request #680 from serde-rs/oli-obk-patch-1

make `ser::iterator` more general by taking `IntoIterator`
This commit is contained in:
David Tolnay
2017-01-12 16:45:39 -08:00
committed by GitHub
2 changed files with 6 additions and 8 deletions
+2 -2
View File
@@ -226,7 +226,7 @@ array_impls!(32);
#[cfg(feature = "unstable")]
impl<'a, I> Serialize for Iterator<I>
where I: iter::Iterator, <I as iter::Iterator>::Item: Serialize
where I: IntoIterator, <I as IntoIterator>::Item: Serialize
{
#[inline]
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
@@ -234,7 +234,7 @@ impl<'a, I> Serialize for Iterator<I>
{
// FIXME: use specialization to prevent invalidating the object in case of clonable iterators?
let iter = match self.0.borrow_mut().take() {
Some(iter) => iter,
Some(iter) => iter.into_iter(),
None => return Err(S::Error::custom("Iterator used twice")),
};
let size = match iter.size_hint() {
+4 -6
View File
@@ -22,8 +22,6 @@ use collections::String;
use core::marker::PhantomData;
#[cfg(feature = "unstable")]
use core::cell::RefCell;
#[cfg(feature = "unstable")]
use core::iter;
pub mod impls;
@@ -424,15 +422,15 @@ pub trait Serializer {
/// every time you want to serialize an iterator.
#[cfg(feature = "unstable")]
pub struct Iterator<I>(RefCell<Option<I>>)
where <I as iter::Iterator>::Item: Serialize,
I: iter::Iterator;
where <I as IntoIterator>::Item: Serialize,
I: IntoIterator;
/// Creates a temporary type that can be passed to any function expecting a `Serialize` and will
/// serialize the given iterator as a sequence
#[cfg(feature = "unstable")]
pub fn iterator<I>(iter: I) -> Iterator<I>
where <I as iter::Iterator>::Item: Serialize,
I: iter::Iterator
where <I as IntoIterator>::Item: Serialize,
I: IntoIterator
{
Iterator(RefCell::new(Some(iter)))
}