Struct hyperscan::state::Scratch

source ·
pub struct Scratch(/* private fields */);

Implementations§

source§

impl Scratch

source

pub const fn new() -> Self

source

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(())
 }
source

pub fn as_ref_native(&self) -> Option<&NativeScratch>

source

pub fn as_mut_native(&mut self) -> Option<&mut NativeScratch>

source

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(())
 }
source

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>>

Available on crate feature 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))]);
source

pub fn scan_sync_vectored<'data>( &mut self, db: &Database, data: VectoredByteSlices<'data>, f: impl FnMut(VectoredMatch<'data>) -> MatchResult ) -> Result<(), HyperscanRuntimeError>

source

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>>

Available on crate feature 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()),
 ]);
source

pub fn scan_sync_stream<'data>( &mut self, data: ByteSlice<'data>, sink: &mut StreamSink ) -> Result<(), HyperscanRuntimeError>

source

pub async fn scan_stream<'data>( &mut self, data: ByteSlice<'data>, sink: &mut StreamSinkChannel ) -> Result<(), ScanError>

Available on crate feature async only.
source

pub fn flush_eod_sync( &mut self, sink: &mut StreamSink ) -> Result<(), HyperscanRuntimeError>

source

pub async fn flush_eod( &mut self, sink: &mut StreamSinkChannel ) -> Result<(), ScanError>

Available on crate feature async only.
source

pub fn get_size(&self) -> Result<usize, HyperscanRuntimeError>

source

pub fn try_clone(&self) -> Result<Self, HyperscanRuntimeError>

source

pub unsafe fn try_drop(&mut self) -> Result<(), HyperscanRuntimeError>

Trait Implementations§

source§

impl Clone for Scratch

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Scratch

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Scratch

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl Send for Scratch

source§

impl Sync for Scratch

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.