Skip to content

Commit 1f6bb74

Browse files
committed
feat: Allow several functions to take AsRef<str> instead of &str
1 parent 952da29 commit 1f6bb74

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/lib.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ pub struct Paths {
161161
/// }
162162
/// ```
163163
/// Paths are yielded in alphabetical order.
164-
pub fn glob(pattern: &str) -> Result<Paths, PatternError> {
164+
pub fn glob(pattern: impl AsRef<str>) -> Result<Paths, PatternError> {
165165
glob_with(pattern, MatchOptions::new())
166166
}
167167

@@ -178,7 +178,7 @@ pub fn glob(pattern: &str) -> Result<Paths, PatternError> {
178178
/// passed to this function.
179179
///
180180
/// Paths are yielded in alphabetical order.
181-
pub fn glob_with(pattern: &str, options: MatchOptions) -> Result<Paths, PatternError> {
181+
pub fn glob_with(pattern: impl AsRef<str>, options: MatchOptions) -> Result<Paths, PatternError> {
182182
#[cfg(windows)]
183183
fn check_windows_verbatim(p: &Path) -> bool {
184184
match p.components().next() {
@@ -209,6 +209,9 @@ pub fn glob_with(pattern: &str, options: MatchOptions) -> Result<Paths, PatternE
209209
p.to_path_buf()
210210
}
211211

212+
// convert pattern into &str
213+
let pattern = pattern.as_ref();
214+
212215
// make sure that the pattern is valid first, else early return with error
213216
let _ = Pattern::new(pattern)?;
214217

@@ -605,7 +608,8 @@ impl Pattern {
605608
/// This function compiles Unix shell style patterns.
606609
///
607610
/// An invalid glob pattern will yield a `PatternError`.
608-
pub fn new(pattern: &str) -> Result<Self, PatternError> {
611+
pub fn new(pattern: impl AsRef<str>) -> Result<Self, PatternError> {
612+
let pattern = pattern.as_ref();
609613
let chars = pattern.chars().collect::<Vec<_>>();
610614
let mut tokens = Vec::new();
611615
let mut is_recursive = false;
@@ -732,7 +736,8 @@ impl Pattern {
732736
/// Escape metacharacters within the given string by surrounding them in
733737
/// brackets. The resulting string will, when compiled into a `Pattern`,
734738
/// match the input string and nothing else.
735-
pub fn escape(s: &str) -> String {
739+
pub fn escape(s: impl AsRef<str>) -> String {
740+
let s = s.as_ref();
736741
let mut escaped = String::new();
737742
for c in s.chars() {
738743
match c {
@@ -763,8 +768,8 @@ impl Pattern {
763768
/// assert!(Pattern::new("k[!e]tteh").unwrap().matches("kitteh"));
764769
/// assert!(Pattern::new("d*g").unwrap().matches("doog"));
765770
/// ```
766-
pub fn matches(&self, str: &str) -> bool {
767-
self.matches_with(str, MatchOptions::new())
771+
pub fn matches(&self, str: impl AsRef<str>) -> bool {
772+
self.matches_with(str.as_ref(), MatchOptions::new())
768773
}
769774

770775
/// Return if the given `Path`, when converted to a `str`, matches this
@@ -776,8 +781,8 @@ impl Pattern {
776781

777782
/// Return if the given `str` matches this `Pattern` using the specified
778783
/// match options.
779-
pub fn matches_with(&self, str: &str, options: MatchOptions) -> bool {
780-
self.matches_from(true, str.chars(), 0, options) == Match
784+
pub fn matches_with(&self, str: impl AsRef<str>, options: MatchOptions) -> bool {
785+
self.matches_from(true, str.as_ref().chars(), 0, options) == Match
781786
}
782787

783788
/// Return if the given `Path`, when converted to a `str`, matches this

0 commit comments

Comments
 (0)