Nucleo

Struct Nucleo 

Source
pub struct Nucleo<T: Sync + Send + 'static> {
    pub pattern: MultiPattern,
    /* private fields */
}
Expand description

A high level matcher worker that quickly computes matches in a background threadpool.

§Example

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;

use ncp_engine::{Config, Nucleo};

static NEEDS_UPDATE: AtomicBool = AtomicBool::new(false);

// initialize a new matcher with default configuration and one column
let matcher = Nucleo::new(
    Config::DEFAULT,
    Arc::new(|| NEEDS_UPDATE.store(true, Ordering::Relaxed)),
    None,
    1
);

// get a handle to add items to the matcher
let injector = matcher.injector();

// add items to the matcher
thread::spawn(move || {
    injector.push("Hello, world!".to_string(), |s, cols| {
        cols[0] = (&**s).into();
    });
});

Fields§

§pattern: MultiPattern

The pattern matched by this matcher.

To update the match pattern, use MultiPattern::reparse. Note that the matcher worker will only become aware of the new pattern after a call to tick.

Implementations§

Source§

impl<T: Sync + Send + 'static> Nucleo<T>

Source

pub fn new( config: Config, notify: Arc<dyn Fn() + Sync + Send>, num_threads: Option<usize>, columns: u32, ) -> Self

Constructs a new nucleo worker threadpool with the provided config.

notify is called whenever new information is available and tick should be called. Note that notify is not debounced; that should be handled by the downstream crate (for example, debouncing to only redraw at most every 1/60 seconds).

If None is passed for the number of worker threads, nucleo will use one thread per hardware thread.

Nucleo can match items with multiple orthogonal properties. columns indicates how many matching columns each item (and the pattern) has. The number of columns cannot be changed after construction.

Source

pub fn active_injectors(&self) -> usize

Returns the total number of active injectors.

Source

pub fn snapshot(&self) -> &Snapshot<T>

Returns a snapshot of the current matcher state.

This method is very cheap and can be called every time a snapshot is required. The snapshot will not change unless tick is called.

Source

pub fn injector(&self) -> Injector<T>

Returns an injector that can be used for adding candidates to the matcher.

Source

pub fn restart(&mut self, clear_snapshot: bool)

Restart the the item stream. Removes all items and disconnects all previously created injectors from this instance. If clear_snapshot is true then all items and matched are removed from the Snapshot immediately. Otherwise the snapshot will keep the current matches until the matcher has run again.

§Note

The injectors will continue to function but they will not affect this instance anymore. The old items will only be dropped when all injectors were dropped.

Source

pub fn update_config(&mut self, config: Config)

Update the internal configuration.

Source

pub fn sort_results(&mut self, sort_results: bool)

Set whether the matcher should sort search results by score after matching. Defaults to true.

Source

pub fn reverse_items(&mut self, reverse_items: bool)

Set whether the matcher should reverse the order of the input. Defaults to false.

Source

pub fn tick(&mut self, timeout: u64) -> Status

Update the internal state to reflect any changes from the background worker threads.

This is the main way to interact with the matcher, and should be called regularly (for example each time a frame is rendered). To avoid excessive redraws this method will wait timeout milliseconds for the worker thread to finish. It is recommend to set the timeout to 10ms.

Trait Implementations§

Source§

impl<T: Sync + Send> Drop for Nucleo<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Nucleo<T>

§

impl<T> !RefUnwindSafe for Nucleo<T>

§

impl<T> Send for Nucleo<T>

§

impl<T> Sync for Nucleo<T>

§

impl<T> Unpin for Nucleo<T>

§

impl<T> !UnwindSafe for Nucleo<T>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

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

Source§

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.