mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-06-16 21:41:02 +00:00
Merge pull request #680 from serde-rs/oli-obk-patch-1
make `ser::iterator` more general by taking `IntoIterator`
This commit is contained in:
@@ -226,7 +226,7 @@ array_impls!(32);
|
|||||||
|
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
impl<'a, I> Serialize for Iterator<I>
|
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]
|
#[inline]
|
||||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
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?
|
// FIXME: use specialization to prevent invalidating the object in case of clonable iterators?
|
||||||
let iter = match self.0.borrow_mut().take() {
|
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")),
|
None => return Err(S::Error::custom("Iterator used twice")),
|
||||||
};
|
};
|
||||||
let size = match iter.size_hint() {
|
let size = match iter.size_hint() {
|
||||||
|
|||||||
@@ -22,8 +22,6 @@ use collections::String;
|
|||||||
use core::marker::PhantomData;
|
use core::marker::PhantomData;
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
#[cfg(feature = "unstable")]
|
|
||||||
use core::iter;
|
|
||||||
|
|
||||||
pub mod impls;
|
pub mod impls;
|
||||||
|
|
||||||
@@ -424,15 +422,15 @@ pub trait Serializer {
|
|||||||
/// every time you want to serialize an iterator.
|
/// every time you want to serialize an iterator.
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
pub struct Iterator<I>(RefCell<Option<I>>)
|
pub struct Iterator<I>(RefCell<Option<I>>)
|
||||||
where <I as iter::Iterator>::Item: Serialize,
|
where <I as IntoIterator>::Item: Serialize,
|
||||||
I: iter::Iterator;
|
I: IntoIterator;
|
||||||
|
|
||||||
/// Creates a temporary type that can be passed to any function expecting a `Serialize` and will
|
/// Creates a temporary type that can be passed to any function expecting a `Serialize` and will
|
||||||
/// serialize the given iterator as a sequence
|
/// serialize the given iterator as a sequence
|
||||||
#[cfg(feature = "unstable")]
|
#[cfg(feature = "unstable")]
|
||||||
pub fn iterator<I>(iter: I) -> Iterator<I>
|
pub fn iterator<I>(iter: I) -> Iterator<I>
|
||||||
where <I as iter::Iterator>::Item: Serialize,
|
where <I as IntoIterator>::Item: Serialize,
|
||||||
I: iter::Iterator
|
I: IntoIterator
|
||||||
{
|
{
|
||||||
Iterator(RefCell::new(Some(iter)))
|
Iterator(RefCell::new(Some(iter)))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user