Skip to content

Commit

Permalink
Derive Clone for LayerTypes
Browse files Browse the repository at this point in the history
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 #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.
  • Loading branch information
schneems committed Sep 8, 2023
1 parent 5de5a66 commit c088b6e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `libcnb-data`:
- `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`:
- Struct `LayerTypes` now implements clone. This allows injection of layer types into the struct implementing the `Layer` trait ([]()).

### Changed

Expand Down
2 changes: 1 addition & 1 deletion libcnb-data/src/layer_content_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl<M: PartialEq> PartialEq for LayerContentMetadata<M> {

/// Used to specify layer availability based
/// on buildpack phase.
#[derive(Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
#[derive(Debug, Default, Deserialize, Serialize, Eq, PartialEq, Clone)]
#[serde(deny_unknown_fields)]
pub struct LayerTypes {
/// Whether the layer is intended for launch.
Expand Down

0 comments on commit c088b6e

Please sign in to comment.