diff --git a/src/async_executor.rs b/src/async_executor.rs new file mode 100644 index 00000000..47535d9b --- /dev/null +++ b/src/async_executor.rs @@ -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) {} +} diff --git a/src/lib.rs b/src/lib.rs index dd247c65..68c51cf8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/prelude.rs b/src/prelude.rs index 7d5a8953..406a8dcc 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -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