libcnb.rs icon indicating copy to clipboard operation
libcnb.rs copied to clipboard

Cannot `todo` the `invalid_metadata_action`

Open schneems opened this issue 1 year ago • 1 comments

Expected

I expect I can use todo!() for any function to make it compile while I work on refining my logic in another area.

Actual

This code that returns an explicit InvalidMetadataAction compiles:

pub(crate) fn install_ruby(
    context: &BuildContext<RubyBuildpack>,
    mut bullet: Print<SubBullet<Stdout>>,
    new_metadata: RubyInstallLayerMetadata,
) -> Result<(Print<SubBullet<Stdout>>, LayerEnv), libcnb::Error<RubyBuildpackError>> {
    let layer = context.cached_layer(
        layer_name!("ruby"),
        CachedLayerDefinition {
            build: true,
            launch: true,
            invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,

            // invalid_metadata_action: &|invalid_metadata| {
            //     todo!();
            // },
            restored_layer_action: &|old_metadata: &RubyInstallLayerMetadata, _| {
                if old_metadata == &new_metadata {
                    RestoredLayerAction::KeepLayer
                } else {
                    RestoredLayerAction::DeleteLayer
                }
            },
        },
    )?;

    match layer.state {
        LayerState::Restored { .. } => {
            //
        }
        LayerState::Empty { .. } => {
            //
        }
    };

    Ok((bullet, layer.read_env()?))
}

This code that replaces it with a todo!() does not:

pub(crate) fn install_ruby(
    context: &BuildContext<RubyBuildpack>,
    mut bullet: Print<SubBullet<Stdout>>,
    new_metadata: RubyInstallLayerMetadata,
) -> Result<(Print<SubBullet<Stdout>>, LayerEnv), libcnb::Error<RubyBuildpackError>> {
    let layer = context.cached_layer(
        layer_name!("ruby"),
        CachedLayerDefinition {
            build: true,
            launch: true,
            // invalid_metadata_action: &|_| InvalidMetadataAction::DeleteLayer,
            invalid_metadata_action: &|invalid_metadata| {
                todo!();
            },
            restored_layer_action: &|old_metadata: &RubyInstallLayerMetadata, _| {
                if old_metadata == &new_metadata {
                    RestoredLayerAction::KeepLayer
                } else {
                    RestoredLayerAction::DeleteLayer
                }
            },
        },
    )?;

    match layer.state {
        LayerState::Restored { .. } => {
            //
        }
        LayerState::Empty { .. } => {
            //
        }
    };

    Ok((bullet, layer.read_env()?))
}

With output:

   Compiling heroku-ruby-buildpack v0.0.0 (/Users/rschneeman/Documents/projects/work/buildpacks-ruby/buildpacks/ruby)
error: could not compile `heroku-ruby-buildpack` (bin "heroku-ruby-buildpack" test) due to 2 previous errors; 87 warnings emitted
warning: build failed, waiting for other jobs to finish...
error: could not compile `heroku-ruby-buildpack` (bin "heroku-ruby-buildpack") due to 2 previous errors; 87 warnings emitted
error[E0277]: the trait bound `(): IntoAction<libcnb::layer::InvalidMetadataAction<RubyInstallLayerMetadataV2>, _, RubyBuildpackError>` is not satisfied
   --> buildpacks/ruby/src/layers/ruby_install_layer.rs:54:25
    |
54  |     let layer = context.cached_layer(
    |                         ^^^^^^^^^^^^ the trait `IntoAction<libcnb::layer::InvalidMetadataAction<RubyInstallLayerMetadataV2>, _, RubyBuildpackError>` is not implemented for `()`
    |
    = help: the trait `IntoAction<libcnb::layer::InvalidMetadataAction<RubyInstallLayerMetadataV2>, _, RubyBuildpackError>` is implemented for `(libcnb::layer::InvalidMetadataAction<RubyInstallLayerMetadataV2>, _)`
    = help: for that trait implementation, expected `(libcnb::layer::InvalidMetadataAction<RubyInstallLayerMetadataV2>, _)`, found `()`
note: required by a bound in `BuildContext::<B>::cached_layer`
   --> /Users/rschneeman/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libcnb-0.23.0/src/build.rs:363:18
    |
356 |     pub fn cached_layer<'a, M, MA, RA, MAC, RAC>(
    |            ------------ required by a bound in this associated function
...
363 |         MA: 'a + IntoAction<InvalidMetadataAction<M>, MAC, B::Error>,
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `BuildContext::<B>::cached_layer`

error[E0277]: the trait bound `(): IntoAction<libcnb::layer::InvalidMetadataAction<ruby_install_layer::RubyInstallLayerMetadataV2>, _, RubyBuildpackError>` is not satisfied
   --> buildpacks/ruby/src/layers/ruby_install_layer.rs:54:25
    |
54  |     let layer = context.cached_layer(
    |                         ^^^^^^^^^^^^ the trait `IntoAction<libcnb::layer::InvalidMetadataAction<ruby_install_layer::RubyInstallLayerMetadataV2>, _, RubyBuildpackError>` is not implemented for `()`
    |
    = help: the trait `IntoAction<libcnb::layer::InvalidMetadataAction<ruby_install_layer::RubyInstallLayerMetadataV2>, _, RubyBuildpackError>` is implemented for `(libcnb::layer::InvalidMetadataAction<ruby_install_layer::RubyInstallLayerMetadataV2>, _)`
    = help: for that trait implementation, expected `(libcnb::layer::InvalidMetadataAction<ruby_install_layer::RubyInstallLayerMetadataV2>, _)`, found `()`
note: required by a bound in `BuildContext::<B>::cached_layer`
   --> /Users/rschneeman/.cargo/registry/src/index.crates.io-6f17d22bba15001f/libcnb-0.23.0/src/build.rs:363:18
    |
356 |     pub fn cached_layer<'a, M, MA, RA, MAC, RAC>(
    |            ------------ required by a bound in this associated function
...
363 |         MA: 'a + IntoAction<InvalidMetadataAction<M>, MAC, B::Error>,
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `BuildContext::<B>::cached_layer`

[Finished running. Exit status: 101]

schneems avatar Sep 09 '24 15:09 schneems

A workaround is to have it return InvalidMetadataAction::DeleteLayer after the todo!().

I'm not sure if we can otherwise improve the experience. Can we implement IntoAction<T, _, _> for () ?

schneems avatar Sep 09 '24 15:09 schneems