From a51872fb823013c98aacde5f072aeaa5a6ce5f38 Mon Sep 17 00:00:00 2001 From: Logan Darklock Date: Fri, 22 Jan 2021 16:33:07 -0800 Subject: [PATCH 1/2] Implement builder pattern; fixes #10 --- Cargo.toml | 1 + src/config.rs | 13 ++++++++++++- src/lib.rs | 4 +++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bbc0f52..c6255df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ readme = "README.md" glutin = "0.26.0" gl = "0.10.0" rustic_gl = "0.3.2" +derive_builder = "0.10.0-alpha" diff --git a/src/config.rs b/src/config.rs index 2aa186a..e9e11f1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,9 +20,20 @@ use glutin::dpi::LogicalSize; /// from a trait like [`Default`]. The [`config!`][config] macro makes it much less tedious to /// construct custom configs. See its documentation for more information. /// +/// Alternatively, you can choose to use the builder pattern instead: +/// +/// ``` +/// use mini_gl_fb::ConfigBuilder; +/// +/// let config = ConfigBuilder::default() +/// .invert_y(false) +/// .build().unwrap(); +/// ``` +/// /// If there's a config option you want to see or think is missing, please open an issue! #[non_exhaustive] -#[derive(Clone, PartialEq, Debug)] +#[builder(default)] +#[derive(Clone, PartialEq, Debug, Builder)] pub struct Config { /// Sets the pixel dimensions of the buffer. The buffer will automatically stretch to fill the /// whole window. By default this will be the same as the window_size. diff --git a/src/lib.rs b/src/lib.rs index 41e3857..1f1e7a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,6 +105,8 @@ #[macro_use] pub extern crate rustic_gl; +#[macro_use] +extern crate derive_builder; pub extern crate glutin; pub extern crate gl; @@ -114,7 +116,7 @@ pub mod core; pub mod breakout; pub use breakout::{GlutinBreakout, BasicInput}; -pub use config::Config; +pub use config::{Config, ConfigBuilder}; pub use core::{Internal, BufferFormat, Framebuffer}; use core::ToGlType; From 3a91ef772d3ff10e0aa2e116c03d1b9ba2692942 Mon Sep 17 00:00:00 2001 From: Logan Darklock Date: Fri, 22 Jan 2021 16:45:11 -0800 Subject: [PATCH 2/2] Building a Config cannot fail --- src/config.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index e9e11f1..603ba85 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,12 +27,13 @@ use glutin::dpi::LogicalSize; /// /// let config = ConfigBuilder::default() /// .invert_y(false) -/// .build().unwrap(); +/// .build(); /// ``` /// /// If there's a config option you want to see or think is missing, please open an issue! #[non_exhaustive] #[builder(default)] +#[builder(build_fn(skip))] #[derive(Clone, PartialEq, Debug, Builder)] pub struct Config { /// Sets the pixel dimensions of the buffer. The buffer will automatically stretch to fill the @@ -54,6 +55,28 @@ pub struct Config { pub invert_y: bool } +impl ConfigBuilder { + /// Builds a new [`Config`]. + pub fn build(&self) -> Config { + let mut config = Config::default(); + + macro_rules! fields { + ($($n:ident),+) => { + $( + if let Some($n) = &self.$n { + config.$n = $n.clone(); + } + )+ + } + } + + // I guess this is better than implementing the entire builder by hand + fields!(buffer_size, resizable, window_title, window_size, invert_y); + + config + } +} + impl Default for Config { fn default() -> Self { Config {