Skip to content

Commit

Permalink
Fix for the new coherence rules which prevents the blank impl for Stream
Browse files Browse the repository at this point in the history
Current fix is to wrap iterators in a newtype using the `from_iter` function.
  • Loading branch information
Marwes committed Apr 3, 2015
1 parent f3d5456 commit c3c54a8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,6 @@ mod tests {
use super::*;
use primitives::Parser;
use char::{digit, string};
use std::num::Int;

#[test]
fn chainr1_test() {
Expand Down
13 changes: 6 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![unstable]
#![feature(collections, into_cow, slice_patterns)]
#![cfg_attr(test, feature(core, test))]
#![feature(into_cow)]
#![cfg_attr(test, feature(test))]

//!This crate contains parser combinators, roughly based on the Haskell library [parsec](http://hackage.haskell.org/package/parsec).
//!
Expand Down Expand Up @@ -81,7 +80,7 @@
extern crate test;

#[doc(inline)]
pub use primitives::{Parser, ParseResult, ParseError};
pub use primitives::{Parser, ParseResult, ParseError, from_iter};
#[doc(inline)]
pub use char::{
any_char,
Expand Down Expand Up @@ -168,9 +167,9 @@ mod tests {
}
#[test]
fn iterator() {
let result = parser(integer).parse("123".chars())
.map(|(i, mut input)| (i, input.next()));
assert_eq!(result, Ok((123i64, None)));
let result = parser(integer).parse(from_iter("123".chars()))
.map(|(i, input)| (i, input.uncons().err()));
assert_eq!(result, Ok((123i64, Some(()))));
}
#[test]
fn field() {
Expand Down
31 changes: 21 additions & 10 deletions src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,16 +263,6 @@ pub trait Stream : Clone {
fn uncons(self) -> Result<(Self::Item, Self), ()>;
}

impl <I: Iterator + Clone> Stream for I {
type Item = <I as Iterator>::Item;
fn uncons(mut self) -> Result<(I::Item, Self), ()> {
match self.next() {
Some(x) => Ok((x, self)),
None => Err(())
}
}
}

impl <'a> Stream for &'a str {
type Item = char;
fn uncons(self) -> Result<(char, &'a str), ()> {
Expand All @@ -295,6 +285,27 @@ impl <'a, T> Stream for &'a [T] {
}
}

#[derive(Clone, Debug)]
pub struct IteratorStream<I>(I)
where I: Iterator + Clone;


///Converts an `Iterator` into a stream.
pub fn from_iter<I>(iter: I) -> IteratorStream<I>
where I: Iterator + Clone {
IteratorStream(iter)
}

impl <I: Iterator + Clone> Stream for IteratorStream<I> {
type Item = <I as Iterator>::Item;
fn uncons(mut self) -> Result<(I::Item, Self), ()> {
match self.0.next() {
Some(x) => Ok((x, self)),
None => Err(())
}
}
}

///By implementing the `Parser` trait a type says that it can be used to parse an input stream into
///the type `Output`.
pub trait Parser {
Expand Down

0 comments on commit c3c54a8

Please sign in to comment.