From b2511f429f3158f135704bb410580ef9ed1f929b Mon Sep 17 00:00:00 2001 From: Richard Schneeman Date: Fri, 8 Sep 2023 09:33:53 -0500 Subject: [PATCH] Derive Copy and Clone for LayerTypes I want to be able to inject `LayerType` into a struct instead of needing to hard code it. Currently this can be done but isn't ergonomic because `LayerTypes` is not clone, but it is serializable and deserializable and all values inside are copy. That allows us to hack around the issue currently: ``` struct InjectLayerTypes { visibility: LayerTypes, // ... } Imply Layer for InjectLayerTypes { // ... fn types(&self) -> LayerTypes { LayerTypes { launch: self.visibility.launch, build: self.visibility.build, cache: self.visibility.cache, } } // Or fn types(&self) -> LayerTypes { // Hack around LayerType not implementing `Clone` let toml_string = toml::to_string(&self.visibility).expect("LayerTypes can be serialized to toml"); let layer_types: LayerTypes = toml::from_str(&toml_string).expect("Serialized data can be deserialized"); layer_types } ``` This could be used to allow building of more generic interfaces. For example, in https://github.com/heroku/libcnb.rs/pull/598 I am trying to introduce a generic struct for use in setting environment variables. One gap in this implementation is that it assumes build, launch, cache should be true. It (or another struct) could instead make no assumptions and accept LayerTypes as an injectable value. --- CHANGELOG.md | 2 +- libcnb-data/src/layer_content_metadata.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04fd5201..8a1b7589 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `ExecDProgramOutputKey`, `ProcessType`, `LayerName`, `BuildpackId` and `StackId` now implement `Ord` and `PartialOrd`. ([#658](https://github.com/heroku/libcnb.rs/pull/658)) - Add `generic::GenericMetadata` as a generic metadata type. Also makes it the default for `BuildpackDescriptor`, `SingleBuildpackDescriptor`, `MetaBuildpackDescriptor` and `LayerContentMetadata`. ([#664](https://github.com/heroku/libcnb.rs/pull/664)) - `libcnb`: - - `LayerTypes` now implements `Clone`. ([#670](https://github.com/heroku/libcnb.rs/pull/670)). + - Struct `LayerTypes` now implements `Copy` and `Clone`. This allows injection of layer types into the struct implementing the `Layer` trait ([#670](https://github.com/heroku/libcnb.rs/pull/670)). ### Changed diff --git a/libcnb-data/src/layer_content_metadata.rs b/libcnb-data/src/layer_content_metadata.rs index 47519be0..dadec05b 100644 --- a/libcnb-data/src/layer_content_metadata.rs +++ b/libcnb-data/src/layer_content_metadata.rs @@ -21,7 +21,7 @@ impl PartialEq for LayerContentMetadata { /// Used to specify layer availability based /// on buildpack phase. -#[derive(Debug, Default, Deserialize, Serialize, Eq, PartialEq, Clone)] +#[derive(Debug, Default, Deserialize, Serialize, Eq, PartialEq, Copy, Clone)] #[serde(deny_unknown_fields)] pub struct LayerTypes { /// Whether the layer is intended for launch.