Crates.io | conquer-struct |
lib.rs | conquer-struct |
version | 0.1.0 |
source | src |
created_at | 2023-06-06 20:21:56.519997 |
updated_at | 2023-06-06 20:21:56.519997 |
description | provides macros to perform concurrent future resolution, while populating struct fields |
homepage | |
repository | https://github.com/NishantJoshi00/conquer-struct |
max_upload_size | |
id | 884174 |
size | 20,804 |
Macros that allow, constructing struct by providing fields as futures. These macros, automatically perform, future resolution concurrently, and construct the struct.
Consider a scenerio, where you are trying to construct a struct, and have to fetch it's fields asynchronously. The code to perform this action would looks something like this:
async fn get_contact_info(contact_id: usize) -> Contact {
Contact {
name: get_contact_name(contact_id).await,
number: get_contact_number(contact_id).await,
is_verified: true
}
}
Seems simple, now as a performance improvement, we need to call those functions concurrently. Oh, that's a refactor. It would require us to add more code, and keep track of the functions that are being executed. It's definitely possible, but consider huge structs it would definitely be difficult to keep track and you might assign outputs of different functions to difficult variables.
Introducing conquer_struct
, as the name suggest it will
conquer concurreny for you and you can avoid writing any boilerplate
code. Let's take the same example as above, look at how to make it concurrent
async fn get_contact_info(contact_id: usize) -> Contact {
conquer_future!(Contact {
name: async { get_contact_name(contact_id).await },
number: async { get_contact_number(contact_id).await },
is_verified: true
}).await
}
Done! Wasn't is simple, with minimal change our code is transformed into concurrent code.
conquer_struct
provides use with 2 macros.
conquer_future
this macro resolves the futures provided inside the struct.
the macro has a rough signature:
conquer_future!(StructName {
value1: T,
value2: async { .. } // ~ impl Future<Output = V>, considering the field
// accepts type V
}) -> impl Future<Output = StructName>
try_conquer_future
this macro resolves the futures provided inside the struct,
as well as consider the result of the future that are present inside.
try_conquer_future!(StructName {
value1: T,
value2: async { .. } // ~ impl Future<Output = Result<V, E>>, consider the field
// accepts type V
}) -> impl Future<Output = Result<StructName, E>>
Contributions as always welcome, before contributing please checkout the issues section, for any previous development that might be done. For, any improvement, features, bugfixes please create a issue first to track it.
Any code that is being added should pass, +nightly fmt
formatting checks, clippy checks, and tests.