summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2018-09-07 12:01:26 -0400
committerAndrew Gallant <jamslam@gmail.com>2018-09-07 12:06:05 -0400
commit3dd4b77dfb677a7dec3b1806943da0dbf416db92 (patch)
tree561efd44d0b40ba34e4454b685ce1a81092b7d8b
parent3b5cdea862e3c4d6346bbef2449523e8c3790b4e (diff)
grep-searcher: add Box<...> impl for Sink
We initially did not have this impl because the first revision of the Sink trait was much more complicated. In particular, each method was parameterized over a Matcher. But not every Sink impl actually needs a Matcher, and it is just as easy to borrow a Matcher explicitly, so the added parameterization wasn't holding its own. This does permit Sink implementations to be used as trait objects. One key use case here is to reduce compile times, since there is quite a bit of code inside grep-searcher that is parameterized on Sink. Unfortunately, that code is *also* parameterized on Matcher, and the various printers in grep-printer are also parameterized on Matcher, which means Sink trait objects are necessary but no sufficient for a major reduction in compile times. Unfortunately, the path to making Matcher object safe isn't quite clear. Extension traits maybe? There's also stuff in the Serde ecosystem that might help, but the type shenanigans can get pretty gnarly.
-rw-r--r--grep-searcher/src/sink.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/grep-searcher/src/sink.rs b/grep-searcher/src/sink.rs
index 2ed1e6c1..bf2316f7 100644
--- a/grep-searcher/src/sink.rs
+++ b/grep-searcher/src/sink.rs
@@ -246,6 +246,53 @@ impl<'a, S: Sink> Sink for &'a mut S {
}
}
+impl<S: Sink + ?Sized> Sink for Box<S> {
+ type Error = S::Error;
+
+ #[inline]
+ fn matched(
+ &mut self,
+ searcher: &Searcher,
+ mat: &SinkMatch,
+ ) -> Result<bool, S::Error> {
+ (**self).matched(searcher, mat)
+ }
+
+ #[inline]
+ fn context(
+ &mut self,
+ searcher: &Searcher,
+ context: &SinkContext,
+ ) -> Result<bool, S::Error> {
+ (**self).context(searcher, context)
+ }
+
+ #[inline]
+ fn context_break(
+ &mut self,
+ searcher: &Searcher,
+ ) -> Result<bool, S::Error> {
+ (**self).context_break(searcher)
+ }
+
+ #[inline]
+ fn begin(
+ &mut self,
+ searcher: &Searcher,
+ ) -> Result<bool, S::Error> {
+ (**self).begin(searcher)
+ }
+
+ #[inline]
+ fn finish(
+ &mut self,
+ searcher: &Searcher,
+ sink_finish: &SinkFinish,
+ ) -> Result<(), S::Error> {
+ (**self).finish(searcher, sink_finish)
+ }
+}
+
/// Summary data reported at the end of a search.
///
/// This reports data such as the total number of bytes searched and the