Skip to content

Commit

Permalink
Merge pull request #179 from Zij-IT/must_use
Browse files Browse the repository at this point in the history
Resolve issue #174 - #[must_use]
  • Loading branch information
zesterer committed Aug 3, 2022
2 parents 8463566 + 5b52c7e commit f17f70e
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 13 deletions.
41 changes: 38 additions & 3 deletions src/combinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub type IgnoreThen<A, B, O, U> = Map<Then<A, B>, fn((O, U)) -> U, (O, U)>;
pub type ThenIgnore<A, B, O, U> = Map<Then<A, B>, fn((O, U)) -> O, (O, U)>;

/// See [`Parser::or`].
#[must_use]
#[derive(Copy, Clone)]
pub struct Or<A, B>(pub(crate) A, pub(crate) B);

Expand Down Expand Up @@ -168,6 +169,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, B: Parser<I, O, Error = E>, E: Err
}

/// See [`Parser::or_not`].
#[must_use]
#[derive(Copy, Clone)]
pub struct OrNot<A>(pub(crate) A);

Expand Down Expand Up @@ -210,6 +212,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, E: Error<I>> Parser<I, Option<O>>
}

/// See [`Parser::then`].
#[must_use]
#[derive(Copy, Clone)]
pub struct Then<A, B>(pub(crate) A, pub(crate) B);

Expand Down Expand Up @@ -262,6 +265,7 @@ impl<I: Clone, O, U, A: Parser<I, O, Error = E>, B: Parser<I, U, Error = E>, E:
}

/// See [`Parser::then_with`]
#[must_use]
pub struct ThenWith<I, O1, O2, A, B, F>(
pub(crate) A,
pub(crate) F,
Expand Down Expand Up @@ -337,6 +341,7 @@ impl<
}

/// See [`Parser::delimited_by`].
#[must_use]
#[derive(Copy, Clone)]
pub struct DelimitedBy<A, L, R, U, V> {
pub(crate) item: A,
Expand Down Expand Up @@ -390,6 +395,7 @@ impl<
}

/// See [`Parser::repeated`].
#[must_use]
#[derive(Copy, Clone)]
pub struct Repeated<A>(pub(crate) A, pub(crate) usize, pub(crate) Option<usize>);

Expand Down Expand Up @@ -531,6 +537,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, E: Error<I>> Parser<I, Vec<O>> for
}

/// See [`Parser::separated_by`].
#[must_use]
pub struct SeparatedBy<A, B, U> {
pub(crate) item: A,
pub(crate) delimiter: B,
Expand Down Expand Up @@ -821,6 +828,7 @@ impl<I: Clone, O, U, A: Parser<I, O, Error = E>, B: Parser<I, U, Error = E>, E:
}

/// See [`Parser::debug`].
#[must_use]
pub struct Debug<A>(
pub(crate) A,
pub(crate) Rc<dyn fmt::Display>,
Expand Down Expand Up @@ -866,6 +874,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, E: Error<I>> Parser<I, O> for Debu
}

/// See [`Parser::map`].
#[must_use]
pub struct Map<A, F, O>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<O>);

impl<A: Copy, F: Copy, O> Copy for Map<A, F, O> {}
Expand Down Expand Up @@ -905,6 +914,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, U, F: Fn(O) -> U, E: Error<I>> Par
}

/// See [`Parser::map_with_span`].
#[must_use]
pub struct MapWithSpan<A, F, O>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<O>);

impl<A: Copy, F: Copy, O> Copy for MapWithSpan<A, F, O> {}
Expand Down Expand Up @@ -948,6 +958,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, U, F: Fn(O, E::Span) -> U, E: Erro
}

/// See [`Parser::validate`].
#[must_use]
pub struct Validate<A, U, F>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<U>);

impl<A: Copy, U, F: Copy> Copy for Validate<A, U, F> {}
Expand Down Expand Up @@ -1004,6 +1015,7 @@ impl<
}

/// See [`Parser::foldl`].
#[must_use]
pub struct Foldl<A, F, O, U>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<(O, U)>);

impl<A: Copy, F: Copy, O, U> Copy for Foldl<A, F, O, U> {}
Expand Down Expand Up @@ -1050,6 +1062,7 @@ impl<
}

/// See [`Parser::foldr`].
#[must_use]
pub struct Foldr<A, F, O, U>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<(O, U)>);

impl<A: Copy, F: Copy, O, U> Copy for Foldr<A, F, O, U> {}
Expand Down Expand Up @@ -1098,6 +1111,7 @@ where
}

/// See [`Parser::map_err`].
#[must_use]
#[derive(Copy, Clone)]
pub struct MapErr<A, F>(pub(crate) A, pub(crate) F);

Expand Down Expand Up @@ -1135,6 +1149,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, F: Fn(E) -> E, E: Error<I>> Parser
}

/// See [`Parser::map_err_with_span`].
#[must_use]
#[derive(Copy, Clone)]
pub struct MapErrWithSpan<A, F>(pub(crate) A, pub(crate) F);

Expand Down Expand Up @@ -1178,6 +1193,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, F: Fn(E, E::Span) -> E, E: Error<I
}

/// See [`Parser::try_map`].
#[must_use]
pub struct TryMap<A, F, O>(pub(crate) A, pub(crate) F, pub(crate) PhantomData<O>);

impl<A: Copy, F: Copy, O> Copy for TryMap<A, F, O> {}
Expand Down Expand Up @@ -1230,6 +1246,7 @@ impl<
}

/// See [`Parser::or_else`].
#[must_use]
#[derive(Copy, Clone)]
pub struct OrElse<A, F>(pub(crate) A, pub(crate) F);

Expand Down Expand Up @@ -1275,6 +1292,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, F: Fn(E) -> Result<O, E>, E: Error
}

/// See [`Parser::labelled`].
#[must_use]
#[derive(Copy, Clone)]
pub struct Label<A, L>(pub(crate) A, pub(crate) L);

Expand Down Expand Up @@ -1324,6 +1342,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, L: Into<E::Label> + Clone, E: Erro
}

/// See [`Parser::to`].
#[must_use]
pub struct To<A, O, U>(pub(crate) A, pub(crate) U, pub(crate) PhantomData<O>);

impl<A: Copy, U: Copy, O> Copy for To<A, O, U> {}
Expand Down Expand Up @@ -1359,6 +1378,7 @@ impl<I: Clone, O, A: Parser<I, O, Error = E>, U: Clone, E: Error<I>> Parser<I, U
}

/// See [`Parser::rewind`].
#[must_use]
#[derive(Copy, Clone)]
pub struct Rewind<A>(pub(crate) A);

Expand Down Expand Up @@ -1409,7 +1429,12 @@ where
}

/// See [`Parser::unwrapped`]
pub struct Unwrapped<A, U, E>(pub(crate) &'static Location<'static>, pub(crate) A, pub(crate) PhantomData<(U, E)>);
#[must_use]
pub struct Unwrapped<A, U, E>(
pub(crate) &'static Location<'static>,
pub(crate) A,
pub(crate) PhantomData<(U, E)>,
);

impl<A: Clone, U, E> Clone for Unwrapped<A, U, E> {
fn clone(&self) -> Self {
Expand All @@ -1419,7 +1444,7 @@ impl<A: Clone, U, E> Clone for Unwrapped<A, U, E> {
impl<A: Copy, U, E> Copy for Unwrapped<A, U, E> {}

impl<I: Clone, O, A: Parser<I, Result<O, U>, Error = E>, U: fmt::Debug, E: Error<I>> Parser<I, O>
for Unwrapped<A, U, E>
for Unwrapped<A, U, E>
{
type Error = E;

Expand All @@ -1434,7 +1459,17 @@ for Unwrapped<A, U, E>

(
errors,
res.map(|(out, alt)| (out.unwrap_or_else(|err| panic!("Parser defined at {} failed to unwrap. Error: {:?}", self.0, err)), alt))
res.map(|(out, alt)| {
(
out.unwrap_or_else(|err| {
panic!(
"Parser defined at {} failed to unwrap. Error: {:?}",
self.0, err
)
}),
alt,
)
}),
)
}

Expand Down
6 changes: 4 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,13 @@ pub enum SimpleReason<I, S> {

impl<I: fmt::Display, S: fmt::Display> fmt::Display for SimpleReason<I, S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
const DEFAULT_DISPLAY_UNEXPECTED: &'static str = "unexpected input";
const DEFAULT_DISPLAY_UNEXPECTED: &str = "unexpected input";

match self {
Self::Unexpected => write!(f, "{}", DEFAULT_DISPLAY_UNEXPECTED),
Self::Unclosed {span, delimiter} => write!(f, "unclosed delimiter ({}) in {}", span, delimiter),
Self::Unclosed { span, delimiter } => {
write!(f, "unclosed delimiter ({}) in {}", span, delimiter)
}
Self::Custom(string) => write!(f, "error {}", string),
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ use crate::{
recovery::*,
};

use alloc::{boxed::Box, rc::Rc, string::String, sync::Arc, vec::Vec, vec};
use alloc::{boxed::Box, rc::Rc, string::String, sync::Arc, vec, vec::Vec};
use core::{
cmp::Ordering,
// TODO: Enable when stable
//lazy::OnceCell,
fmt,
marker::PhantomData,
ops::Range,
str::FromStr,
panic::Location,
str::FromStr,
};

#[cfg(doc)]
Expand Down Expand Up @@ -1301,6 +1301,7 @@ impl<I: Clone, O, T: Parser<I, O> + ?Sized> Parser<I, O> for Arc<T> {
/// efficient cloning. This is likely to change in the future. Unlike [`Box`], [`Rc`] has no size guarantees: although
/// it is *currently* the same size as a raw pointer.
// TODO: Don't use an Rc
#[must_use]
#[repr(transparent)]
pub struct BoxedParser<'a, I, O, E: Error<I>>(Rc<dyn Parser<I, O, Error = E> + 'a>);

Expand Down
24 changes: 20 additions & 4 deletions src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::*;
use core::panic::Location;

/// See [`custom`].
#[must_use]
pub struct Custom<F, E>(F, PhantomData<E>);

impl<F: Copy, E> Copy for Custom<F, E> {}
Expand Down Expand Up @@ -60,6 +61,7 @@ pub fn custom<F, E>(f: F) -> Custom<F, E> {
}

/// See [`end`].
#[must_use]
pub struct End<E>(PhantomData<E>);

impl<E> Clone for End<E> {
Expand Down Expand Up @@ -277,6 +279,7 @@ impl<T: Clone> OrderedContainer<T> for alloc::collections::LinkedList<T> {}
impl<T: Clone> OrderedContainer<T> for alloc::collections::VecDeque<T> {}

/// See [`just`].
#[must_use]
pub struct Just<I, C: OrderedContainer<I>, E>(C, PhantomData<(I, E)>);

impl<I, C: Copy + OrderedContainer<I>, E> Copy for Just<I, C, E> {}
Expand All @@ -286,7 +289,9 @@ impl<I, C: Clone + OrderedContainer<I>, E> Clone for Just<I, C, E> {
}
}

impl<I: Clone + PartialEq, C: OrderedContainer<I> + Clone, E: Error<I>> Parser<I, C> for Just<I, C, E> {
impl<I: Clone + PartialEq, C: OrderedContainer<I> + Clone, E: Error<I>> Parser<I, C>
for Just<I, C, E>
{
type Error = E;

fn parse_inner<D: Debugger>(
Expand Down Expand Up @@ -344,6 +349,7 @@ pub fn just<I, C: OrderedContainer<I>, E: Error<I>>(inputs: C) -> Just<I, C, E>
}

/// See [`seq`].
#[must_use]
pub struct Seq<I, E>(Vec<I>, PhantomData<E>);

impl<I: Clone, E> Clone for Seq<I, E> {
Expand Down Expand Up @@ -417,6 +423,7 @@ pub fn seq<I: Clone + PartialEq, Iter: IntoIterator<Item = I>, E>(xs: Iter) -> S
}

/// See [`one_of`].
#[must_use]
pub struct OneOf<I, C, E>(C, PhantomData<(I, E)>);

impl<I, C: Clone, E> Clone for OneOf<I, C, E> {
Expand Down Expand Up @@ -478,6 +485,7 @@ pub fn one_of<I, C: Container<I>, E: Error<I>>(inputs: C) -> OneOf<I, C, E> {
}

/// See [`empty`].
#[must_use]
pub struct Empty<E>(PhantomData<E>);

impl<E> Clone for Empty<E> {
Expand Down Expand Up @@ -515,6 +523,7 @@ pub fn empty<E>() -> Empty<E> {
}

/// See [`none_of`].
#[must_use]
pub struct NoneOf<I, C, E>(C, PhantomData<(I, E)>);

impl<I, C: Clone, E> Clone for NoneOf<I, C, E> {
Expand Down Expand Up @@ -578,6 +587,7 @@ pub fn none_of<I, C: Container<I>, E: Error<I>>(inputs: C) -> NoneOf<I, C, E> {
}

/// See [`take_until`].
#[must_use]
#[derive(Copy, Clone)]
pub struct TakeUntil<A>(A);

Expand Down Expand Up @@ -673,6 +683,7 @@ pub fn take_until<A>(until: A) -> TakeUntil<A> {
}

/// See [`filter`].
#[must_use]
pub struct Filter<F, E>(F, PhantomData<E>);

impl<F: Copy, E> Copy for Filter<F, E> {}
Expand Down Expand Up @@ -733,6 +744,7 @@ pub fn filter<I, F: Fn(&I) -> bool, E>(f: F) -> Filter<F, E> {
}

/// See [`filter_map`].
#[must_use]
pub struct FilterMap<F, E>(F, PhantomData<E>);

impl<F: Copy, E> Copy for FilterMap<F, E> {}
Expand Down Expand Up @@ -822,6 +834,7 @@ pub fn any<I, E>() -> Any<I, E> {
}

/// See [`fn@todo`].
#[must_use]
pub struct Todo<I, O, E>(&'static Location<'static>, PhantomData<(I, O, E)>);

/// A parser that can be used wherever you need to implement a parser later.
Expand Down Expand Up @@ -894,6 +907,7 @@ impl<I: Clone, O, E: Error<I>> Parser<I, O> for Todo<I, O, E> {
}

/// See [`choice`].
#[must_use]
pub struct Choice<T, E>(pub(crate) T, pub(crate) PhantomData<E>);

impl<T: Copy, E> Copy for Choice<T, E> {}
Expand All @@ -903,7 +917,9 @@ impl<T: Clone, E> Clone for Choice<T, E> {
}
}

impl<I: Clone, O, E: Error<I>, A: Parser<I, O, Error = E>, const N: usize> Parser<I, O> for Choice<[A; N], E> {
impl<I: Clone, O, E: Error<I>, A: Parser<I, O, Error = E>, const N: usize> Parser<I, O>
for Choice<[A; N], E>
{
type Error = E;

fn parse_inner<D: Debugger>(
Expand All @@ -922,7 +938,7 @@ impl<I: Clone, O, E: Error<I>, A: Parser<I, O, Error = E>, const N: usize> Parse
(errors, Ok(out)) => return (errors, Ok(out)),
(_, Err(a_alt)) => {
alt = merge_alts(alt.take(), Some(a_alt));
},
}
};
}

Expand Down Expand Up @@ -967,7 +983,7 @@ impl<I: Clone, O, E: Error<I>, A: Parser<I, O, Error = E>> Parser<I, O> for Choi
(errors, Ok(out)) => return (errors, Ok(out)),
(_, Err(a_alt)) => {
alt = merge_alts(alt.take(), Some(a_alt));
},
}
};
}

Expand Down

0 comments on commit f17f70e

Please sign in to comment.