Skip to content

Commit 343e8aa

Browse files
committed
Change to using a reference to a Haystack
1 parent 9ebcbc3 commit 343e8aa

12 files changed

Lines changed: 133 additions & 123 deletions

File tree

examples/ad_regex/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ad_regex/src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,31 +181,31 @@ mod impl_structex {
181181
}
182182
}
183183

184-
impl Haystack<Regex> for &str {
184+
impl Haystack<Regex> for str {
185185
fn is_match_between(&self, re: &Regex, from: usize, to: usize) -> bool {
186-
re.matches_between(self, from, to)
186+
re.matches_between(&self, from, to)
187187
}
188188

189189
fn captures_between(&self, re: &Regex, from: usize, to: usize) -> Option<RawCaptures> {
190-
let m = re.find_between(self, from, to)?;
190+
let m = re.find_between(&self, from, to)?;
191191

192192
Some(RawCaptures::new(m.iter_locs()))
193193
}
194194
}
195195

196-
impl Haystack<Regex> for &GapBuffer {
196+
impl Haystack<Regex> for GapBuffer {
197197
fn is_match_between(&self, re: &Regex, from: usize, to: usize) -> bool {
198-
re.matches_between(*self, from, to)
198+
re.matches_between(self, from, to)
199199
}
200200

201201
fn captures_between(&self, re: &Regex, from: usize, to: usize) -> Option<RawCaptures> {
202-
let m = re.find_between(*self, from, to)?;
202+
let m = re.find_between(self, from, to)?;
203203

204204
Some(RawCaptures::new(m.iter_locs()))
205205
}
206206
}
207207

208-
impl Sliceable for &GapBuffer {
208+
impl Sliceable for GapBuffer {
209209
type Slice<'h>
210210
= gap_buffer::Slice<'h>
211211
where
@@ -224,7 +224,7 @@ mod impl_structex {
224224
}
225225
}
226226

227-
impl Writable for &GapBuffer {
227+
impl Writable for GapBuffer {
228228
fn write_to<W>(&self, w: &mut W) -> std::io::Result<usize>
229229
where
230230
W: std::io::Write,

examples/ad_regex/src/stream.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,22 +226,22 @@ mod impl_structex {
226226
use std::{io::Read, ops::Range};
227227
use structex::re::{Haystack, RawCaptures, Sliceable, Writable};
228228

229-
impl<R> Haystack<Regex> for &CachingStream<R>
229+
impl<R> Haystack<Regex> for CachingStream<R>
230230
where
231231
R: Read,
232232
{
233233
fn is_match_between(&self, re: &Regex, from: usize, to: usize) -> bool {
234-
re.matches_between(*self, from, to)
234+
re.matches_between(self, from, to)
235235
}
236236

237237
fn captures_between(&self, re: &Regex, from: usize, to: usize) -> Option<RawCaptures> {
238-
let m = re.find_between(*self, from, to)?;
238+
let m = re.find_between(self, from, to)?;
239239

240240
Some(RawCaptures::new(m.iter_locs()))
241241
}
242242
}
243243

244-
impl<R> Sliceable for &CachingStream<R>
244+
impl<R> Sliceable for CachingStream<R>
245245
where
246246
R: Read,
247247
{
@@ -272,7 +272,7 @@ mod impl_structex {
272272
}
273273
}
274274

275-
impl<R> Writable for &CachingStream<R>
275+
impl<R> Writable for CachingStream<R>
276276
where
277277
R: Read,
278278
{

examples/sgrep/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ fn main() -> Result<()> {
5858
Ok(())
5959
}
6060

61-
fn run_for<H>(
61+
fn run_for<'h, H>(
6262
se: &Structex<Regex>,
63-
h: H,
63+
h: &'h H,
6464
templates: &HashMap<usize, Template>,
65-
after_match: impl Fn(H, &TaggedCaptures<H>),
65+
after_match: impl Fn(&'h H, &TaggedCaptures<'h, H>),
6666
) -> Result<()>
6767
where
68-
H: Haystack<Regex>,
68+
H: Haystack<Regex> + ?Sized,
6969
{
7070
for caps in se.iter_tagged_captures(h) {
7171
let id = caps.id().unwrap();

examples/ssed/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ fn main() -> Result<()> {
6464
Ok(())
6565
}
6666

67-
fn run_for<H>(
67+
fn run_for<'h, H>(
6868
se: &Structex<Regex>,
69-
h: H,
69+
h: &'h H,
7070
templates: &HashMap<usize, Template>,
71-
after_match: impl Fn(H, &TaggedCaptures<H>),
71+
after_match: impl Fn(&'h H, &TaggedCaptures<'h, H>),
7272
) -> Result<()>
7373
where
74-
H: Haystack<Regex>,
74+
H: Haystack<Regex> + ?Sized,
7575
{
7676
let mut pos = 0;
7777

src/re.rs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ where
5555
}
5656

5757
/// Something that supports extracting a contiguous sub-section between two bytes offsets.
58-
pub trait Sliceable: Writable + Copy {
58+
pub trait Sliceable: Writable {
5959
/// The output of the [slice][Sliceable::slice] method.
6060
type Slice<'h>: Writable
6161
where
@@ -75,43 +75,50 @@ pub trait Sliceable: Writable + Copy {
7575
fn max_len(&self) -> usize;
7676
}
7777

78-
/// Something that can be written to to a given [io::Write].
79-
pub trait Writable {
80-
/// Writes `self` to the given [io::Write], returning the number of bytes written.
81-
fn write_to<W>(&self, w: &mut W) -> io::Result<usize>
82-
where
83-
W: io::Write;
84-
}
85-
86-
impl Sliceable for &str {
78+
impl<T> Sliceable for T
79+
where
80+
T: AsRef<str> + ?Sized,
81+
{
8782
type Slice<'h>
8883
= &'h str
8984
where
9085
Self: 'h;
9186

9287
fn char_at(&self, byte_offset: usize) -> Option<char> {
93-
if byte_offset >= self.len() {
88+
if byte_offset >= self.as_ref().len() {
9489
return None;
9590
}
9691

97-
self[byte_offset..].chars().next()
92+
self.as_ref()[byte_offset..].chars().next()
9893
}
9994

10095
fn slice(&self, range: Range<usize>) -> &str {
101-
&self[range]
96+
&self.as_ref()[range]
10297
}
10398

10499
fn max_len(&self) -> usize {
105-
self.len()
100+
self.as_ref().len()
106101
}
107102
}
108103

109-
impl Writable for &str {
104+
/// Something that can be written to to a given [io::Write].
105+
pub trait Writable {
106+
/// Writes `self` to the given [io::Write], returning the number of bytes written.
107+
fn write_to<W>(&self, w: &mut W) -> io::Result<usize>
108+
where
109+
W: io::Write;
110+
}
111+
112+
impl<T> Writable for T
113+
where
114+
T: AsRef<str> + ?Sized,
115+
{
110116
fn write_to<W>(&self, w: &mut W) -> io::Result<usize>
111117
where
112118
W: io::Write,
113119
{
114-
w.write_all(self.as_bytes()).map(|_| self.len())
120+
w.write_all(self.as_ref().as_bytes())
121+
.map(|_| self.as_ref().len())
115122
}
116123
}
117124

@@ -154,19 +161,19 @@ impl RawCaptures {
154161
/// Represents the capture group positions for a single [RegexEngine] match in terms of byte
155162
/// offsets into the original haystack that the match was run against.
156163
#[derive(Debug, PartialEq, Eq)]
157-
pub struct Captures<H>
164+
pub struct Captures<'h, H>
158165
where
159-
H: Sliceable,
166+
H: Sliceable + ?Sized,
160167
{
161-
haystack: H,
168+
haystack: &'h H,
162169
caps: Vec<Option<(usize, usize)>>,
163170
}
164171

165-
impl<H> Captures<H>
172+
impl<'h, H> Captures<'h, H>
166173
where
167-
H: Sliceable,
174+
H: Sliceable + ?Sized,
168175
{
169-
pub(crate) fn new(haystack: H, caps: Vec<Option<(usize, usize)>>) -> Self {
176+
pub(crate) fn new(haystack: &'h H, caps: Vec<Option<(usize, usize)>>) -> Self {
170177
Self { haystack, caps }
171178
}
172179

@@ -241,7 +248,7 @@ mod impl_for_regex {
241248
}
242249
}
243250

244-
impl Haystack<Regex> for &str {
251+
impl Haystack<Regex> for str {
245252
fn is_match_between(&self, re: &Regex, from: usize, to: usize) -> bool {
246253
re.is_match(&self[from..to])
247254
}

src/se/extract.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,30 +43,30 @@ impl Extract {
4343
}
4444
}
4545

46-
pub(super) struct Iter<'s, R, H>
46+
pub(super) struct Iter<'s, 'h, R, H>
4747
where
4848
R: RegexEngine,
49-
H: Haystack<R>,
49+
H: Haystack<R> + ?Sized,
5050
{
51-
haystack: H,
51+
haystack: &'h H,
5252
ext: &'s Extract,
5353
inner: Arc<Inner<R>>,
5454
/// The original parent dot we are extracting from
5555
parent: Dot,
5656
/// The child branch we are currently iterating over
57-
child: Option<Box<MatchesInner<'s, R, H>>>,
57+
child: Option<Box<MatchesInner<'s, 'h, R, H>>>,
5858
/// The current match
5959
held: Option<RawCaptures>,
6060
/// The current byte offset we are up to
6161
pos: usize,
6262
}
6363

64-
impl<'s, R, H> Iter<'s, R, H>
64+
impl<'s, 'h, R, H> Iter<'s, 'h, R, H>
6565
where
6666
R: RegexEngine,
67-
H: Haystack<R>,
67+
H: Haystack<R> + ?Sized,
6868
{
69-
pub fn new(haystack: H, parent: Dot, ext: &'s Extract, inner: Arc<Inner<R>>) -> Self {
69+
pub fn new(haystack: &'h H, parent: Dot, ext: &'s Extract, inner: Arc<Inner<R>>) -> Self {
7070
let pos = parent.from();
7171

7272
Self {
@@ -98,12 +98,12 @@ where
9898
}
9999
}
100100

101-
impl<'s, R, H> Iterator for Iter<'s, R, H>
101+
impl<'s, 'h, R, H> Iterator for Iter<'s, 'h, R, H>
102102
where
103103
R: RegexEngine,
104-
H: Haystack<R>,
104+
H: Haystack<R> + ?Sized,
105105
{
106-
type Item = TaggedCaptures<H>;
106+
type Item = TaggedCaptures<'h, H>;
107107

108108
fn next(&mut self) -> Option<Self::Item> {
109109
loop {

src/se/guard.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ impl Guard {
4242
Some(self)
4343
}
4444

45-
pub(super) fn apply<'s, R, H>(
45+
pub(super) fn apply<'s, 'h, R, H>(
4646
&'s self,
47-
haystack: H,
47+
haystack: &'h H,
4848
dot: Dot,
4949
inner: Arc<Inner<R>>,
50-
) -> Option<MatchesInner<'s, R, H>>
50+
) -> Option<MatchesInner<'s, 'h, R, H>>
5151
where
5252
R: RegexEngine,
53-
H: Haystack<R>,
53+
H: Haystack<R> + ?Sized,
5454
{
5555
let (from, to) = dot.loc();
5656
let is_match = haystack.is_match_between(&inner.re[self.re], from, to);

0 commit comments

Comments
 (0)