Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The boundless .boxed().clone() cacophony (1.0.0-alpha.8) #727

Open
alion02 opened this issue Feb 6, 2025 · 0 comments
Open

The boundless .boxed().clone() cacophony (1.0.0-alpha.8) #727

alion02 opened this issue Feb 6, 2025 · 0 comments

Comments

@alion02
Copy link

alion02 commented Feb 6, 2025

In order to keep compile times in check and rust-analyzer snappy, I've resorted to spamming .boxed() everywhere I go. This, combined with the resultant frequent requirement of .clone(), drives the signal-to-noise ratio of a parser off a cliff. If only there was a better way...

A better way?

Provide a sibling method to .boxed(), .leak(). It boxes the parser, leaks it, and returns a dyn reference. Such a reference can be used any number of times without extra noise or worries about lifetimes.

Many users can just s/boxed/leak/ and remove .clone()s, because their parser is constructed once and alive for the entire duration of their application anyway. Otherwise, they can stuff their parser in a OnceLock or something (note: I don't really know how to do this or if it's even possible).

Here's the implementation I'm using in my application:

fn leak<'b>(self) -> &'b dyn Parser<'src, I, O, E>
where
    Self: Sized + 'src + 'b,
{
    Box::leak(Box::new(self))
}

A betterer way?

Add a default feature, concrete-types. Turning it off makes the return type of almost every primitive and combinator a dyn reference. Users can disable this feature for debug builds to keep rust-analyzer and cargo check happy, only occasionally sprinkling in .boxed() or .leak() to keep release builds acceptably fast, or disable it entirely for simplicity at the cost of performance.

I'm not really sure about the possibility, practicality, or usability of this, but if it works it's probably as close as one can get to a seamless experience without tooling improvements or new language features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant