Skip to content

Commit 420ea9d

Browse files
tsnoamshepmaster
authored andcommitted
Substring now takes the needle as generic type T instead of &str
This allows to either own (consume) the needle or keep a reference to it.
1 parent 22ad6b3 commit 420ea9d

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/lib.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,25 @@ where
325325
/// A convenience type that can be used in a constant or static.
326326
pub type ByteSubstringConst = ByteSubstring<&'static [u8]>;
327327

328-
/// Searches a string for the first occurence of the substring.
329-
pub struct Substring<'a>(ByteSubstring<&'a [u8]>);
328+
/// Searches a string for the first occurrence of the substring.
329+
pub struct Substring<T>(ByteSubstring<T>);
330330

331-
impl<'a> Substring<'a> {
331+
impl<'a> Substring<&'a [u8]> {
332332
pub /* const */ fn new(needle: &'a str) -> Self {
333333
Substring(ByteSubstring::new(needle.as_bytes()))
334334
}
335+
}
336+
337+
impl Substring<Vec<u8>> {
338+
pub fn new_owned(needle: String) -> Self {
339+
Substring(ByteSubstring::new(needle.into_bytes()))
340+
}
341+
}
335342

343+
impl<T> Substring<T>
344+
where
345+
T: AsRef<[u8]>,
346+
{
336347
#[cfg(feature = "pattern")]
337348
fn needle_len(&self) -> usize {
338349
self.0.needle_len()
@@ -346,7 +357,7 @@ impl<'a> Substring<'a> {
346357
}
347358

348359
/// A convenience type that can be used in a constant or static.
349-
pub type SubstringConst = Substring<'static>;
360+
pub type SubstringConst = Substring<&'static str>;
350361

351362
#[cfg(all(test, feature = "benchmarks"))]
352363
mod bench {

0 commit comments

Comments
 (0)