Un-macro succeed

This implementation requires that the type of the value impls `Clone`,
which means the function version of `succeed` cannot be used to succeed
with values of non-`Clone` types.

If this becomes an issue, we could either bring back the macro version,
or create a new `succeed_with` function that takes a `Fn() -> T`
argument. `succeed_with` could then be used to succeed with values that
aren't `Clone` by creating the value on-demand each time the parser is
run.
This commit is contained in:
Jackson Wambolt 2024-04-15 20:29:02 -05:00
parent 5c0b2a0938
commit f6c977fb96
No known key found for this signature in database
GPG key ID: 76F29A42FEE8811C
3 changed files with 11 additions and 13 deletions

View file

@ -1521,7 +1521,7 @@ macro_rules! collection_trailing_sep_e {
/// # use bumpalo::Bump;
/// # let arena = Bump::new();
/// # fn foo<'a>(arena: &'a Bump) {
/// let parser = succeed!("different");
/// let parser = succeed("different");
///
/// let (progress, output, state) = Parser::<&'a str,()>::parse(&parser, &arena, State::new("hello, world".as_bytes()), 0).unwrap();
/// assert_eq!(progress, Progress::NoProgress);
@ -1530,13 +1530,10 @@ macro_rules! collection_trailing_sep_e {
/// # }
/// # foo(&arena);
/// ```
#[macro_export]
macro_rules! succeed {
($value:expr) => {
move |_arena: &'a bumpalo::Bump, state: $crate::state::State<'a>, _min_indent: u32| {
Ok((NoProgress, $value, state))
}
};
pub fn succeed<'a, T: Clone, E: 'a>(value: T) -> impl Parser<'a, T, E> {
move |_arena: &'a bumpalo::Bump, state: crate::state::State<'a>, _min_indent: u32| {
Ok((NoProgress, value.clone(), state))
}
}
/// Creates a parser that always fails.