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

chore: use &Path in FileRegistry::new_from_paths() #594

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions crates/core/src/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use sha2::{Digest, Sha256};
use crate::api::FileMatchResult;
use std::{
collections::HashMap,
path::PathBuf,
path::{Path, PathBuf},
sync::mpsc::{self, Receiver, Sender},
};
use std::{fmt::Debug, str::FromStr};
Expand Down Expand Up @@ -268,7 +268,7 @@ impl Problem {
.iter()
.map(|f| PathBuf::from_str(&f.name()).unwrap())
.collect();
let borrowed_names: Vec<&PathBuf> = file_names.iter().collect();
let borrowed_names: Vec<&Path> = file_names.iter().map(PathBuf::as_path).collect();
let lazy_files: Vec<Box<dyn LoadableFile>> = files
.into_iter()
.map(|file| Box::new(file) as Box<dyn LoadableFile>)
Expand Down Expand Up @@ -538,7 +538,7 @@ impl Problem {
&self,
binding: FilePattern,
files: Vec<Box<dyn LoadableFile + 'a>>,
file_names: Vec<&PathBuf>,
file_names: Vec<&Path>,
owned_files: &FileOwners<Tree>,
context: &ExecutionContext,
) -> Result<Vec<MatchResult>> {
Expand Down
12 changes: 6 additions & 6 deletions crates/grit-pattern-matcher/src/pattern/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use grit_util::{
};
use rand::SeedableRng;
use std::ops::Range as StdRange;
use std::{collections::HashMap, path::PathBuf};
use std::{collections::HashMap, path::Path};

#[derive(Debug, Clone)]
pub struct EffectRange<'a, Q: QueryContext> {
Expand All @@ -37,7 +37,7 @@ pub struct FileRegistry<'a, Q: QueryContext> {
/// The number of versions for each file
version_count: Vec<u16>,
/// Original file paths, for lazy loading
file_paths: Vec<&'a PathBuf>,
file_paths: Vec<&'a Path>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing it to a &Path makes fewer assumptions on where the paths come from, and it doesn't appear to have any downstream effects.

/// The actual FileOwner, which has the full file available
owners: Vec<Vec<&'a FileOwner<Q::Tree<'a>>>>,
}
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<'a, Q: QueryContext> FileRegistry<'a, Q> {
self.owners[pointer.file as usize][pointer.version as usize]
}

pub fn get_file_name(&self, pointer: FilePtr) -> &'a PathBuf {
pub fn get_file_name(&self, pointer: FilePtr) -> &'a Path {
let file_index = pointer.file as usize;
let version_index = pointer.version as usize;
if let Some(owners) = self.owners.get(file_index) {
Expand All @@ -81,7 +81,7 @@ impl<'a, Q: QueryContext> FileRegistry<'a, Q> {
.expect("File path should exist for given file index.")
}

pub fn get_absolute_path(&self, pointer: FilePtr) -> GritResult<&'a PathBuf> {
pub fn get_absolute_path(&self, pointer: FilePtr) -> GritResult<&'a Path> {
let file_index = pointer.file as usize;
let version_index = pointer.version as usize;
if let Some(owners) = self.owners.get(file_index) {
Expand All @@ -95,8 +95,8 @@ impl<'a, Q: QueryContext> FileRegistry<'a, Q> {
}

/// If only the paths are available, create a FileRegistry with empty owners
/// This is *unsafe* if you do not later insert the appropriate owners before get_file_owner is called
pub fn new_from_paths(file_paths: Vec<&'a PathBuf>) -> Self {
/// This is a logic error if you do not later insert the appropriate owners before get_file_owner is called
pub fn new_from_paths(file_paths: Vec<&'a Path>) -> Self {
Self {
version_count: file_paths.iter().map(|_| 0).collect(),
owners: file_paths.iter().map(|_| Vec::new()).collect(),
Expand Down
Loading