Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/number/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ use crate::traits::ParseTo;
/// ```
pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
where
T: Clone + Offset + ParseTo<f32> + Compare<&'static str>,
T: Clone + Offset + ParseTo + Compare<&'static str>,
T: Input,
<T as Input>::Item: AsChar,
<T as Input>::Iter: Clone,
Expand Down Expand Up @@ -1457,7 +1457,7 @@ where
/// ```
pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
where
T: Clone + Offset + ParseTo<f64> + Compare<&'static str>,
T: Clone + Offset + ParseTo + Compare<&'static str>,
T: Input,
<T as Input>::Item: AsChar,
<T as Input>::Iter: Clone,
Expand Down
8 changes: 5 additions & 3 deletions src/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use core::{
marker::PhantomData,
ops::{Add, Shl},
str::FromStr,
};

use crate::{
Expand Down Expand Up @@ -1350,7 +1351,7 @@ where
pub fn float<T, E: ParseError<T>>() -> impl Parser<T, Output = f32, Error = E>
where
T: Clone + Offset,
T: Input + crate::traits::ParseTo<f32> + Compare<&'static str>,
T: Input + crate::traits::ParseTo + Compare<&'static str>,
<T as Input>::Item: AsChar + Clone,
T: AsBytes,
T: for<'a> Compare<&'a [u8]>,
Expand All @@ -1365,7 +1366,7 @@ where
pub fn double<T, E: ParseError<T>>() -> impl Parser<T, Output = f64, Error = E>
where
T: Clone + Offset,
T: Input + crate::traits::ParseTo<f64> + Compare<&'static str>,
T: Input + crate::traits::ParseTo + Compare<&'static str>,
<T as Input>::Item: AsChar + Clone,
T: AsBytes,
T: for<'a> Compare<&'a [u8]>,
Expand All @@ -1385,10 +1386,11 @@ struct Float<O, E> {
impl<I, O, E: ParseError<I>> Parser<I> for Float<O, E>
where
I: Clone + Offset,
I: Input + crate::traits::ParseTo<O> + Compare<&'static str>,
I: Input + crate::traits::ParseTo + Compare<&'static str>,
<I as Input>::Item: AsChar + Clone,
I: AsBytes,
I: for<'a> Compare<&'a [u8]>,
O: FromStr,
{
type Output = O;
type Error = E;
Expand Down
6 changes: 3 additions & 3 deletions src/number/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::error::{ErrorKind, ParseError};
use crate::lib::std::ops::{Add, Shl};
use crate::sequence::pair;
use crate::traits::{AsBytes, AsChar, Compare, Offset};
use crate::{internal::*, Input};
use crate::{internal::*, Input, ParseTo};

/// Recognizes an unsigned 1 byte integer.
///
Expand Down Expand Up @@ -1377,7 +1377,7 @@ where
pub fn float<T, E: ParseError<T>>(input: T) -> IResult<T, f32, E>
where
T: Clone + Offset,
T: Input + crate::traits::ParseTo<f32> + Compare<&'static str>,
T: Input + ParseTo + Compare<&'static str>,
<T as Input>::Item: AsChar + Clone,
T: AsBytes,
T: for<'a> Compare<&'a [u8]>,
Expand Down Expand Up @@ -1427,7 +1427,7 @@ where
pub fn double<T, E: ParseError<T>>(input: T) -> IResult<T, f64, E>
where
T: Clone + Offset,
T: Input + crate::traits::ParseTo<f64> + Compare<&'static str>,
T: Input + ParseTo + Compare<&'static str>,
<T as Input>::Item: AsChar + Clone,
T: AsBytes,
T: for<'a> Compare<&'a [u8]>,
Expand Down
12 changes: 6 additions & 6 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,20 +1078,20 @@ impl<'a, 'b> FindSubstring<&'b str> for &'a str {
}

/// Used to integrate `str`'s `parse()` method
pub trait ParseTo<R> {
pub trait ParseTo {
/// Succeeds if `parse()` succeeded. The byte slice implementation
/// will first convert it to a `&str`, then apply the `parse()` function
fn parse_to(&self) -> Option<R>;
fn parse_to<R: FromStr>(&self) -> Option<R>;
}

impl<'a, R: FromStr> ParseTo<R> for &'a [u8] {
fn parse_to(&self) -> Option<R> {
impl<'a> ParseTo for &'a [u8] {
fn parse_to<R: FromStr>(&self) -> Option<R> {
from_utf8(self).ok().and_then(|s| s.parse().ok())
}
}

impl<'a, R: FromStr> ParseTo<R> for &'a str {
fn parse_to(&self) -> Option<R> {
impl<'a> ParseTo for &'a str {
fn parse_to<R: FromStr>(&self) -> Option<R> {
self.parse().ok()
}
}
Expand Down