pub struct Scratch(/* private fields */);Implementations§
source§impl Scratch
impl Scratch
pub const fn new() -> Self
sourcepub fn setup_for_db(
&mut self,
db: &Database
) -> Result<(), HyperscanRuntimeError>
pub fn setup_for_db( &mut self, db: &Database ) -> Result<(), HyperscanRuntimeError>
#[cfg(feature = "compiler")]
fn main() -> Result<(), hyperscan::error::HyperscanError> {
use hyperscan::{expression::*, flags::*, matchers::*, state::*};
let a_expr: Expression = "a+".parse()?;
let a_db = a_expr.compile(Flags::UTF8 | Flags::SOM_LEFTMOST, Mode::BLOCK)?;
let b_expr: Expression = "b+".parse()?;
let b_db = b_expr.compile(Flags::UTF8 | Flags::SOM_LEFTMOST, Mode::BLOCK)?;
let mut scratch = Scratch::new();
scratch.setup_for_db(&a_db)?;
scratch.setup_for_db(&b_db)?;
let s: ByteSlice = "ababaabb".into();
let mut matches: Vec<&str> = Vec::new();
scratch
.scan_sync(&a_db, s, |m| {
matches.push(unsafe { m.source.as_str() });
MatchResult::Continue
})?;
assert_eq!(&matches, &["a", "a", "a", "aa"]);
matches.clear();
scratch
.scan_sync(&b_db, s, |m| {
matches.push(unsafe { m.source.as_str() });
MatchResult::Continue
})?;
assert_eq!(&matches, &["b", "b", "b", "bb"]);
Ok(())
}pub fn as_ref_native(&self) -> Option<&NativeScratch>
pub fn as_mut_native(&mut self) -> Option<&mut NativeScratch>
sourcepub fn scan_sync<'data>(
&mut self,
db: &Database,
data: ByteSlice<'data>,
f: impl FnMut(Match<'data>) -> MatchResult
) -> Result<(), HyperscanRuntimeError>
pub fn scan_sync<'data>( &mut self, db: &Database, data: ByteSlice<'data>, f: impl FnMut(Match<'data>) -> MatchResult ) -> Result<(), HyperscanRuntimeError>
#[cfg(feature = "compiler")]
fn main() -> Result<(), hyperscan::error::HyperscanError> {
use hyperscan::{expression::*, flags::*, matchers::{*, contiguous_slice::*}, error::*};
let a_expr: Expression = "a+".parse()?;
let b_expr: Expression = "b+".parse()?;
let flags = Flags::SOM_LEFTMOST;
let expr_set = ExpressionSet::from_exprs([&a_expr, &b_expr])
.with_flags([flags, flags])
.with_ids([ExprId(1), ExprId(2)]);
let db = expr_set.compile(Mode::BLOCK)?;
let mut scratch = db.allocate_scratch()?;
let mut matches: Vec<&str> = Vec::new();
{
let mut f = |Match { source, .. }| {
matches.push(unsafe { source.as_str() });
MatchResult::Continue
};
scratch.scan_sync(&db, "aardvark".into(), &mut f)?;
scratch.scan_sync(&db, "imbibbe".into(), &mut f)?;
}
assert_eq!(&matches, &["a", "aa", "a", "b", "b", "bb"]);
let ret = scratch.scan_sync(&db, "abwuebiaubeb".into(), |_| MatchResult::CeaseMatching);
assert!(matches![ret, Err(HyperscanRuntimeError::ScanTerminated)]);
Ok(())
}sourcepub fn scan_channel<'data>(
&mut self,
db: &Database,
data: ByteSlice<'data>,
f: impl FnMut(&Match<'data>) -> MatchResult + Send + Sync
) -> impl Stream<Item = Result<Match<'data>, ScanError>>
Available on crate feature async only.
pub fn scan_channel<'data>( &mut self, db: &Database, data: ByteSlice<'data>, f: impl FnMut(&Match<'data>) -> MatchResult + Send + Sync ) -> impl Stream<Item = Result<Match<'data>, ScanError>>
async only. use hyperscan::{expression::*, flags::*, matchers::{*, contiguous_slice::*}, error::*};
use futures_util::TryStreamExt;
let a_expr: Expression = "a+".parse()?;
let b_expr: Expression = "b+".parse()?;
let flags = Flags::UTF8 | Flags::SOM_LEFTMOST;
let expr_set = ExpressionSet::from_exprs([&a_expr, &b_expr])
.with_flags([flags, flags])
.with_ids([ExprId(1), ExprId(2)]);
let db = expr_set.compile(Mode::BLOCK)?;
let mut scratch = db.allocate_scratch()?;
let matches: Vec<&str> = scratch
.scan_channel(&db, "aardvark".into(), |_| MatchResult::Continue)
.and_then(|Match { source, .. }| async move { Ok(unsafe { source.as_str() }) })
.try_collect()
.await?;
assert_eq!(&matches, &["a", "aa", "a"]);
let matches: Vec<&str> = scratch
.scan_channel(&db, "imbibbe".into(), |_| MatchResult::Continue)
.and_then(|Match { source, .. }| async move { Ok(unsafe { source.as_str() }) })
.try_collect()
.await?;
assert_eq!(&matches, &["b", "b", "bb"]);
let ret = scratch
.scan_channel(&db, "abwuebiaubeb".into(), |_| MatchResult::CeaseMatching)
.try_for_each(|_| async { Ok(()) })
.await;
assert!(matches![ret, Err(ScanError::ReturnValue(HyperscanRuntimeError::ScanTerminated))]);pub fn scan_sync_vectored<'data>( &mut self, db: &Database, data: VectoredByteSlices<'data>, f: impl FnMut(VectoredMatch<'data>) -> MatchResult ) -> Result<(), HyperscanRuntimeError>
sourcepub fn scan_channel_vectored<'data>(
&mut self,
db: &Database,
data: VectoredByteSlices<'data>,
f: impl FnMut(&VectoredMatch<'data>) -> MatchResult + Send + Sync
) -> impl Stream<Item = Result<VectoredMatch<'data>, ScanError>>
Available on crate feature async only.
pub fn scan_channel_vectored<'data>( &mut self, db: &Database, data: VectoredByteSlices<'data>, f: impl FnMut(&VectoredMatch<'data>) -> MatchResult + Send + Sync ) -> impl Stream<Item = Result<VectoredMatch<'data>, ScanError>>
async only. use hyperscan::{expression::*, flags::*, matchers::{*, vectored_slice::*}};
use futures_util::TryStreamExt;
let a_plus: Expression = "a+".parse()?;
let b_plus: Expression = "b+".parse()?;
let asdf: Expression = "asdf(.)".parse()?;
let flags = Flags::UTF8 | Flags::SOM_LEFTMOST;
let expr_set = ExpressionSet::from_exprs([&a_plus, &b_plus, &asdf])
.with_flags([flags, flags, flags])
.with_ids([ExprId(0), ExprId(3), ExprId(2)]);
let db = expr_set.compile(Mode::VECTORED)?;
let mut scratch = db.allocate_scratch()?;
let data: [ByteSlice; 4] = [
"aardvark".into(),
"imbibbe".into(),
"leas".into(),
"dfeg".into(),
];
let matches: Vec<(u32, String)> = scratch
.scan_channel_vectored(&db, data.as_ref().into(), |_| MatchResult::Continue)
.and_then(|VectoredMatch { id: ExpressionIndex(id), source, .. }| async move {
let joined = source.into_iter()
.map(|s| unsafe { s.as_str() })
.collect::<Vec<_>>()
.concat();
Ok((id, joined))
})
.try_collect()
.await?;
assert_eq!(matches, vec![
(0, "a".to_string()),
(0, "aa".to_string()),
(0, "a".to_string()),
(3, "b".to_string()),
(3, "b".to_string()),
(3, "bb".to_string()),
(0, "a".to_string()),
(2, "asdfe".to_string()),
]);pub fn scan_sync_stream<'data>( &mut self, data: ByteSlice<'data>, sink: &mut StreamSink ) -> Result<(), HyperscanRuntimeError>
pub async fn scan_stream<'data>( &mut self, data: ByteSlice<'data>, sink: &mut StreamSinkChannel ) -> Result<(), ScanError>
Available on crate feature
async only.pub fn flush_eod_sync( &mut self, sink: &mut StreamSink ) -> Result<(), HyperscanRuntimeError>
pub async fn flush_eod( &mut self, sink: &mut StreamSinkChannel ) -> Result<(), ScanError>
Available on crate feature
async only.pub fn get_size(&self) -> Result<usize, HyperscanRuntimeError>
pub fn try_clone(&self) -> Result<Self, HyperscanRuntimeError>
pub unsafe fn try_drop(&mut self) -> Result<(), HyperscanRuntimeError>
Trait Implementations§
impl Send for Scratch
impl Sync for Scratch
Auto Trait Implementations§
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more