Closed
Description
I'm trying to add an async function argument to a function and run into a situation where it get a mismatched types error although the types I'm presented in the error message are actually both identical:
= note: expected trait `for<'r> <for<'r> fn(&'r str, std::string::String) -> impl for<'r> Future<Output = u16> {request_sendgrid} as FnOnce<(&'r str, std::string::String)>>`
found trait `for<'r> <for<'r> fn(&'r str, std::string::String) -> impl for<'r> Future<Output = u16> {request_sendgrid} as FnOnce<(&'r str, std::string::String)>>`
I'm aware I'm almost 100% certainly doing sth. wrong, still the note there seems probably wrong as well?
I tried this code:
pub async fn send_message<Fut>(payload: Payload, api_key: &str, sendgrid: impl FnOnce(&str, String) -> Fut) -> Result<Response>
where
Fut: Future<Output = u16>, {
let message = payload.message.trim();
let message = if !message.is_empty() { message } else { "–" };
let data = json!({
"personalizations": [{
"to": [
{ "email": "contact@domain.tld", "name": "My Domain" }
]}
],
"from": { "email": "no-reply@domain.tld", "name": format!("{} via domain.tld", payload.name) },
"reply_to": { "email": payload.email, "name": payload.name },
"subject": "Inquiry",
"content": [{
"type": "text/plain",
"value": message
}]
});
let client = reqwest::Client::new();
let result = client
.post("https://api.sendgrid.com/v3/mail/send")
.header("Authorization", format!("Bearer {}", api_key))
.header("Content-Type", "application/json")
.body(data.to_string())
.send()
.await;
match result {
Ok(response) => match response.status() {
reqwest::StatusCode::ACCEPTED => Response::ok(""),
_ => Response::error("Bad Gateway", 502),
},
Err(_) => Response::error("Internal Server Error", 500),
}
}
async fn request_sendgrid(api_key: &str, data: String) -> u16 {
let client = reqwest::Client::new();
let result = client
.post("https://api.sendgrid.com/v3/mail/send")
.header("Authorization", format!("Bearer {}", api_key))
.header("Content-Type", "application/json")
.body(data)
.send()
.await;
match result {
Ok(response) => response.status().as_u16(),
Err(_) => 500u16,
}
}
I expected to see this happen: 2 different types in the error message
Instead, this happened: 2 equal types in the error message
Meta
rustc --version --verbose
:
rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: aarch64-apple-darwin
release: 1.64.0
LLVM version: 14.0.6
Backtrace
Compiling mainmatter-website-mailer v0.0.0 (/Users/marcoow/Code/mainmatter-website-mailer)
error[E0308]: mismatched types
--> src/lib.rs:43:32
|
43 | Ok(payload) => send_message(payload, &api_key, &request_sendgrid).await,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ one type is more general than the other
|
= note: expected trait `for<'r> <for<'r> fn(&'r str, std::string::String) -> impl for<'r> Future<Output = u16> {request_sendgrid} as FnOnce<(&'r str, std::string::String)>>`
found trait `for<'r> <for<'r> fn(&'r str, std::string::String) -> impl for<'r> Future<Output = u16> {request_sendgrid} as FnOnce<(&'r str, std::string::String)>>`
note: the lifetime requirement is introduced here
--> src/lib.rs:53:104
|
53 | pub async fn send_message<Fut>(payload: Payload, api_key: &str, sendgrid: impl FnOnce(&str, String) -> Fut) -> Result<Response>
| ^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `mainmatter-website-mailer` due to previous error