With the following code (an attempt to make an HTTP request using the reqwest
crate), the compiler says that my value SID_URI
does not implement the trait PolyfillTryInto
. What's going on here? reqwest::Url
clearly implements the private trait reqwest::into_url::PolyfillTryInto
.
#[macro_use]
extern crate lazy_static;
extern crate reqwest;
static R_EMAIL: &str = "example@example.com";
static R_PASS: &str = "password";
static API_PUBKEY: &str = "99754106633f94d350db34d548d6091a";
static API_URI: &str = "https://example.com";
static AUTH_PATH: &str = "/api/v1";
lazy_static! {
static ref SID_URI: reqwest::Url = reqwest::Url::parse(&(API_URI.to_owned() + AUTH_PATH)).unwrap();
}
fn get_sid() -> Result<reqwest::Response, reqwest::Error> {
let client = reqwest::Client::new();
let params = [("ID", R_EMAIL), ("PW", R_PASS), ("KY", API_PUBKEY)];
let q = client.post(SID_URI).form(¶ms).send()?;
Ok(q)
}
fn main() {
assert!(get_sid().is_ok());
}
error[E0277]: the trait bound `SID_URI: reqwest::into_url::PolyfillTryInto` is not satisfied
--> src/main.rs:19:20
|
19 | let q = client.post(SID_URI).form(¶ms).send()?;
| ^^^^ the trait `reqwest::into_url::PolyfillTryInto` is not implemented for `SID_URI`
|
= note: required because of the requirements on the impl of `reqwest::IntoUrl` for `SID_URI`
The compiler isn't lying to you, you are just skipping over a relevant detail of the error message. Here's a self-contained example:
Compare the two error messages:
The second error message doesn't say that
42
does not implementExampleTrait
, it says thati32
lacks the implementation. This error message shows the type that fails, not the name of the value! That means thatEXAMPLE
in the same context is referring to a type.Lazy-static works by creating one-off types that wrap your value and provide thread-safe single initialization guarantees:
This wrapper type does not implement your trait, only the wrapped type does. You will need to invoke
Deref
and then probably re-reference it to get to a&Url
, assuming that a reference to aUrl
implements your trait:Additionally, using the bare static variable would attempt to move it out of the static location (which would be a Very Bad Thing), so you always need to use it by reference.