try_conquer_future!() { /* proc-macro */ }
Expand description
Macro function to use for resolving the futures concurrently, for fields of struct and
returning the struct itself. Additionally, try_conquer_future
resolve the Result<T, E>
which is expected to be yielded by the future.
note: the E
of the result type should match the E
on the current scope. Internally, ?
is
used to get the fields.
ยงExample
similar to conquer_future
, only difference being it uses tokio::try_join
internally to try
and resolve the futures and get the Ok(...)
of the Result<T, E>
returned by the futures
#[derive(PartialEq, Debug)]
struct Contact {
name: String,
phone_no: String,
address: String,
}
#[tokio::main]
async fn main() -> Result<(), ()> {
let contact = conquer_struct::try_conquer_future!(Contact {
name: "John Doe".to_string(),
phone_no: async { get_contact_no().await },
address: async { get_address().await }
}).await?;
assert_eq!(contact, Contact { name: "John Doe".to_string(), phone_no:
"1234567890".to_string(), address: "221B Baker Street".to_string() });
Ok(())
}
async fn get_contact_no() -> Result<String, ()> { Ok("1234567890".to_string()) }
async fn get_address() -> Result<String, ()> { Ok("221B Baker Street".to_string()) }