Skip to main content

Plugin

Trait Plugin 

Source
pub trait Plugin:
    Send
    + Sync
    + 'static {
    type Ctx: Send + 'static;

    const ENTRIES: &'static [&'static str];
    const ASYNC_ENTRIES: &'static [&'static str] = _;

    // Required methods
    fn new() -> Self
       where Self: Sized;
    fn new_ctx(&self) -> Self::Ctx;
    fn on_call(&self, session: &mut Session<'_>, ctx: &mut Self::Ctx) -> Reply;

    // Provided methods
    fn on_async_call(
        &self,
        session: AsyncSession,
        ctx: Self::Ctx,
    ) -> impl Future<Output = Reply> + Send { ... }
    fn spawn_async(&self, task: AsyncTask) { ... }
    fn on_stream_data(&self, _sid: u64, _data: &[u8]) -> NrStatus { ... }
    fn on_stream_close(&self, _sid: u64) -> NrStatus { ... }
    fn on_shutdown(&self) { ... }
}
Expand description

A nylon-ring plugin. Implement this and export it with export_plugin!; see the module docs for the state model.

Required Associated Constants§

Source

const ENTRIES: &'static [&'static str]

Entry names dispatched to Plugin::on_call. An entry’s dispatch id is its index here, so appending keeps existing ids stable.

Provided Associated Constants§

Source

const ASYNC_ENTRIES: &'static [&'static str] = _

Entry names dispatched to Plugin::on_async_call. Their dispatch ids continue after Plugin::ENTRIES (a name in both lists dispatches synchronously).

Required Associated Types§

Source

type Ctx: Send + 'static

Per-call state, created fresh for every incoming call and dropped when the call ends. Use () when no per-call state is needed. ('static so async handlers can carry it across await points.)

Required Methods§

Source

fn new() -> Self
where Self: Sized,

Constructs the plugin instance; runs once when the host initializes the library.

Source

fn new_ctx(&self) -> Self::Ctx

Creates the per-call context.

Source

fn on_call(&self, session: &mut Session<'_>, ctx: &mut Self::Ctx) -> Reply

Handles one call. Runs on a host thread; may be called concurrently from many threads, which is why it takes &self.

Provided Methods§

Source

fn on_async_call( &self, session: AsyncSession, ctx: Self::Ctx, ) -> impl Future<Output = Reply> + Send

Handles one ASYNC_ENTRIES call. The ABI callback returns Ok immediately; the reply is delivered whenever the returned future completes, from whatever thread runs it — the host’s standard async path supports this deferred, cross-thread reply natively. Implement with plain async fn syntax; no async_trait crate is needed.

Reply::Fail is sent to the host as a result (so an awaiting caller fails instead of hanging); Reply::None sends nothing.

Source

fn spawn_async(&self, task: AsyncTask)

Runs an async handler’s task. Only futures that are still pending after their first poll arrive here — ready-on-first-poll handlers deliver inline on the host thread and never touch the executor. The default drives the task on a dedicated thread with a park/unpark waker — correct, dependency-free, and fine for occasional calls. Override with a real executor for throughput, e.g. tokio::spawn(task); on a runtime the plugin owns (stop it in Plugin::on_shutdown).

Source

fn on_stream_data(&self, _sid: u64, _data: &[u8]) -> NrStatus

Data the host pushes into a plugin-consumed stream.

Source

fn on_stream_close(&self, _sid: u64) -> NrStatus

The host closed a plugin-consumed stream.

Source

fn on_shutdown(&self)

Runs when the host shuts the plugin down; stop worker threads here.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§