1 unstable release
0.1.0 | Apr 30, 2023 |
---|
#1876 in Procedural macros
6KB
112 lines
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
Dependencies
~1–1.6MB
~32K SLoC