Skip to content

Commit 131b030

Browse files
committed
Add "nightly" feature that impls TrustedLen and a few more methods for iterators
1 parent 836288d commit 131b030

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ serde = { version = "1.0.95", optional = true, default-features = false, feature
2424
default = ["std"]
2525
std = []
2626
use_std = ["std"] # deprecated alias
27+
nightly = []
2728

2829
[dev-dependencies]
2930
serde_json = "1.0.0"

src/iterator.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ where
130130
{
131131
for_both!(self, inner => inner.position(predicate))
132132
}
133+
134+
#[cfg(feature = "nightly")]
135+
fn advance_by(&mut self, n: usize) -> Result<(), core::num::NonZeroUsize> {
136+
for_both!(self, inner => inner.advance_by(n))
137+
}
138+
139+
#[cfg(feature = "nightly")]
140+
fn try_fold<B, F, T>(&mut self, init: B, f: F) -> T
141+
where
142+
Self: Sized,
143+
F: FnMut(B, Self::Item) -> T,
144+
T: core::ops::Try<Output = B>,
145+
{
146+
for_both!(self, inner => inner.try_fold(init, f))
147+
}
133148
}
134149

135150
impl<L, R> DoubleEndedIterator for Either<L, R>
@@ -158,6 +173,21 @@ where
158173
{
159174
for_both!(self, inner => inner.rfind(predicate))
160175
}
176+
177+
#[cfg(feature = "nightly")]
178+
fn advance_back_by(&mut self, n: usize) -> Result<(), core::num::NonZeroUsize> {
179+
for_both!(self, ref mut inner => inner.advance_back_by(n))
180+
}
181+
182+
#[cfg(feature = "nightly")]
183+
fn try_rfold<B, F, T>(&mut self, init: B, f: F) -> T
184+
where
185+
Self: Sized,
186+
F: FnMut(B, Self::Item) -> T,
187+
T: core::ops::Try<Output = B>,
188+
{
189+
for_both!(self, inner => inner.try_fold(init, f))
190+
}
161191
}
162192

163193
impl<L, R> ExactSizeIterator for Either<L, R>
@@ -170,6 +200,14 @@ where
170200
}
171201
}
172202

203+
#[cfg(feature = "nightly")]
204+
unsafe impl<L, R> iter::TrustedLen for Either<L, R>
205+
where
206+
L: iter::TrustedLen,
207+
R: iter::TrustedLen<Item = L::Item>,
208+
{
209+
}
210+
173211
impl<L, R> iter::FusedIterator for Either<L, R>
174212
where
175213
L: iter::FusedIterator,
@@ -267,6 +305,21 @@ where
267305
{
268306
wrap_either!(&mut self.inner => .position(predicate))
269307
}
308+
309+
#[cfg(feature = "nightly")]
310+
fn advance_by(&mut self, n: usize) -> Result<(), core::num::NonZeroUsize> {
311+
for_both!(self.inner, ref mut inner => inner.advance_by(n))
312+
}
313+
314+
#[cfg(feature = "nightly")]
315+
fn try_fold<B, F, T>(&mut self, init: B, f: F) -> T
316+
where
317+
Self: Sized,
318+
F: FnMut(B, Self::Item) -> T,
319+
T: core::ops::Try<Output = B>,
320+
{
321+
wrap_either!(&mut self.inner => .try_fold(init, f))
322+
}
270323
}
271324

272325
impl<L, R> DoubleEndedIterator for IterEither<L, R>
@@ -295,6 +348,21 @@ where
295348
{
296349
wrap_either!(&mut self.inner => .rfind(predicate))
297350
}
351+
352+
#[cfg(feature = "nightly")]
353+
fn advance_back_by(&mut self, n: usize) -> Result<(), core::num::NonZeroUsize> {
354+
for_both!(self.inner, ref mut inner => inner.advance_back_by(n))
355+
}
356+
357+
#[cfg(feature = "nightly")]
358+
fn try_rfold<B, F, T>(&mut self, init: B, f: F) -> T
359+
where
360+
Self: Sized,
361+
F: FnMut(B, Self::Item) -> T,
362+
T: core::ops::Try<Output = B>,
363+
{
364+
wrap_either!(&mut self.inner => .try_rfold(init, f))
365+
}
298366
}
299367

300368
impl<L, R> ExactSizeIterator for IterEither<L, R>
@@ -307,6 +375,14 @@ where
307375
}
308376
}
309377

378+
#[cfg(feature = "nightly")]
379+
unsafe impl<L, R> iter::TrustedLen for IterEither<L, R>
380+
where
381+
L: iter::TrustedLen,
382+
R: iter::TrustedLen,
383+
{
384+
}
385+
310386
impl<L, R> iter::FusedIterator for IterEither<L, R>
311387
where
312388
L: iter::FusedIterator,

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@
1111
//! * `"serde"`
1212
//! Disabled by default. Enable to `#[derive(Serialize, Deserialize)]` for `Either`
1313
//!
14+
//! * `"nightly"`
15+
//! Disabled by default. Enable to use nightly-only features for `Iterator` implementations.
16+
//! This feature should be considered unstable and may break at any time.
17+
//!
1418
19+
#![cfg_attr(
20+
feature = "nightly",
21+
feature(iter_advance_by, try_trait_v2, trusted_len)
22+
)]
1523
#![doc(html_root_url = "https://docs.rs/either/1/")]
1624
#![no_std]
1725

0 commit comments

Comments
 (0)