Skip to content

Commit 4d8715a

Browse files
authored
Merge pull request #22 from cuviper/sources
Add free fns as iterator sources
2 parents 3e01f0d + a269b0f commit 4d8715a

File tree

2 files changed

+530
-193
lines changed

2 files changed

+530
-193
lines changed

src/lib.rs

Lines changed: 11 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,17 @@
4444
extern crate core;
4545

4646
use core::cmp;
47-
use core::marker::PhantomData;
47+
48+
mod sources;
49+
pub use sources::{convert, Convert};
50+
pub use sources::{convert_ref, ConvertRef};
51+
pub use sources::{empty, Empty};
52+
pub use sources::{from_fn, FromFn};
53+
pub use sources::{once, Once};
54+
pub use sources::{once_with, OnceWith};
55+
pub use sources::{repeat, Repeat};
56+
pub use sources::{repeat_with, RepeatWith};
57+
pub use sources::{successors, Successors};
4858

4959
/// An interface for dealing with streaming iterators.
5060
pub trait StreamingIterator {
@@ -507,67 +517,6 @@ pub trait DoubleEndedStreamingIterator: StreamingIterator {
507517
}
508518
}
509519

510-
/// Turns a normal, non-streaming iterator into a streaming iterator.
511-
///
512-
/// ```
513-
/// # use streaming_iterator::{StreamingIterator, convert};
514-
/// let scores = vec![100, 50, 80];
515-
/// let mut streaming_iter = convert(scores);
516-
/// while let Some(score) = streaming_iter.next() {
517-
/// println!("The score is: {}", score);
518-
/// }
519-
/// ```
520-
#[inline]
521-
pub fn convert<I>(it: I) -> Convert<I::IntoIter>
522-
where
523-
I: IntoIterator,
524-
{
525-
Convert { it: it.into_iter(), item: None }
526-
}
527-
528-
/// Turns an iterator of references into a streaming iterator.
529-
#[inline]
530-
pub fn convert_ref<'a, I, T: ?Sized>(iterator: I) -> ConvertRef<'a, I::IntoIter, T>
531-
where
532-
I: IntoIterator<Item = &'a T>,
533-
{
534-
ConvertRef {
535-
it: iterator.into_iter(),
536-
item: None,
537-
}
538-
}
539-
540-
/// A simple iterator that returns nothing
541-
#[derive(Clone, Debug)]
542-
pub struct Empty<I> {
543-
phantom: PhantomData<I>,
544-
}
545-
546-
impl<I> StreamingIterator for Empty<I> {
547-
type Item = I;
548-
549-
#[inline]
550-
fn advance(&mut self) {}
551-
552-
#[inline]
553-
fn get(&self) -> Option<&Self::Item> {
554-
None
555-
}
556-
}
557-
558-
impl<I> DoubleEndedStreamingIterator for Empty<I> {
559-
#[inline]
560-
fn advance_back(&mut self) {}
561-
}
562-
563-
/// Creates an empty iterator
564-
#[inline]
565-
pub fn empty<I>() -> Empty<I> {
566-
Empty {
567-
phantom: PhantomData,
568-
}
569-
}
570-
571520
/// A streaming iterator that concatenates two streaming iterators
572521
#[derive(Debug)]
573522
pub struct Chain<A, B> {
@@ -727,137 +676,6 @@ where
727676
}
728677
}
729678

730-
/// A streaming iterator which yields elements from a normal, non-streaming, iterator.
731-
#[derive(Clone, Debug)]
732-
pub struct Convert<I>
733-
where
734-
I: Iterator,
735-
{
736-
it: I,
737-
item: Option<I::Item>,
738-
}
739-
740-
impl<I> StreamingIterator for Convert<I>
741-
where
742-
I: Iterator,
743-
{
744-
type Item = I::Item;
745-
746-
#[inline]
747-
fn advance(&mut self) {
748-
self.item = self.it.next();
749-
}
750-
751-
#[inline]
752-
fn get(&self) -> Option<&I::Item> {
753-
self.item.as_ref()
754-
}
755-
756-
#[inline]
757-
fn size_hint(&self) -> (usize, Option<usize>) {
758-
self.it.size_hint()
759-
}
760-
761-
#[inline]
762-
fn count(self) -> usize {
763-
self.it.count()
764-
}
765-
766-
#[inline]
767-
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
768-
where
769-
Self: Sized,
770-
Fold: FnMut(Acc, &Self::Item) -> Acc,
771-
{
772-
self.it.fold(init, move |acc, item| f(acc, &item))
773-
}
774-
}
775-
776-
impl<I> DoubleEndedStreamingIterator for Convert<I>
777-
where
778-
I: DoubleEndedIterator,
779-
{
780-
#[inline]
781-
fn advance_back(&mut self) {
782-
self.item = self.it.next_back();
783-
}
784-
785-
#[inline]
786-
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
787-
where
788-
Self: Sized,
789-
Fold: FnMut(Acc, &Self::Item) -> Acc,
790-
{
791-
self.it.rev().fold(init, move |acc, item| f(acc, &item))
792-
}
793-
}
794-
795-
/// A streaming iterator which yields elements from an iterator of references.
796-
#[derive(Clone, Debug)]
797-
pub struct ConvertRef<'a, I, T: ?Sized>
798-
where
799-
I: Iterator<Item = &'a T>,
800-
T: 'a,
801-
{
802-
it: I,
803-
item: Option<&'a T>,
804-
}
805-
806-
impl<'a, I, T: ?Sized> StreamingIterator for ConvertRef<'a, I, T>
807-
where
808-
I: Iterator<Item = &'a T>,
809-
{
810-
type Item = T;
811-
812-
#[inline]
813-
fn advance(&mut self) {
814-
self.item = self.it.next();
815-
}
816-
817-
#[inline]
818-
fn get(&self) -> Option<&T> {
819-
self.item
820-
}
821-
822-
#[inline]
823-
fn size_hint(&self) -> (usize, Option<usize>) {
824-
self.it.size_hint()
825-
}
826-
827-
#[inline]
828-
fn count(self) -> usize {
829-
self.it.count()
830-
}
831-
832-
#[inline]
833-
fn fold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
834-
where
835-
Self: Sized,
836-
Fold: FnMut(Acc, &Self::Item) -> Acc,
837-
{
838-
self.it.fold(init, move |acc, item| f(acc, item))
839-
}
840-
}
841-
842-
impl<'a, I, T: ?Sized> DoubleEndedStreamingIterator for ConvertRef<'a, I, T>
843-
where
844-
I: DoubleEndedIterator<Item = &'a T>,
845-
{
846-
#[inline]
847-
fn advance_back(&mut self) {
848-
self.item = self.it.next_back();
849-
}
850-
851-
#[inline]
852-
fn rfold<Acc, Fold>(self, init: Acc, mut f: Fold) -> Acc
853-
where
854-
Self: Sized,
855-
Fold: FnMut(Acc, &Self::Item) -> Acc,
856-
{
857-
self.it.rev().fold(init, move |acc, item| f(acc, item))
858-
}
859-
}
860-
861679
/// A streaming iterator which filters the elements of a streaming iterator with a predicate.
862680
#[derive(Debug)]
863681
pub struct Filter<I, F> {

0 commit comments

Comments
 (0)