Skip to content

Commit eb07c4b

Browse files
committed
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 7a5e775 commit eb07c4b

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,22 @@ impl<T> ByteSubstring<T> where T: AsRef<[u8]> {
323323
/// A convenience type that can be used in a constant or static.
324324
pub type ByteSubstringConst = ByteSubstring<&'static [u8]>;
325325

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

329-
impl<'a> Substring<'a> {
329+
impl<'a> Substring<&'a [u8]> {
330330
pub /* const */ fn new(needle: &'a str) -> Self {
331331
Substring(ByteSubstring::new(needle.as_bytes()))
332332
}
333+
}
334+
335+
impl Substring<Vec<u8>> {
336+
pub fn new_owned(needle: String) -> Self {
337+
Substring(ByteSubstring::new(needle.into_bytes()))
338+
}
339+
}
333340

341+
impl<T> Substring<T> where T: AsRef<[u8]> {
334342
#[cfg(feature = "pattern")]
335343
fn needle_len(&self) -> usize {
336344
self.0.needle_len()
@@ -344,7 +352,7 @@ impl<'a> Substring<'a> {
344352
}
345353

346354
/// A convenience type that can be used in a constant or static.
347-
pub type SubstringConst = Substring<'static>;
355+
pub type SubstringConst = Substring<&'static str>;
348356

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

0 commit comments

Comments
 (0)