Skip to content

Instantly share code, notes, and snippets.

@ducaale
Last active June 10, 2020 19:40
Show Gist options
  • Save ducaale/20050b3767f7a4a2c6687b459d013deb to your computer and use it in GitHub Desktop.
Save ducaale/20050b3767f7a4a2c6687b459d013deb to your computer and use it in GitHub Desktop.
#[derive(Debug)]
struct User {
name: String,
email: String,
sign_in_count: u64,
active: bool,
}
impl Default for User {
fn default() -> Self {
User {
name: "ahmed".to_owned(),
email: "[email protected]".to_owned(),
sign_in_count: 0,
active: false
}
}
}
#[derive(Debug)]
enum Direction {
Horizontal,
Vertical
}
#[derive(Debug)]
enum Constraint {
Percentage(u32),
Min(u32),
Max(u32)
}
#[derive(Debug)]
struct Layout {
direction: Direction,
constraints: Vec<Constraint>,
}
impl Default for Layout {
fn default() -> Self {
Layout {
direction: Direction::Vertical,
constraints: vec![]
}
}
}
macro_rules! config {
($t: ident; $($p: ident = $v: expr),*) => {{
let mut obj: $t = Default::default();
$(obj.$p = $v;)*
obj
}}
}
fn main() {
let a = User { active: false, sign_in_count: 9, ..User::default() };
let b = config!(User; sign_in_count=5, active=true, name="ali".to_owned());
let c = config!(
Layout;
direction=Direction::Vertical,
constraints=vec![Constraint::Percentage(4), Constraint::Percentage(6)]
);
println!("{:?}", a);
println!("{:?}", b);
println!("{:?}", c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment