Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #70404

Merged
merged 31 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
513ea64
add missing visit_consts
lcnr Mar 23, 2020
bda976d
add missing const super folds
lcnr Mar 23, 2020
d7ecc8c
query normalize_generic_arg_after_erasing_regions
lcnr Mar 23, 2020
03bb3bd
Add long error explanation for E0710 #61137
bishtpawan Mar 24, 2020
cd9921e
Refactor changes
bishtpawan Mar 24, 2020
10226da
Update tools_lints
bishtpawan Mar 24, 2020
6c4d5d9
improve normalize cycle error
lcnr Mar 24, 2020
1509160
Add explanation for inner attribute
bishtpawan Mar 24, 2020
b31707e
Remove unknown lint from deny attribute
bishtpawan Mar 24, 2020
11763d4
update mir opt test
lcnr Mar 24, 2020
212e6ce
Implement Fuse with Option
cuviper Mar 22, 2020
bedc358
fix type name typo in doc comments
JOE1994 Mar 25, 2020
5c65568
update tool_lints
bishtpawan Mar 25, 2020
5be304b
miri: simplify shift operator overflow checking
RalfJung Mar 17, 2020
1ddbdc6
use checked casts and arithmetic in Miri engine
RalfJung Mar 21, 2020
9de6008
make bit_width return u64, consistently with other sizes in the compiler
RalfJung Mar 21, 2020
cd15b65
avoid double-cast in mplace_field
RalfJung Mar 21, 2020
d7e2650
miri: avoid a bunch of casts by offering usized-based field indexing
RalfJung Mar 21, 2020
f16b491
remove unnecessary cast
RalfJung Mar 21, 2020
0bc108a
make Size::from* methods generic in the integer type they accept
RalfJung Mar 22, 2020
afcb634
use Size addition instead of checked int addition
RalfJung Mar 24, 2020
1d67ca0
add helper method for ptr ops on Scalar; reduce unnecessary large ope…
RalfJung Mar 24, 2020
b7db732
go back to infix ops for Size
RalfJung Mar 24, 2020
7400955
add usize methods for Size getters
RalfJung Mar 24, 2020
f8e3da5
run test only on 64bit
lcnr Mar 25, 2020
4f429c0
impl TrustedRandomAccess for Fuse without FusedIterator
cuviper Mar 25, 2020
97f0a9e
Rollup merge of #70226 - RalfJung:checked, r=oli-obk
Dylan-DPC Mar 25, 2020
1154023
Rollup merge of #70319 - lcnr:issue63695, r=eddyb
Dylan-DPC Mar 25, 2020
3586ab6
Rollup merge of #70352 - bishtpawan:doc/61137-add-long-error-code-e07…
Dylan-DPC Mar 25, 2020
530c320
Rollup merge of #70366 - cuviper:option-fuse, r=dtolnay
Dylan-DPC Mar 25, 2020
9a9cb2d
Rollup merge of #70379 - JOE1994:patch-2, r=petrochenkov
Dylan-DPC Mar 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
342 changes: 342 additions & 0 deletions src/libcore/iter/adapters/fuse.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,342 @@
use crate::intrinsics;
use crate::iter::{
DoubleEndedIterator, ExactSizeIterator, FusedIterator, Iterator, TrustedRandomAccess,
};
use crate::ops::Try;

/// An iterator that yields `None` forever after the underlying iterator
/// yields `None` once.
///
/// This `struct` is created by the [`fuse`] method on [`Iterator`]. See its
/// documentation for more.
///
/// [`fuse`]: trait.Iterator.html#method.fuse
/// [`Iterator`]: trait.Iterator.html
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Fuse<I> {
// NOTE: for `I: FusedIterator`, this is always assumed `Some`!
iter: Option<I>,
}
impl<I> Fuse<I> {
pub(in crate::iter) fn new(iter: I) -> Fuse<I> {
Fuse { iter: Some(iter) }
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<I> FusedIterator for Fuse<I> where I: Iterator {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> Iterator for Fuse<I>
where
I: Iterator,
{
type Item = <I as Iterator>::Item;

#[inline]
default fn next(&mut self) -> Option<<I as Iterator>::Item> {
let next = self.iter.as_mut()?.next();
if next.is_none() {
self.iter = None;
}
next
}

#[inline]
default fn nth(&mut self, n: usize) -> Option<I::Item> {
let nth = self.iter.as_mut()?.nth(n);
if nth.is_none() {
self.iter = None;
}
nth
}

#[inline]
default fn last(self) -> Option<I::Item> {
self.iter?.last()
}

#[inline]
default fn count(self) -> usize {
self.iter.map_or(0, I::count)
}

#[inline]
default fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.as_ref().map_or((0, Some(0)), I::size_hint)
}

#[inline]
default fn try_fold<Acc, Fold, R>(&mut self, mut acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Ok = Acc>,
{
if let Some(ref mut iter) = self.iter {
acc = iter.try_fold(acc, fold)?;
self.iter = None;
}
Try::from_ok(acc)
}

#[inline]
default fn fold<Acc, Fold>(self, mut acc: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
if let Some(iter) = self.iter {
acc = iter.fold(acc, fold);
}
acc
}

#[inline]
default fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
{
let found = self.iter.as_mut()?.find(predicate);
if found.is_none() {
self.iter = None;
}
found
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> DoubleEndedIterator for Fuse<I>
where
I: DoubleEndedIterator,
{
#[inline]
default fn next_back(&mut self) -> Option<<I as Iterator>::Item> {
let next = self.iter.as_mut()?.next_back();
if next.is_none() {
self.iter = None;
}
next
}

#[inline]
default fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
let nth = self.iter.as_mut()?.nth_back(n);
if nth.is_none() {
self.iter = None;
}
nth
}

#[inline]
default fn try_rfold<Acc, Fold, R>(&mut self, mut acc: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Ok = Acc>,
{
if let Some(ref mut iter) = self.iter {
acc = iter.try_rfold(acc, fold)?;
self.iter = None;
}
Try::from_ok(acc)
}

#[inline]
default fn rfold<Acc, Fold>(self, mut acc: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
if let Some(iter) = self.iter {
acc = iter.rfold(acc, fold);
}
acc
}

#[inline]
default fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
{
let found = self.iter.as_mut()?.rfind(predicate);
if found.is_none() {
self.iter = None;
}
found
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> ExactSizeIterator for Fuse<I>
where
I: ExactSizeIterator,
{
default fn len(&self) -> usize {
self.iter.as_ref().map_or(0, I::len)
}

default fn is_empty(&self) -> bool {
self.iter.as_ref().map_or(true, I::is_empty)
}
}

// NOTE: for `I: FusedIterator`, we assume that the iterator is always `Some`
impl<I: FusedIterator> Fuse<I> {
#[inline(always)]
fn as_inner(&self) -> &I {
match self.iter {
Some(ref iter) => iter,
// SAFETY: the specialized iterator never sets `None`
None => unsafe { intrinsics::unreachable() },
}
}

#[inline(always)]
fn as_inner_mut(&mut self) -> &mut I {
match self.iter {
Some(ref mut iter) => iter,
// SAFETY: the specialized iterator never sets `None`
None => unsafe { intrinsics::unreachable() },
}
}

#[inline(always)]
fn into_inner(self) -> I {
match self.iter {
Some(iter) => iter,
// SAFETY: the specialized iterator never sets `None`
None => unsafe { intrinsics::unreachable() },
}
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<I> Iterator for Fuse<I>
where
I: FusedIterator,
{
#[inline]
fn next(&mut self) -> Option<<I as Iterator>::Item> {
self.as_inner_mut().next()
}

#[inline]
fn nth(&mut self, n: usize) -> Option<I::Item> {
self.as_inner_mut().nth(n)
}

#[inline]
fn last(self) -> Option<I::Item> {
self.into_inner().last()
}

#[inline]
fn count(self) -> usize {
self.into_inner().count()
}

#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.as_inner().size_hint()
}

#[inline]
fn try_fold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Ok = Acc>,
{
self.as_inner_mut().try_fold(init, fold)
}

#[inline]
fn fold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.into_inner().fold(init, fold)
}

#[inline]
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
{
self.as_inner_mut().find(predicate)
}
}

#[stable(feature = "fused", since = "1.26.0")]
impl<I> DoubleEndedIterator for Fuse<I>
where
I: DoubleEndedIterator + FusedIterator,
{
#[inline]
fn next_back(&mut self) -> Option<<I as Iterator>::Item> {
self.as_inner_mut().next_back()
}

#[inline]
fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item> {
self.as_inner_mut().nth_back(n)
}

#[inline]
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, fold: Fold) -> R
where
Self: Sized,
Fold: FnMut(Acc, Self::Item) -> R,
R: Try<Ok = Acc>,
{
self.as_inner_mut().try_rfold(init, fold)
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.into_inner().rfold(init, fold)
}

#[inline]
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where
P: FnMut(&Self::Item) -> bool,
{
self.as_inner_mut().rfind(predicate)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<I> ExactSizeIterator for Fuse<I>
where
I: ExactSizeIterator + FusedIterator,
{
fn len(&self) -> usize {
self.as_inner().len()
}

fn is_empty(&self) -> bool {
self.as_inner().is_empty()
}
}

unsafe impl<I> TrustedRandomAccess for Fuse<I>
where
I: TrustedRandomAccess,
{
unsafe fn get_unchecked(&mut self, i: usize) -> I::Item {
match self.iter {
Some(ref mut iter) => iter.get_unchecked(i),
// SAFETY: the caller asserts there is an item at `i`, so we're not exhausted.
None => intrinsics::unreachable(),
}
}

fn may_have_side_effect() -> bool {
I::may_have_side_effect()
}
}
Loading