Skip to content

Instantly share code, notes, and snippets.

@dimitrilw
Last active September 21, 2023 18:43
Show Gist options
  • Save dimitrilw/1fe398113c931eba8e1b084205922976 to your computer and use it in GitHub Desktop.
Save dimitrilw/1fe398113c931eba8e1b084205922976 to your computer and use it in GitHub Desktop.
playing with Struct construction patterns via derive-Default & the typed_builder crate
/* Cargo.toml
[package]
name = "builder-rs"
version = "0.1.0"
edition = "2021"
[dependencies]
typed-builder = "0.16.1"
*/
// I usually don't use this, but it is handy for this quick example.
#![allow(dead_code)]
use typed_builder::TypedBuilder;
#[derive(Debug, Default)]
struct OnlyDefault {
b: bool,
i: i32,
s: String,
}
#[derive(Debug, TypedBuilder)]
struct OnlyBuilder {
b: bool,
#[builder(default=99)]
i: i32,
#[builder(default="hello from OnlyBuilder's default string".to_string())]
s: String,
}
#[derive(Debug, Default, TypedBuilder)]
struct DefaultAndBuilder {
b: bool,
#[builder(default=99)]
i: i32,
#[builder(default="hello from DefaultAndBuilder's default string".to_string())]
s: String,
}
fn main() {
println!("==================== OnlyDefault ====================");
let od_default = OnlyDefault::default();
println!("od_default: {:?}", od_default);
let od_set_str = OnlyDefault {
s: "hello from od_set_str".to_string(),
..OnlyDefault::default()
};
println!("od_set_str: {:?}", od_set_str);
println!("==================== OnlyBuilder ====================");
let ob_set_b = OnlyBuilder::builder().b(true).build();
println!("ob_set_b: {:?}", ob_set_b);
let ob_set_all = OnlyBuilder::builder()
.b(true)
.i(42)
.s("hello from ob_set_all".to_string())
.build();
println!("ob_set_all: {:?}", ob_set_all);
println!("==================== DefaultAndBuilder ====================");
let dab_struct_default = DefaultAndBuilder::default();
println!("dab_struct_default: {:?}", dab_struct_default);
let dab_struct_set = DefaultAndBuilder {
i: 42,
s: "hello from dab_set_struct".to_string(),
..DefaultAndBuilder::default()
};
println!("dab_struct_set: {:?}", dab_struct_set);
let dab_set_builder = DefaultAndBuilder::builder()
.b(true)
.i(42)
.s("hello from dab_set_builder".to_string())
.build();
println!("dab_set_builder: {:?}", dab_set_builder);
let dab_set_struct_cond = DefaultAndBuilder {
// note: not aware of a method for saying "set this if true, else default"
s: (if true {
"hello from dab_set_struct_cond"
} else {
"hello from dab_set_struct_cond else"
}).to_string(),
..DefaultAndBuilder::default()
};
println!("dab_set_struct_cond: {:?}", dab_set_struct_cond);
// note: this is a mutable struct, which adds mutation risk
let mut dab_mutable = DefaultAndBuilder::builder()
.b(true)
.i(42)
.s("hello from dab_mutable".to_string())
.build();
println!("dab_mutable, step 1: {:?}", dab_mutable);
if vec![1, 2, 3].len() > 2 {
dab_mutable.s = "vec.len > 2".to_string();
} else if true {
dab_mutable.s = "else if true".to_string();
}
println!("dab_mutable, step 2: {:?}", dab_mutable);
}
/* output:
==================== OnlyDefault ====================
od_default: OnlyDefault { b: false, i: 0, s: "" }
od_set_str: OnlyDefault { b: false, i: 0, s: "hello from od_set_str" }
==================== OnlyBuilder ====================
ob_set_b: OnlyBuilder { b: true, i: 99, s: "hello from OnlyBuilder's default string" }
ob_set_all: OnlyBuilder { b: true, i: 42, s: "hello from ob_set_all" }
==================== DefaultAndBuilder ====================
dab_struct_default: DefaultAndBuilder { b: false, i: 0, s: "" }
dab_struct_set: DefaultAndBuilder { b: false, i: 42, s: "hello from dab_set_struct" }
dab_set_builder: DefaultAndBuilder { b: true, i: 42, s: "hello from dab_set_builder" }
dab_set_struct_cond: DefaultAndBuilder { b: false, i: 0, s: "hello from dab_set_struct_cond" }
dab_mutable, step 1: DefaultAndBuilder { b: true, i: 42, s: "hello from dab_chain" }
dab_mutable, step 2: DefaultAndBuilder { b: true, i: 42, s: "vec.len > 2" }
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment