Skip to content

Commit

Permalink
Use impl Trait instead of explicit generics (#278)
Browse files Browse the repository at this point in the history
This cleans up the code a bit by simplifying some of the syntax, not not
declaring explicit generics where they don't need to be declared.

This is a breaking change because:
- The implementation of a public trait has changed
- The turbofish cannot be used on methods that use "anonymous" generics
  • Loading branch information
spenserblack authored Jan 3, 2024
1 parent 6e4517f commit 57ab2f0
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 50 deletions.
8 changes: 4 additions & 4 deletions gengo-bin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ enum Commands {
}

impl CLI {
pub fn run<Out: Write, Err: Write>(&self, mut out: Out, mut err: Err) -> Result<(), io::Error> {
pub fn run(&self, mut out: impl Write, mut err: impl Write) -> Result<(), io::Error> {
let results = self.command.analyze(self.read_limit);
let results = match results {
Ok(results) => results,
Expand Down Expand Up @@ -102,10 +102,10 @@ impl CLI {
Ok(())
}

fn run_breakdown<Out: Write, Err: Write>(
fn run_breakdown(
&self,
mut out: Out,
mut _err: Err,
mut out: impl Write,
mut _err: impl Write,
results: Analysis,
) -> Result<(), io::Error> {
let files_per_language = {
Expand Down
6 changes: 3 additions & 3 deletions gengo/src/documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ impl Documentation {
Self { globs }
}

pub fn is_documentation<P: AsRef<Path>>(&self, filepath: P, contents: &[u8]) -> bool {
pub fn is_documentation(&self, filepath: impl AsRef<Path>, contents: &[u8]) -> bool {
self.is_documentation_no_read(&filepath)
|| self.is_documentation_with_read(&filepath, contents)
}

fn is_documentation_no_read<P: AsRef<Path>>(&self, filepath: P) -> bool {
fn is_documentation_no_read(&self, filepath: impl AsRef<Path>) -> bool {
self.globs
.iter()
.any(|g| g.matches_path_with(filepath.as_ref(), GLOB_MATCH_OPTIONS))
}

fn is_documentation_with_read<P: AsRef<Path>>(&self, _filepath: P, _contents: &[u8]) -> bool {
fn is_documentation_with_read(&self, _filepath: impl AsRef<Path>, _contents: &[u8]) -> bool {
false
}

Expand Down
5 changes: 1 addition & 4 deletions gengo/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ impl Error {
Self { kind, source: None }
}

pub fn with_source<E>(kind: ErrorKind, source: E) -> Self
where
E: ErrorTrait + 'static,
{
pub fn with_source(kind: ErrorKind, source: impl ErrorTrait + 'static) -> Self {
Self {
kind,
source: Some(Box::new(source)),
Expand Down
2 changes: 1 addition & 1 deletion gengo/src/file_source/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Directory {
/// Set `buf_size` to a reasonable value for your system. You most likely
/// would want to set this to the same value as `read_limit` when building
/// a [`Gengo`](crate::Gengo) instance with a [`Builder`](crate::Builder).
pub fn new<P: AsRef<Path>>(path: P, buf_size: usize) -> Result<Self> {
pub fn new(path: impl AsRef<Path>, buf_size: usize) -> Result<Self> {
let path = path.as_ref();
if !path.is_dir() {
return Err("path is not a directory".into());
Expand Down
8 changes: 4 additions & 4 deletions gengo/src/file_source/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct Builder {
}

impl Builder {
fn new<P: AsRef<Path>>(path: P, rev: &str) -> crate::Result<Self> {
fn new(path: impl AsRef<Path>, rev: &str) -> crate::Result<Self> {
let repository = match gix::discover(path) {
Ok(r) => r,
Err(DiscoverError::Discover(err)) => {
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Git {
const GENERATED_OVERRIDE: usize = 2;
const VENDORED_OVERRIDE: usize = 3;
const DETECTABLE_OVERRIDE: usize = 4;
pub fn new<P: AsRef<Path>>(path: P, rev: &str) -> crate::Result<Self> {
pub fn new(path: impl AsRef<Path>, rev: &str) -> crate::Result<Self> {
Builder::new(path, rev)?.build()
}
}
Expand Down Expand Up @@ -127,9 +127,9 @@ impl<'repo> FileSource<'repo> for Git {
Ok((self.state.clone(), self.repository.to_thread_local()))
}

fn overrides<O: AsRef<Path>>(
fn overrides(
&self,
path: O,
path: impl AsRef<Path>,
(state, repository): &mut Self::State,
) -> Overrides {
let Ok(platform) = state
Expand Down
22 changes: 11 additions & 11 deletions gengo/src/file_source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub trait FileSource<'files>: Sync {
fn state(&'files self) -> crate::Result<Self::State>;

/// Provides combined overrides for the file.
fn overrides<O: AsRef<Path>>(&self, path: O, state: &mut Self::State) -> Overrides {
fn overrides(&self, path: impl AsRef<Path>, state: &mut Self::State) -> Overrides {
Overrides {
language: self.language_override(&path, state),
is_documentation: self.is_documentation_override(&path, state),
Expand All @@ -50,45 +50,45 @@ pub trait FileSource<'files>: Sync {
}

/// Provides an optional override for the detected language.
fn language_override<O: AsRef<Path>>(
fn language_override(
&self,
_path: O,
_path: impl AsRef<Path>,
_state: &mut Self::State,
) -> Option<String> {
None
}

/// Provides an optional override for documentation file detection.
fn is_documentation_override<O: AsRef<Path>>(
fn is_documentation_override(
&self,
_path: O,
_path: impl AsRef<Path>,
_state: &mut Self::State,
) -> Option<bool> {
None
}

/// Provides an optional override for generated file detection.
fn is_generated_override<O: AsRef<Path>>(
fn is_generated_override(
&self,
_path: O,
_path: impl AsRef<Path>,
_state: &mut Self::State,
) -> Option<bool> {
None
}

/// Provides an optional override for vendored file detection.
fn is_vendored_override<O: AsRef<Path>>(
fn is_vendored_override(
&self,
_path: O,
_path: impl AsRef<Path>,
_state: &mut Self::State,
) -> Option<bool> {
None
}

/// Provides an optional override for if the file is detectable.
fn is_detectable_override<O: AsRef<Path>>(
fn is_detectable_override(
&self,
_path: O,
_path: impl AsRef<Path>,
_state: &mut Self::State,
) -> Option<bool> {
None
Expand Down
6 changes: 3 additions & 3 deletions gengo/src/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ impl Generated {
Self { globs }
}

pub fn is_generated<P: AsRef<Path>>(&self, filepath: P, contents: &[u8]) -> bool {
pub fn is_generated(&self, filepath: impl AsRef<Path>, contents: &[u8]) -> bool {
self.is_generated_no_read(&filepath) || self.is_generated_with_read(&filepath, contents)
}

fn is_generated_no_read<P: AsRef<Path>>(&self, filepath: P) -> bool {
fn is_generated_no_read(&self, filepath: impl AsRef<Path>) -> bool {
self.globs
.iter()
.any(|g| g.matches_path_with(filepath.as_ref(), GLOB_MATCH_OPTIONS))
}

fn is_generated_with_read<P: AsRef<Path>>(&self, _filepath: P, contents: &[u8]) -> bool {
fn is_generated_with_read(&self, _filepath: impl AsRef<Path>, contents: &[u8]) -> bool {
self.likely_minified(contents)
}

Expand Down
16 changes: 8 additions & 8 deletions gengo/src/languages/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Analyzers {
}

/// Returns the analyzers that have matched by extension.
pub fn by_extension<P: AsRef<Path>>(&self, filepath: P) -> Found {
pub fn by_extension(&self, filepath: impl AsRef<Path>) -> Found {
let matches: Vec<_> = self
.iter()
.filter(|(_, a)| {
Expand All @@ -50,7 +50,7 @@ impl Analyzers {
}

/// Returns the analyzers that have matched by filename.
pub fn by_filename<P: AsRef<Path>>(&self, filepath: P) -> Found {
pub fn by_filename(&self, filepath: impl AsRef<Path>) -> Found {
let matches: Vec<_> = self
.iter()
.filter(|(_, a)| {
Expand All @@ -71,7 +71,7 @@ impl Analyzers {
}

/// Returns the analyzers that have matched by filepath pattern.
pub fn by_filepath_pattern<P: AsRef<Path>>(&self, filepath: P) -> Found {
pub fn by_filepath_pattern(&self, filepath: impl AsRef<Path>) -> Found {
let matches: Vec<_> = self
.iter()
.filter(|(_, a)| {
Expand Down Expand Up @@ -117,7 +117,7 @@ impl Analyzers {
/// It attempts to identify the file in this order:
/// 1. by shebang (`#!`)
/// 2. by filepath
pub fn simple<P: AsRef<Path>>(&self, filepath: P, contents: &[u8]) -> Found {
pub fn simple(&self, filepath: impl AsRef<Path>, contents: &[u8]) -> Found {
let matches = self.by_shebang(contents);
if !matches.is_empty() {
return matches;
Expand All @@ -139,9 +139,9 @@ impl Analyzers {
/// If none of the found heuristics match, returns the original matches.
///
/// Use `limit` to limit the number of bytes to read to match to heuristics.
pub fn with_heuristics<P: AsRef<Path>>(
pub fn with_heuristics(
&self,
filepath: P,
filepath: impl AsRef<Path>,
contents: &[u8],
limit: usize,
) -> Found {
Expand Down Expand Up @@ -213,9 +213,9 @@ impl Analyzers {
/// let language = analyzers.pick(filename, contents, limit).unwrap();
/// assert_eq!(language.name(), "Rust");
/// ```
pub fn pick<P: AsRef<Path>>(
pub fn pick(
&self,
filepath: P,
filepath: impl AsRef<Path>,
contents: &[u8],
limit: usize,
) -> Option<&Language> {
Expand Down
12 changes: 6 additions & 6 deletions gengo/src/languages/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ pub struct Extension {

impl Extension {
/// Create a new filepath matcher.
pub fn new<S: AsRef<OsStr>>(extensions: &[S]) -> Self {
pub fn new(extensions: &[impl AsRef<OsStr>]) -> Self {
let extensions = extensions.iter().map(Into::into).collect();
Self { extensions }
}

pub fn matches<P: AsRef<Path>>(&self, filename: P) -> bool {
pub fn matches(&self, filename: impl AsRef<Path>) -> bool {
self.extensions
.contains(filename.as_ref().extension().unwrap_or_default())
}
Expand All @@ -45,12 +45,12 @@ pub struct Filename {

impl Filename {
/// Create a new filepath matcher.
pub fn new<S: AsRef<OsStr>>(filenames: &[S]) -> Self {
pub fn new(filenames: &[impl AsRef<OsStr>]) -> Self {
let filenames = filenames.iter().map(Into::into).collect();
Self { filenames }
}

pub fn matches<P: AsRef<Path>>(&self, filename: P) -> bool {
pub fn matches(&self, filename: impl AsRef<Path>) -> bool {
self.filenames
.contains(filename.as_ref().file_name().unwrap_or_default())
}
Expand All @@ -72,7 +72,7 @@ impl FilepathPattern {
Self { patterns }
}

pub fn matches<P: AsRef<Path>>(&self, filename: P) -> bool {
pub fn matches(&self, filename: impl AsRef<Path>) -> bool {
self.patterns
.iter()
.any(|p| p.matches_path_with(filename.as_ref(), GLOB_MATCH_OPTIONS))
Expand All @@ -88,7 +88,7 @@ pub struct Shebang {
impl Shebang {
const MAX_SHEBANG_LENGTH: usize = 50;

pub fn new<S: Display>(interpreters: &[S]) -> Self {
pub fn new(interpreters: &[impl Display]) -> Self {
let interpreters = interpreters.iter().map(|s| s.to_string()).collect();
Self { interpreters }
}
Expand Down
6 changes: 3 additions & 3 deletions gengo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ impl<FS: for<'fs> FileSource<'fs>> Gengo<FS> {
}

/// Guesses if a file is generated.
pub fn is_generated<P: AsRef<Path>>(&self, filepath: P, contents: &[u8]) -> bool {
pub fn is_generated(&self, filepath: impl AsRef<Path>, contents: &[u8]) -> bool {
self.generated.is_generated(filepath, contents)
}

/// Guesses if a file is documentation.
pub fn is_documentation<P: AsRef<Path>>(&self, filepath: P, contents: &[u8]) -> bool {
pub fn is_documentation(&self, filepath: impl AsRef<Path>, contents: &[u8]) -> bool {
self.documentation.is_documentation(filepath, contents)
}

/// Guesses if a file is vendored.
pub fn is_vendored<P: AsRef<Path>>(&self, filepath: P, contents: &[u8]) -> bool {
pub fn is_vendored(&self, filepath: impl AsRef<Path>, contents: &[u8]) -> bool {
self.vendored.is_vendored(filepath, contents)
}
}
Expand Down
6 changes: 3 additions & 3 deletions gengo/src/vendored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ impl Vendored {
Self { globs }
}

pub fn is_vendored<P: AsRef<Path>>(&self, filepath: P, contents: &[u8]) -> bool {
pub fn is_vendored(&self, filepath: impl AsRef<Path>, contents: &[u8]) -> bool {
self.is_vendored_no_read(&filepath) || self.is_vendored_with_read(&filepath, contents)
}

fn is_vendored_no_read<P: AsRef<Path>>(&self, filepath: P) -> bool {
fn is_vendored_no_read(&self, filepath: impl AsRef<Path>) -> bool {
self.globs
.iter()
.any(|g| g.matches_path_with(filepath.as_ref(), GLOB_MATCH_OPTIONS))
}

fn is_vendored_with_read<P: AsRef<Path>>(&self, _filepath: P, _contents: &[u8]) -> bool {
fn is_vendored_with_read(&self, _filepath: impl AsRef<Path>, _contents: &[u8]) -> bool {
false
}

Expand Down

0 comments on commit 57ab2f0

Please sign in to comment.