Skip to content

Commit

Permalink
impl for &[T] where T : clone
Browse files Browse the repository at this point in the history
  • Loading branch information
asibahi committed Feb 25, 2025
1 parent a44b52e commit 0e811ba
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//! Traits input types have to implement to work with nom combinators
use core::iter::Enumerate;
use core::iter::{Cloned, Enumerate};
use core::str::CharIndices;

use crate::error::{ErrorKind, ParseError};
use crate::internal::{Err, IResult, Needed};
use crate::lib::std::iter::Copied;
use crate::lib::std::ops::{
Bound, Range, RangeBounds, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive,
};
Expand Down Expand Up @@ -190,9 +189,9 @@ pub trait Input: Clone + Sized {
}
}

impl<'a> Input for &'a [u8] {
type Item = u8;
type Iter = Copied<Iter<'a, u8>>;
impl<'a, T> Input for &'a [T] where T : Clone {
type Item = T;
type Iter = Cloned<Iter<'a, T>>;
type IterIndices = Enumerate<Self::Iter>;

fn input_len(&self) -> usize {
Expand All @@ -218,12 +217,12 @@ impl<'a> Input for &'a [u8] {
where
P: Fn(Self::Item) -> bool,
{
self.iter().position(|b| predicate(*b))
self.iter().position(|b| predicate(b.clone()))
}

#[inline]
fn iter_elements(&self) -> Self::Iter {
self.iter().copied()
self.iter().cloned()
}

#[inline]
Expand All @@ -245,7 +244,7 @@ impl<'a> Input for &'a [u8] {
where
P: Fn(Self::Item) -> bool,
{
match self.iter().position(|c| predicate(*c)) {
match self.iter().position(|c| predicate(c.clone())) {
Some(i) => Ok(self.take_split(i)),
None => Err(Err::Incomplete(Needed::new(1))),
}
Expand All @@ -260,7 +259,7 @@ impl<'a> Input for &'a [u8] {
where
P: Fn(Self::Item) -> bool,
{
match self.iter().position(|c| predicate(*c)) {
match self.iter().position(|c| predicate(c.clone())) {
Some(0) => Err(Err::Error(E::from_error_kind(self, e))),
Some(i) => Ok(self.take_split(i)),
None => Err(Err::Incomplete(Needed::new(1))),
Expand All @@ -274,7 +273,7 @@ impl<'a> Input for &'a [u8] {
where
P: Fn(Self::Item) -> bool,
{
match self.iter().position(|c| predicate(*c)) {
match self.iter().position(|c| predicate(c.clone())) {
Some(i) => Ok(self.take_split(i)),
None => Ok(self.take_split(self.len())),
}
Expand All @@ -289,7 +288,7 @@ impl<'a> Input for &'a [u8] {
where
P: Fn(Self::Item) -> bool,
{
match self.iter().position(|c| predicate(*c)) {
match self.iter().position(|c| predicate(c.clone())) {
Some(0) => Err(Err::Error(E::from_error_kind(self, e))),
Some(i) => Ok(self.take_split(i)),
None => {
Expand All @@ -311,7 +310,7 @@ impl<'a> Input for &'a [u8] {
where
P: Fn(Self::Item) -> bool,
{
match self.iter().position(|c| predicate(*c)) {
match self.iter().position(|c| predicate(c.clone())) {
Some(n) => Ok((self.take_from(n), OM::Output::bind(|| self.take(n)))),
None => {
if OM::Incomplete::is_streaming() {
Expand All @@ -336,7 +335,7 @@ impl<'a> Input for &'a [u8] {
where
P: Fn(Self::Item) -> bool,
{
match self.iter().position(|c| predicate(*c)) {
match self.iter().position(|c| predicate(c.clone())) {
Some(0) => Err(Err::Error(OM::Error::bind(|| E::from_error_kind(self, e)))),
Some(n) => Ok((self.take_from(n), OM::Output::bind(|| self.take(n)))),
None => {
Expand Down

0 comments on commit 0e811ba

Please sign in to comment.