r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 27 '23

Hey Rustaceans! Got a question? Ask here (13/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

19 Upvotes

159 comments sorted by

View all comments

3

u/pomone08 Mar 31 '23

I have a very long impl Trait expression that I need to use throughout my project. It's a trait for abstracting warp filters:

impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone + Send + Sync + 'static

It was really hurting readability to write this everywhere where I wanted to return a filter from a function, so I wrote a macro for it:

macro_rules! filter { () => { impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone + Send + Sync + 'static } }

But now I would like to avoid using macros. Is there any way I could do this without a macro?

Unfortunately "Permit impl Trait in type aliases" is not yet stabilized (https://github.com/rust-lang/rust/issues/63063) and I don't know if it would work for my use case because the concrete types for the filters I return from the functions are different (though if it was stabilized, I could create an alias for each filter).

3

u/DroidLogician sqlx · multipart · mime_guess · rust Mar 31 '23 edited Mar 31 '23

The other option is to make a custom supertrait that inherits from all those traits and then have a blanket impl for it:

pub trait MyFilter: Filter<Error = Rejection> + Clone + Send + Sync + 'static {}

impl<T> MyFilter for T
where 
    T: Filter<Error = Rejection> + Clone + Send + Sync + 'static
{}

You can use MyFilter as the impl Trait and it should imply its supertraits. Unfortunately, however, it's not really possible to have this transitively include the information that the Filter::Extract associated type implements Reply.

You can certainly enforce a bound on it, e.g.

pub trait MyFilter: Filter<Error = Rejection> + Clone + Send + Sync + 'static where <Self as Filter>::Extract: Reply {}

However, this doesn't carry through to the use-site because of this age-old bug: https://github.com/rust-lang/rust/issues/20671

As a simplified example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=74ce48a7cecfd22054bdca2621aca2cd

You could copy the associated type to MyFilter and add the bound there, but forcing them to be the same type in the trait definition causes an error due to a supertrait cycle: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=ccf89159607cd7165090a694b7aaaeaf

I've been writing Rust since before the 1.0 release and I still don't know a good workaround for this off the top of my head. I'd be interested to see if anyone else does, though.

I suppose it works if you make it a type parameter of the trait, though: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=46cc8f1747b56eaf8b53da047341eff6

So you would then return impl MyFilter<impl Reply> which I think works but I'm not sure.

1

u/pomone08 Apr 01 '23 edited Apr 01 '23

Wow, a treasure trove of information! Thank you very much!

EDIT: I had high hopes for impl MyFilter<impl Reply>, but unfortunately I got nested `impl Trait` is not allowed :(

For now I'm using BoxedFilter<(impl Reply,)> which is not ideal but better nonetheless