Skip to content

Commit

Permalink
InputTake should not work on references
Browse files Browse the repository at this point in the history
this makes it hard to implement for types other than &[u8] and &str
  • Loading branch information
Geal committed Nov 26, 2017
1 parent 9f8a210 commit 784b1f9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ pub trait InputIter {
}

/// abstracts slicing operations
pub trait InputTake {
pub trait InputTake: Sized {
/// returns a slice of `count` bytes
fn take(&self, count: usize) -> Option<&Self>;
fn take(&self, count: usize) -> Option<Self>;
/// split the stream at the `count` byte offset
fn take_split(&self, count: usize) -> Option<(&Self,&Self)>;
fn take_split(&self, count: usize) -> Option<(Self,Self)>;
}

impl<'a> InputIter for &'a [u8] {
Expand Down Expand Up @@ -229,17 +229,17 @@ impl<'a> InputIter for &'a [u8] {
}
}

impl InputTake for [u8] {
impl<'a> InputTake for &'a [u8] {
#[inline]
fn take(&self, count: usize) -> Option<&Self> {
fn take(&self, count: usize) -> Option<Self> {
if self.len() >= count {
Some(&self[0..count])
} else {
None
}
}
#[inline]
fn take_split(&self, count: usize) -> Option<(&Self,&Self)> {
fn take_split(&self, count: usize) -> Option<(Self,Self)> {
if self.len() >= count {
Some((&self[count..],&self[..count]))
} else {
Expand Down Expand Up @@ -285,9 +285,9 @@ impl<'a> InputIter for &'a str {
}
}

impl InputTake for str {
impl<'a> InputTake for &'a str {
#[inline]
fn take(&self, count: usize) -> Option<&Self> {
fn take(&self, count: usize) -> Option<Self> {
let mut cnt = 0;
for (index, _) in self.char_indices() {
if cnt == count {
Expand All @@ -300,7 +300,7 @@ impl InputTake for str {

// return byte index
#[inline]
fn take_split(&self, count: usize) -> Option<(&Self,&Self)> {
fn take_split(&self, count: usize) -> Option<(Self,Self)> {
let mut cnt = 0;
for (index, _) in self.char_indices() {
if cnt == count {
Expand Down Expand Up @@ -582,7 +582,7 @@ pub trait AtEof {

pub fn need_more<I: AtEof, O, E>(input: I, needed: Needed) -> IResult<I, O, E> {
if input.at_eof() {
Err(Err::Error(Context::Code(input, ErrorKind::Eof)))
Err(Err::Failure(Context::Code(input, ErrorKind::Eof)))
} else {
Err(Err::Incomplete(needed))
}
Expand Down
21 changes: 9 additions & 12 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use traits::{AtEof,Compare,CompareResult,InputLength,InputIter,InputTake,Slice,FindSubstring,ParseTo};
use util::Offset;

use std::str::{self,FromStr,Chars,CharIndices};
use std::ops::{Range,RangeTo,RangeFrom,RangeFull};
Expand Down Expand Up @@ -56,17 +57,15 @@ impl<'a> InputIter for CompleteStr<'a> {
}
}

/*
impl<'a> InputTake for CompleteStr<'a> {
fn take(&self, count: usize) -> Option<&Self> {
self.0.take(count).map(|s| &CompleteStr(s))
fn take(&self, count: usize) -> Option<Self> {
self.0.take(count).map(|s| CompleteStr(s))
}

fn take_split(&self, count: usize) -> Option<(&Self,&Self)> {
self.0.take_split(count).map(|(s1, s2)| (&CompleteStr(s1), &CompleteStr(s2)))
fn take_split(&self, count: usize) -> Option<(Self,Self)> {
self.0.take_split(count).map(|(s1, s2)| (CompleteStr(s1), CompleteStr(s2)))
}
}
*/

impl<'a> InputLength for CompleteStr<'a> {
fn input_len(&self) -> usize {
Expand Down Expand Up @@ -147,17 +146,15 @@ impl<'a> InputIter for CompleteByteSlice<'a> {
}
}

/*
impl<'a> InputTake for CompleteByteSlice<'a> {
fn take(&self, count: usize) -> Option<&Self> {
self.0.take(count).map(|s| &CompleteByteSlice(s))
fn take(&self, count: usize) -> Option<Self> {
self.0.take(count).map(|s| CompleteByteSlice(s))
}

fn take_split(&self, count: usize) -> Option<(&Self,&Self)> {
self.0.take_split(count).map(|(s1, s2)| (&CompleteByteSlice(s1), &CompleteByteSlice(s2)))
fn take_split(&self, count: usize) -> Option<(Self,Self)> {
self.0.take_split(count).map(|(s1, s2)| (CompleteByteSlice(s1), CompleteByteSlice(s2)))
}
}
*/

impl<'a> InputLength for CompleteByteSlice<'a> {
fn input_len(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion tests/complete_arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ fn parens_test() {
assert_eq!(expr(input2), Ok((CompleteStr(""), 4)));

let input3 = CompleteStr(" 2*2 / ( 5 - 1) + ");
assert_eq!(expr(input3), Ok((CompleteStr("+ "), 1)));
assert_eq!(expr(input3), Err(nom::Err::Failure(error_position!(ErrorKind::Eof, CompleteStr("")))));
}

0 comments on commit 784b1f9

Please sign in to comment.