Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Layer interface from &self to &mut self #669

Merged
merged 4 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- `libcnb`:
- Change Layer interface from `&self` to `&mut self`. ([#669](https://github.com/heroku/libcnb.rs/pull/669))
edmorley marked this conversation as resolved.
Show resolved Hide resolved


## [0.17.0] - 2023-12-06

Expand Down
2 changes: 1 addition & 1 deletion examples/execd/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Layer for ExecDLayer {
}

fn create(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand Down
2 changes: 1 addition & 1 deletion libcnb/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<B: Buildpack + ?Sized> BuildContext<B> {
/// # }
/// #
/// fn create(
/// &self,
/// &mut self,
/// context: &BuildContext<Self::Buildpack>,
/// layer_path: &Path,
/// ) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand Down
14 changes: 8 additions & 6 deletions libcnb/src/layer/handling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use std::path::{Path, PathBuf};
pub(crate) fn handle_layer<B: Buildpack + ?Sized, L: Layer<Buildpack = B>>(
context: &BuildContext<B>,
layer_name: LayerName,
layer: L,
mut layer: L,
) -> Result<LayerData<L::Metadata>, HandleLayerErrorOrBuildpackError<B::Error>> {
match read_layer(&context.layers_dir, &layer_name) {
Ok(None) => handle_create_layer(context, &layer_name, &layer),
Ok(None) => handle_create_layer(context, &layer_name, &mut layer),
Ok(Some(layer_data)) => {
let existing_layer_strategy = layer
.existing_layer_strategy(context, &layer_data)
Expand All @@ -33,9 +33,11 @@ pub(crate) fn handle_layer<B: Buildpack + ?Sized, L: Layer<Buildpack = B>>(
match existing_layer_strategy {
ExistingLayerStrategy::Recreate => {
delete_layer(&context.layers_dir, &layer_name)?;
handle_create_layer(context, &layer_name, &layer)
handle_create_layer(context, &layer_name, &mut layer)
}
ExistingLayerStrategy::Update => {
handle_update_layer(context, &layer_data, &mut layer)
}
ExistingLayerStrategy::Update => handle_update_layer(context, &layer_data, &layer),
ExistingLayerStrategy::Keep => {
// We need to rewrite the metadata even if we just want to keep the layer around
// since cached layers are restored without their types, causing the layer to be
Expand Down Expand Up @@ -106,7 +108,7 @@ pub(crate) fn handle_layer<B: Buildpack + ?Sized, L: Layer<Buildpack = B>>(
fn handle_create_layer<B: Buildpack + ?Sized, L: Layer<Buildpack = B>>(
context: &BuildContext<B>,
layer_name: &LayerName,
layer: &L,
layer: &mut L,
) -> Result<LayerData<L::Metadata>, HandleLayerErrorOrBuildpackError<B::Error>> {
let layer_dir = context.layers_dir.join(layer_name.as_str());

Expand Down Expand Up @@ -138,7 +140,7 @@ fn handle_create_layer<B: Buildpack + ?Sized, L: Layer<Buildpack = B>>(
fn handle_update_layer<B: Buildpack + ?Sized, L: Layer<Buildpack = B>>(
context: &BuildContext<B>,
layer_data: &LayerData<L::Metadata>,
layer: &L,
layer: &mut L,
) -> Result<LayerData<L::Metadata>, HandleLayerErrorOrBuildpackError<B::Error>> {
let layer_result = layer
.update(context, layer_data)
Expand Down
8 changes: 4 additions & 4 deletions libcnb/src/layer/public_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub trait Layer {
/// # Implementation Requirements
/// Implementations **MUST NOT** write to any other location than `layer_path`.
fn create(
&self,
&mut self,
context: &BuildContext<Self::Buildpack>,
layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error>;
Expand Down Expand Up @@ -74,7 +74,7 @@ pub trait Layer {
/// # Implementation Requirements
/// Implementations **MUST NOT** modify the file-system.
fn existing_layer_strategy(
&self,
&mut self,
context: &BuildContext<Self::Buildpack>,
layer_data: &LayerData<Self::Metadata>,
) -> Result<ExistingLayerStrategy, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -100,7 +100,7 @@ pub trait Layer {
/// # Implementation Requirements
/// Implementations **MUST NOT** write to any other location than `layer_path`.
fn update(
&self,
&mut self,
context: &BuildContext<Self::Buildpack>,
layer_data: &LayerData<Self::Metadata>,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -125,7 +125,7 @@ pub trait Layer {
/// Implementations **MUST** be read-only. They **MUST NOT** modify the file-system or write
/// anything to stdout/stdout or any other stream.
fn migrate_incompatible_metadata(
&self,
&mut self,
context: &BuildContext<Self::Buildpack>,
metadata: &GenericMetadata,
) -> Result<MetadataMigration<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand Down
18 changes: 9 additions & 9 deletions libcnb/src/layer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Layer for TestLayer {
}

fn create(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -79,15 +79,15 @@ impl Layer for TestLayer {
}

fn existing_layer_strategy(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_data: &LayerData<Self::Metadata>,
) -> Result<ExistingLayerStrategy, <Self::Buildpack as Buildpack>::Error> {
Ok(self.existing_layer_strategy)
}

fn update(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
layer_data: &LayerData<Self::Metadata>,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -101,7 +101,7 @@ impl Layer for TestLayer {
}

fn migrate_incompatible_metadata(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
metadata: &GenericMetadata,
) -> Result<MetadataMigration<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand Down Expand Up @@ -749,7 +749,7 @@ fn default_layer_method_implementations() {
}

fn create(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -770,7 +770,7 @@ fn default_layer_method_implementations() {
let temp_dir = tempdir().unwrap();
let context = build_context(&temp_dir);
let layer_name = random_layer_name();
let simple_layer = SimpleLayer;
let mut simple_layer = SimpleLayer;

let simple_layer_metadata = SimpleLayerMetadata {
field_one: String::from("value one"),
Expand Down Expand Up @@ -838,7 +838,7 @@ fn layer_env_read_write() {
}

fn create(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -848,7 +848,7 @@ fn layer_env_read_write() {
}

fn existing_layer_strategy(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
layer_data: &LayerData<Self::Metadata>,
) -> Result<ExistingLayerStrategy, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -858,7 +858,7 @@ fn layer_env_read_write() {
}

fn update(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
layer_data: &LayerData<Self::Metadata>,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand Down
4 changes: 2 additions & 2 deletions test-buildpacks/readonly-layer-files/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl Layer for TestLayer {
}

fn create(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -42,7 +42,7 @@ impl Layer for TestLayer {
}

fn existing_layer_strategy(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_data: &LayerData<Self::Metadata>,
) -> Result<ExistingLayerStrategy, <Self::Buildpack as Buildpack>::Error> {
Expand Down
6 changes: 3 additions & 3 deletions test-buildpacks/sbom/src/test_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Layer for TestLayer {
}

fn create(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -43,15 +43,15 @@ impl Layer for TestLayer {
}

fn existing_layer_strategy(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_data: &LayerData<Self::Metadata>,
) -> Result<ExistingLayerStrategy, <Self::Buildpack as Buildpack>::Error> {
Ok(ExistingLayerStrategy::Update)
}

fn update(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_data: &LayerData<Self::Metadata>,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand Down
4 changes: 2 additions & 2 deletions test-buildpacks/sbom/src/test_layer_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Layer for TestLayer2 {
}

fn create(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_path: &Path,
) -> Result<LayerResult<Self::Metadata>, <Self::Buildpack as Buildpack>::Error> {
Expand All @@ -43,7 +43,7 @@ impl Layer for TestLayer2 {
}

fn existing_layer_strategy(
&self,
&mut self,
_context: &BuildContext<Self::Buildpack>,
_layer_data: &LayerData<Self::Metadata>,
) -> Result<ExistingLayerStrategy, <Self::Buildpack as Buildpack>::Error> {
Expand Down
Loading