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

Generalize Reply impl for Box<dyn Reply> #450

Merged

Conversation

mikeyhew
Copy link
Contributor

We had a case where we were boxing up replies, and needed them to impl Send + Sync to use in other code. Box<dyn Reply + Send + Sync> unfortunately doesn't implement Reply, because the impl is too specific. This generalizes the impl of Reply to include any Box<T> where T: Reply + ?Sized.

We had a case where we were boxing up replies, and needed them to impl `Send + Sync` to use in other code. `Box<dyn Reply + Send + Sync>` unfortunately doesn't implement `Reply`, because the impl is too specific. This generalizes the impl of `Reply` to include any `Box<T>` where `T: Reply + ?Sized`.
Copy link
Owner

@seanmonstar seanmonstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I suppose technically this could be breaking if someone has impl Reply for Box<MyThingy>, right? Or am I remembering wrong and that hits the orphan rule? Anywho, I can't imagine people actually doing so...

@seanmonstar seanmonstar merged commit e1d6834 into seanmonstar:master Feb 19, 2020
@mikeyhew
Copy link
Contributor Author

Looks like it is, the example below compiles without the blanket impl. Probably because Box has the #[fundamental] attribute.

struct MyThingy;

impl Reply for Box<MyThingy> {
    fn into_response(self) -> warp::reply::Response {
        todo!()
    }
}

Arc, on the other hand, isn't #[fundamental], and this impl fails to compile:

impl Reply for std::sync::Arc<MyThingy> {
    fn into_response(self) -> warp::reply::Response {
        todo!()
    }
}

with the error message:

error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
 --> server/src/api/products.rs:6:1
  |
6 | impl Reply for std::sync::Arc<MyThingy> {
  | ^^^^^^^^^^^^^^^------------------------
  | |              |
  | |              `std::sync::Arc` is not defined in the current crate
  | impl doesn't use only types from inside the current crate
  |
  = note: define and implement a trait or new type instead

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

Successfully merging this pull request may close these issues.

None yet

2 participants