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

Make usage of mutable references to libcnb::Env more ergonomic #850

Open
edmorley opened this issue Aug 2, 2024 · 0 comments
Open

Make usage of mutable references to libcnb::Env more ergonomic #850

edmorley opened this issue Aug 2, 2024 · 0 comments
Labels
enhancement New feature or request libcnb

Comments

@edmorley
Copy link
Member

edmorley commented Aug 2, 2024

In the Python CNB as part of switching to the new struct layer API, I've started using the pattern of having the layer function accept an env: &mut Env argument, to allow threading the libcnb::Env around the buildpack as env vars are added to it and to allow it to be used when running commands.

However, since mutable references are being used, libcnb::Env's existing IntoIterator implementation doesn't work, instead requiring either casting to an immutable reference (ie: .envs(&*env)) or else calling libcnb::Env::iter directly (ie: .envs(env.iter())).

For example:
https://github.com/heroku/buildpacks-python/blob/92c77a653ecead1d331559bc24f0f27fb72bb9d4/src/layers/python.rs#L149
https://github.com/heroku/buildpacks-python/blob/92c77a653ecead1d331559bc24f0f27fb72bb9d4/src/layers/pip_dependencies.rs#L73

We could avoid the need for this by implementing impl<'a> IntoIterator for &'a mut Env.
And for completeness probably the owned impl IntoIterator for Env too.

See also Rust's 3x IntoIterator implementations for HashMap as an example:
https://github.com/rust-lang/rust/blob/53676730146e38e4697b6204c2ee61a9fd6b7e51/library/std/src/collections/hash/map.rs#L2172-L2224

@edmorley edmorley added enhancement New feature or request libcnb labels Aug 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request libcnb
Projects
None yet
Development

No branches or pull requests

1 participant