1
0
Fork 0

Add an AsyncExecutor trait for background tasks

This commit is contained in:
Robbert van der Helm 2022-10-21 23:52:29 +02:00
parent 558922c9a9
commit 25d20f1950
3 changed files with 25 additions and 0 deletions

23
src/async_executor.rs Normal file
View file

@ -0,0 +1,23 @@
//! Traits for running background tasks from a [`Plugin`][crate::prelude::Plugin].
//!
//! This should not be confused with the `async` language features and ecosystem in Rust.
/// Something that can run tasks of type [`Task`][Self::Task]. This can be used to defer expensive
/// computations to a background thread. Tasks can be spawned through the methods on the various
/// [`*Context`][crate::context] types.
pub trait AsyncExecutor {
/// The type of task this executor can execute. This is usually an enum type. The task type
/// should not contain any heap allocated data like [`Vec`]s and [`Box`]es.
type Task;
/// Run `task` on the current thread. This is usually called from the operating system's main
/// thread or a similar thread.
fn execute(&self, task: Self::Task);
}
/// A default implementation for plugins that don't need asynchronous background tasks.
impl AsyncExecutor for () {
type Task = ();
fn execute(&self, _task: Self::Task) {}
}

View file

@ -101,6 +101,7 @@ pub mod prelude;
pub mod formatters;
pub mod util;
pub mod async_executor;
pub mod buffer;
pub mod context;
pub mod editor;

View file

@ -10,6 +10,7 @@ pub use crate::wrapper::standalone::{nih_export_standalone, nih_export_standalon
pub use crate::formatters;
pub use crate::util;
pub use crate::async_executor::AsyncExecutor;
pub use crate::buffer::Buffer;
pub use crate::context::{GuiContext, InitContext, ParamSetter, PluginApi, ProcessContext};
// This also includes the derive macro