Skip to content

Commit

Permalink
Derive Default instead of implementing manually (heroku#356)
Browse files Browse the repository at this point in the history
Switches any manual `Default` implementations to using `derive`,
when the manual implementation was identical to that which would
be automatically generated.
  • Loading branch information
edmorley authored Feb 28, 2022
1 parent 0ca9e3e commit edf6ab1
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 75 deletions.
27 changes: 4 additions & 23 deletions libcnb-data/src/build_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Serialize;
use std::collections::VecDeque;
use toml::value::Table;

#[derive(Serialize, Debug)]
#[derive(Serialize, Debug, Default)]
#[must_use]
pub struct BuildPlan {
#[serde(skip_serializing_if = "Vec::is_empty")]
Expand All @@ -15,20 +15,11 @@ pub struct BuildPlan {

impl BuildPlan {
pub fn new() -> Self {
Self {
provides: vec![],
requires: vec![],
or: vec![],
}
}
}

impl Default for BuildPlan {
fn default() -> Self {
Self::new()
Self::default()
}
}

#[derive(Default)]
#[must_use]
pub struct BuildPlanBuilder {
acc: VecDeque<(Vec<Provide>, Vec<Require>)>,
Expand All @@ -38,11 +29,7 @@ pub struct BuildPlanBuilder {

impl BuildPlanBuilder {
pub fn new() -> Self {
Self {
acc: VecDeque::new(),
current_provides: vec![],
current_requires: vec![],
}
Self::default()
}

pub fn provides(mut self, name: impl AsRef<str>) -> Self {
Expand Down Expand Up @@ -86,12 +73,6 @@ impl BuildPlanBuilder {
}
}

impl Default for BuildPlanBuilder {
fn default() -> Self {
Self::new()
}
}

#[derive(Serialize, Debug)]
pub struct Or {
#[serde(skip_serializing_if = "Vec::is_empty")]
Expand Down
15 changes: 2 additions & 13 deletions libcnb-data/src/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::bom;
use crate::newtypes::libcnb_newtype;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug)]
#[derive(Deserialize, Serialize, Debug, Default)]
#[serde(deny_unknown_fields)]
pub struct Launch {
#[serde(default, skip_serializing_if = "Vec::is_empty")]
Expand Down Expand Up @@ -33,12 +33,7 @@ pub struct Launch {
impl Launch {
#[must_use]
pub fn new() -> Self {
Self {
bom: bom::Bom::new(),
labels: Vec::new(),
processes: Vec::new(),
slices: Vec::new(),
}
Self::default()
}

#[must_use]
Expand All @@ -48,12 +43,6 @@ impl Launch {
}
}

impl Default for Launch {
fn default() -> Self {
Self::new()
}
}

#[derive(Deserialize, Serialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Label {
Expand Down
12 changes: 2 additions & 10 deletions libcnb/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub(crate) enum InnerBuildResult {
/// .launch(Launch::new().process(ProcessBuilder::new(process_type!("type"), "command").arg("-v").build()))
/// .build();
/// ```
#[derive(Default)]
#[must_use]
pub struct BuildResultBuilder {
launch: Option<Launch>,
Expand All @@ -151,10 +152,7 @@ pub struct BuildResultBuilder {

impl BuildResultBuilder {
pub fn new() -> Self {
Self {
launch: None,
store: None,
}
Self::default()
}

/// Builds the final [`BuildResult`].
Expand Down Expand Up @@ -185,9 +183,3 @@ impl BuildResultBuilder {
self
}
}

impl Default for BuildResultBuilder {
fn default() -> Self {
Self::new()
}
}
12 changes: 2 additions & 10 deletions libcnb/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::ffi::{OsStr, OsString};
/// String::from_utf8_lossy(&output.stdout)
/// );
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Env {
inner: HashMap<OsString, OsString>,
}
Expand All @@ -46,9 +46,7 @@ impl Env {
/// Creates an empty `Env` struct.
#[must_use]
pub fn new() -> Self {
Self {
inner: HashMap::new(),
}
Self::default()
}

/// Inserts a key-value pair into the environment, overriding the value if `key` was already
Expand Down Expand Up @@ -76,12 +74,6 @@ impl Env {
}
}

impl Default for Env {
fn default() -> Self {
Self::new()
}
}

impl From<VarsOs> for Env {
fn from(vars_os: VarsOs) -> Self {
Self {
Expand Down
23 changes: 4 additions & 19 deletions libcnb/src/layer_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ use std::path::Path;
/// assert_eq!(env.get("PATH").unwrap(), layer_dir.join("bin"));
/// assert_eq!(env.get("CPATH"), None); // None, because CPATH is only added during build
/// ```
#[derive(Eq, PartialEq, Debug, Clone)]
#[derive(Eq, PartialEq, Debug, Default, Clone)]
pub struct LayerEnv {
all: LayerEnvDelta,
build: LayerEnvDelta,
Expand Down Expand Up @@ -112,14 +112,7 @@ impl LayerEnv {
/// ```
#[must_use]
pub fn new() -> Self {
Self {
all: LayerEnvDelta::new(),
build: LayerEnvDelta::new(),
launch: LayerEnvDelta::new(),
process: HashMap::new(),
layer_paths_build: LayerEnvDelta::new(),
layer_paths_launch: LayerEnvDelta::new(),
}
Self::default()
}

/// Applies this [`LayerEnv`] to the given [`Env`] for the given [`Scope`].
Expand Down Expand Up @@ -381,12 +374,6 @@ impl LayerEnv {
}
}

impl Default for LayerEnv {
fn default() -> Self {
Self::new()
}
}

/// Environment variable modification behavior.
/// ([CNB spec: Environment Variable Modification Rules](https://github.com/buildpacks/spec/blob/main/buildpack.md#environment-variable-modification-rules))
#[derive(Eq, PartialEq, Debug, Clone)]
Expand Down Expand Up @@ -431,16 +418,14 @@ pub enum Scope {
Process(String),
}

#[derive(Eq, PartialEq, Debug, Clone)]
#[derive(Eq, PartialEq, Debug, Default, Clone)]
struct LayerEnvDelta {
entries: BTreeMap<(ModificationBehavior, OsString), OsString>,
}

impl LayerEnvDelta {
fn new() -> Self {
Self {
entries: BTreeMap::new(),
}
Self::default()
}

fn apply(&self, env: &Env) -> Env {
Expand Down

0 comments on commit edf6ab1

Please sign in to comment.