How to tell Rust that two types are the same, but have different paths?

Hi Rustaceans,

I'm trying to implement the From trait for an error type:

impl From<redis::RedisError> for WebError {
   fn from(err: redis::RedisError) -> Self {
        WebError { original: Box::new(err), name: String::from("Redis error") }
    }
}

The problem is that the Redis crate doesn't expose the RedisError type directly. It has a types module and then it does pub use crate::types:: in order to make the types public. So now Rust thinks that the type I need to use is redis::types::RedisError, but I can't use it because it's in a private module and I get the following error:

error[E0277]: `?` couldn't convert the error to `errors::WebError`
  --> src/lib.rs:40:53
   |
40 |             .query_async::<String>(&mut redis).await?;
   |                                                     ^ the trait `std::convert::From<redis::types::RedisError>` is not implemented for `errors::WebError`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following implementations were found:
             <errors::WebError as std::convert::From<deadpool::managed::errors::PoolError<T>>>
             <errors::WebError as std::convert::From<redis::RedisError>>
   = note: required by `std::convert::From::from`

Is there any way to tell Rust these are the same types? Or would it have to be patched in the redis crate?

If redis::RedisError and redis::types::RedisError are really the same type, then this should just work. Re-exporting a type doesn't change what it is.

If it isn't that, though, something is causing these types to be different. Have you checked for multiple versions of the redis crate in your dependency tree? cargo tree --duplicates should show these if they're there.

2 Likes

Thanks, that was it!

I use deadpool-redis and it was indeed importing its own version of Redis and I also put another one directly in Cargo.toml

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.