I am trying to generify a Result
that is returned by the reqwest::blocking::get
function. It returns a Result<reqwest::blocking::Response, reqwest::Error>
but the function it is called in returns a Result<reqwest::blocking::Response, Box<dyn std::error::Error>
.
- Why does my first attempt fail to compile?
- What is the most idiomatic way to make this conversion?
This is the first attempt:
fn get_example_fails() -> Result<Response, Box<dyn Error>> {
let result = blocking::get("http://example.com");
result.map_err(|error| Box::new(error))
}
It has the following error which I do not know how to fix but feel it might be more idiomatic with some minor tweaking - but am not sure what to tweak:
error[E0308]: mismatched types
--> src/bittrex.rs:143:9
|
141 | fn get_example_fails() -> Result<Response, Box<dyn Error>> {
| -------------------------------- expected `Result<reqwes
t::blocking::Response, Box<(dyn StdError + 'static)>>` because of return type
142 | let result = blocking::get("http://example.com");
143 | result.map_err(|error| Box::new(error))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn StdError`,
found struct `reqwest::Error`
|
= note: expected enum `Result<_, Box<(dyn StdError + 'static)>>`
found enum `Result<_, Box<reqwest::Error>>`
This attempt compiles but seems verbose:
fn get_example_works() -> Result<Response, Box<dyn Error>> {
let result = blocking::get("http://example.com");
match result {
Ok(resp) => Ok(resp),
Err(error) => Err(Box::new(error)),
}
}