I notice that there're multiple definitions of the StatusCode structs in different libraries in rust, namely:
Isn't it an issue when trying to compare status code from one library to another library?
For example, I have the following actix_web handler to call a thirdparty api and return the thirdparty api endpoint status code.
async fn get_third_party_status_code() -> impl Responder {
let client = reqwest::ClientBuilder::default().build().unwrap();
let res = client.get("http://www.example.com").send().await.unwrap();
// but this is not possible because the types are diff
let resp = HttpResponse::new(res.status());
resp
}
19 | let x = HttpResponse::new(res.status());
| ----------------- ^^^^^^^^^^^^ expected `actix_web::http::StatusCode`, found `reqwest::StatusCode`
| |
| arguments to this function are incorrect
What's the correct rust way to solve this?
Thanks for the help.
The request and actix_web ones are re-exports from http (click Source).
However it still may be an issue if you end up with different http versions in your project; for example it looks like actix_web uses http version 0.2.11 for some reason. If you need to compare or convert across versions, you could use the as_u16 method and from_u16 methods.
Thanks for the feedbacks. Correct, the http version is different on actix_web/http and reqwest.
Right, I could use from_u16 and as_u16. I was hoping there's a better or straight forward way to compare or convert between these types without going through a manual conversion.
// from reqwest -> http StatusCode
http::StatusCode::from_u16(status.as_u16()).unwrap()
If you have different versions of a crate in your deps (due to semver incompatibility), the types in each version of the crate are distinct from each other, as-if they had no crate relationship at all. So it's like any other pair of distinct types; there is no crate-version-aware exception or magic.