Use try_fold in default implementation of collect_seq, collect_map
This commit is contained in:
parent
9451ea8df1
commit
75d8902371
@ -62,8 +62,11 @@ fn main() {
|
|||||||
|
|
||||||
// Inclusive ranges methods stabilized in Rust 1.27:
|
// Inclusive ranges methods stabilized in Rust 1.27:
|
||||||
// https://github.com/rust-lang/rust/pull/50758
|
// https://github.com/rust-lang/rust/pull/50758
|
||||||
|
// Also Iterator::try_for_each:
|
||||||
|
// https://blog.rust-lang.org/2018/06/21/Rust-1.27.html#library-stabilizations
|
||||||
if minor >= 27 {
|
if minor >= 27 {
|
||||||
println!("cargo:rustc-cfg=range_inclusive");
|
println!("cargo:rustc-cfg=range_inclusive");
|
||||||
|
println!("cargo:rustc-cfg=iterator_try_fold");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Non-zero integers stabilized in Rust 1.28:
|
// Non-zero integers stabilized in Rust 1.28:
|
||||||
|
@ -1279,9 +1279,20 @@ pub trait Serializer: Sized {
|
|||||||
{
|
{
|
||||||
let iter = iter.into_iter();
|
let iter = iter.into_iter();
|
||||||
let mut serializer = try!(self.serialize_seq(iterator_len_hint(&iter)));
|
let mut serializer = try!(self.serialize_seq(iterator_len_hint(&iter)));
|
||||||
for item in iter {
|
|
||||||
try!(serializer.serialize_element(&item));
|
#[cfg(iterator_try_fold)]
|
||||||
|
{
|
||||||
|
let mut iter = iter;
|
||||||
|
try!(iter.try_for_each(|item| serializer.serialize_element(&item)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(iterator_try_fold))]
|
||||||
|
{
|
||||||
|
for item in iter {
|
||||||
|
try!(serializer.serialize_element(&item));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
serializer.end()
|
serializer.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1319,9 +1330,20 @@ pub trait Serializer: Sized {
|
|||||||
{
|
{
|
||||||
let iter = iter.into_iter();
|
let iter = iter.into_iter();
|
||||||
let mut serializer = try!(self.serialize_map(iterator_len_hint(&iter)));
|
let mut serializer = try!(self.serialize_map(iterator_len_hint(&iter)));
|
||||||
for (key, value) in iter {
|
|
||||||
try!(serializer.serialize_entry(&key, &value));
|
#[cfg(iterator_try_fold)]
|
||||||
|
{
|
||||||
|
let mut iter = iter;
|
||||||
|
try!(iter.try_for_each(|(key, value)| serializer.serialize_entry(&key, &value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(iterator_try_fold))]
|
||||||
|
{
|
||||||
|
for (key, value) in iter {
|
||||||
|
try!(serializer.serialize_entry(&key, &value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
serializer.end()
|
serializer.end()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user