Skip to content

Commit

Permalink
Derive Copy and 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 19, 2023
1 parent cfcb3ff commit b2511f4
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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, Clone)]
#[derive(Debug, Default, Deserialize, Serialize, Eq, PartialEq, Copy, Clone)]
#[serde(deny_unknown_fields)]
pub struct LayerTypes {
/// Whether the layer is intended for launch.
Expand Down

0 comments on commit b2511f4

Please sign in to comment.