Function set_allocator

Source
pub fn set_allocator(
    allocator: Arc<impl GlobalAlloc + 'static>,
) -> Result<(), VectorscanRuntimeError>
Available on crate feature alloc only.
Expand description

Convenience method to reset all vectorscan dynamic allocators at once.

Example: use jemalloc for all vectorscan allocations:

 #[cfg(feature = "compiler")]
 fn main() -> Result<(), vectorscan::error::VectorscanError> {
   use vectorscan::{expression::*, flags::*, matchers::*};
   use jemallocator::Jemalloc;

   // Use jemalloc for all vectorscan allocations.
   vectorscan::alloc::set_allocator(Jemalloc.into())?;

   // Everything works as normal.
   let expr: Expression = "(he)ll".parse()?;
   let db = expr.compile(Flags::default(), Mode::BLOCK)?;

   let mut scratch = db.allocate_scratch()?;

   let mut matches: Vec<&str> = Vec::new();
   scratch
     .scan_sync(&db, "hello".into(), |m| {
       matches.push(unsafe { m.source.as_str() });
       MatchResult::Continue
     })?;
   assert_eq!(&matches, &["hell"]);
   Ok(())
 }