make_fields

Crates.iomake_fields
lib.rsmake_fields
version0.1.0
sourcesrc
created_at2023-04-30 16:48:55.344267
updated_at2023-04-30 16:48:55.344267
descriptionTiny derive macro to work with fields inspired by lens's makeFields
homepagehttps://github.com/aloussase/make_fields
repositoryhttps://github.com/aloussase/make_fields
max_upload_size
id852885
size7,413
Alexander Goussas (aloussase)

documentation

README

make_fields

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.

Example

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>,
{
    // ...
}

Contributing

Please feel free to open an issue or PR if you think you can make make_fields better!

License

MIT

Commit count: 0

cargo fmt