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

Use RPITIT in PathResolve #654

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Changes from all 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
30 changes: 12 additions & 18 deletions crates/containerd-shim-wasm/src/container/path.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
use std::path::{Path, PathBuf};

pub trait PathResolve {
// TODO: Once RPITIT lands in stable, change the return types from
// `-> Box<dyn Trait>` to `-> impl Trait`
// See: https://rustc-dev-guide.rust-lang.org/return-position-impl-trait-in-trait.html

// Resolve the path of a file give a set of directories as the `which` unix
// command would do with components of the `PATH` environment variable, and
// return an iterator over all candidates.
// Resulting candidates are files that exist, but no other constraing is
// imposed, in particular this function does not check for the executable bits.
// Further contraints can be added by calling filtering the returned iterator.
fn resolve_in_dirs<'a>(
fn resolve_in_dirs(
&self,
dirs: impl IntoIterator<Item = impl AsRef<Path>> + 'a,
) -> Box<dyn Iterator<Item = PathBuf> + 'a>;
fn resolve_in_path(&self) -> Box<dyn Iterator<Item = PathBuf>>;
fn resolve_in_path_or_cwd(&self) -> Box<dyn Iterator<Item = PathBuf>>;
dirs: impl IntoIterator<Item = impl AsRef<Path>>,
) -> impl Iterator<Item = PathBuf>;
fn resolve_in_path(&self) -> impl Iterator<Item = PathBuf>;
fn resolve_in_path_or_cwd(&self) -> impl Iterator<Item = PathBuf>;
}

// Gets the content of the `PATH` environment variable as an
Expand All @@ -32,10 +28,10 @@ pub fn paths() -> impl Iterator<Item = PathBuf> {
}

impl<T: AsRef<Path>> PathResolve for T {
fn resolve_in_dirs<'a>(
fn resolve_in_dirs(
&self,
dirs: impl IntoIterator<Item = impl AsRef<Path>> + 'a,
) -> Box<dyn Iterator<Item = PathBuf> + 'a> {
dirs: impl IntoIterator<Item = impl AsRef<Path>>,
) -> impl Iterator<Item = PathBuf> {
let cwd = std::env::current_dir().ok();

let has_separator = self.as_ref().components().count() > 1;
Expand All @@ -55,25 +51,23 @@ impl<T: AsRef<Path>> PathResolve for T {
};

let file = self.as_ref().to_owned();
let it = first
first
.into_iter()
.chain(second.into_iter().flatten())
.filter_map(move |p| {
// skip any paths that are not files
let path = p.join(&file).canonicalize().ok()?;
path.is_file().then_some(path)
});

Box::new(it)
})
}

// Like `find_in_dirs`, but searches on the entries of `PATH`.
fn resolve_in_path(&self) -> Box<dyn Iterator<Item = PathBuf>> {
fn resolve_in_path(&self) -> impl Iterator<Item = PathBuf> {
self.resolve_in_dirs(paths())
}

// Like `find_in_dirs`, but searches on the entries of `PATH`, and on `cwd`, in that order.
fn resolve_in_path_or_cwd(&self) -> Box<dyn Iterator<Item = PathBuf>> {
fn resolve_in_path_or_cwd(&self) -> impl Iterator<Item = PathBuf> {
self.resolve_in_dirs(paths().chain(std::env::current_dir().ok()))
}
}
Loading