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

Can't use downcast in libraries #9

Closed
s-panferov opened this issue Dec 27, 2014 · 5 comments
Closed

Can't use downcast in libraries #9

s-panferov opened this issue Dec 27, 2014 · 5 comments

Comments

@s-panferov
Copy link
Contributor

Hello @reem. I have a strange problem with rust-error. To demonstrate it I clone the last Iron version and modify source in-place:

#[deriving(Show, PartialEq)]
pub struct ParseError {
    location: uint,
}

impl StdError for ParseError {
    fn description(&self) -> &str { "Parse Error" }
}

/// `AfterMiddleware` are fired after a `Handler` is called inside of a Chain.
///
/// `AfterMiddleware` receive both a `Request` and a `Response` and are responsible for doing
/// any response post-processing.
///
/// `AfterMiddleware` should *not* overwrite the contents of a Response. In
/// the common case, a complete response is generated by the Chain's `Handler` and
/// `AfterMiddleware` simply do post-processing of that Response, such as
/// adding headers or logging.
pub trait AfterMiddleware: Send + Sync {
    /// Do whatever work this middleware needs to do with both a `Request` and `Response` objects.
    ///
    /// An error here is propagated by the containing Chain down to this and any later
    /// `AfterMiddleware`'s `catch` methods, which can attempt to handle the error or modify
    /// the `Response` to indicate to a client that something went wrong.
    fn after(&self, &mut Request, &mut Response) -> IronResult<()>;

    /// Try to catch an error thrown by previous `AfterMiddleware`, the `Handler`, or a previous
    /// `BeforeMiddleware`.
    ///
    /// This indicates that the `Response` is abnormal in some way, either because it was
    /// generated by a `Handler`s `catch` method or because a previous `AfterMiddleware`
    /// errored.
    fn catch(&self, _: &mut Request, _: &mut Response, err: IronError) -> IronResult<()> {
        let error = err.downcast::<ParseError>();

        Err(err)
    }
}

And when I compile it, I get:

src/middleware.rs:152:25: 152:49 error: type `Box<error::Error + 'static>` does not implement any method in scope named `downcast`
src/middleware.rs:152         let error = err.downcast::<ParseError>();
                                              ^~~~~~~~~~~~~~~~~~~~~~~~

I have the same error in rustless and I don't know how to deal with it. Can you help?

@reem
Copy link
Owner

reem commented Dec 27, 2014

This is bizarre and I'm 99% sure that this is an error in the compiler's treatment of inherent impls on trait objects. I can reproduce with similarly short examples in rust-error.

@reem
Copy link
Owner

reem commented Dec 27, 2014

Ugh, I was right: rust-lang/rust#19056

@s-panferov
Copy link
Contributor Author

@reem, thanks. I'll wait until it will be fixed. I think we can close this issue.

@reem
Copy link
Owner

reem commented Dec 27, 2014

I'll keep it open, since it makes the library basically unusable :/ I will look into an alternative fix if this isn't fixed upstream soon.

@s-panferov
Copy link
Contributor Author

It has been fixed already.

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

2 participants