Crates.io | make_fields |
lib.rs | make_fields |
version | 0.1.0 |
source | src |
created_at | 2023-04-30 16:48:55.344267 |
updated_at | 2023-04-30 16:48:55.344267 |
description | Tiny derive macro to work with fields inspired by lens's makeFields |
homepage | https://github.com/aloussase/make_fields |
repository | https://github.com/aloussase/make_fields |
max_upload_size | |
id | 852885 |
size | 7,413 |
Tiny derive macro inspired by makeFields
from the
lens library.
Rust's lack of higher kinded types prevents us from doing cool things like monadic tagless final, free monads or monad transformers.
I have found that using traits or typeclasses to describe generically what your functions need is sometimes enough in the abscence of these commodities.
Thus, this crate.
use make_fields::HasFields;
// This will create immutable accessors for all fields, plus a trait
// indicating that some T "Has" that field.
#[derive(HasFields)]
struct AppConfig {
db: HashMap<String, String>,
port: u16,
host: String,
}
// Now we can write our function generically like this, which makes it
// easier to write test using a mock object or something.
fn connect_to_server<T>(cfg: &T)
where
T: HasPort<Port = u16> + HasHost<Host = String>,
{
// ...
}
Please feel free to open an issue or PR if you think you can make make_fields
better!
MIT