`T.into()` works but `From::from(T)` does not

Viewed 136

In this following code snippet, convert is trying to convert its input to a Box<Error>:

fn convert<T: Into<Box<Error>>>(input: T) -> Box<Error> {
    input.into() // Compiles
    // From::from(input) // Fails to compile
}

It works with input.into(), but when using From::from(T) it no longer works, it requires T to implement Error:

error[E0277]: the trait bound `T: std::error::Error` is not satisfied
 --> src/main.rs:4:3
  |
4 |   From::from(input)
  |   ^^^^^^^^^^ the trait `std::error::Error` is not implemented for `T`
  |
  = help: consider adding a `where T: std::error::Error` bound
  = note: required because of the requirements on the impl of `std::convert::From<T>` for `std::boxed::Box<std::error::Error>`
  = note: required by `std::convert::From::from`

Why do the requirements change when using From or Into? This becomes specially annoying when using the ? operator:

fn convert<T: Into<Box<Error>>>(input: T) -> Result<(), Box<Error>> {
    Err(input)? // Fails to compile
}

Is there any way to use the ? operator properly in these cases, or do I have to resort to manual match and into?

2 Answers
Related