Skip to main content

HostedRpcDep

Trait HostedRpcDep 

Source
pub trait HostedRpcDep:
    Send
    + Sync
    + 'static {
    type Stub: Send + Sync + 'static;

    // Required methods
    fn dispatch(
        &mut self,
        method_idx: u32,
        args: &[u8],
    ) -> Result<Vec<u8>, String>;
    fn build_stub(channel: HostedRpcChannel) -> Self::Stub;
}
Expand description

User-facing trait that opts a dependency value into the HostedRpc sharing strategy. Like HostedDep, the owner lives in the parent test runner process for the entire suite; unlike Hosted, workers do NOT see the owner type — they see a separate Stub type that calls back into the parent over the existing IPC socket through a small generated method-dispatch table.

The implementor provides:

  • type Stub: the worker-side handle type tests actually parameterise on.
  • dispatch: owner-side method dispatcher. Receives a stable method_idx plus serialized argument bytes and returns serialized result bytes (or a textual error).
  • build_stub: worker-side constructor. Wraps the supplied HostedRpcChannel into a Self::Stub that serialises calls and forwards them to the parent’s dispatcher.

Calls to a HostedRpc dep are serialised on the parent side (owner is held behind a single Mutex). Even logical &self methods do not run concurrently, matching how most singleton service handles already behave internally.

Required Associated Types§

Source

type Stub: Send + Sync + 'static

The worker-side handle type that tests parameterise on. Typically a small struct that holds a HostedRpcChannel and implements a user-defined trait by routing each method through the channel.

Required Methods§

Source

fn dispatch(&mut self, method_idx: u32, args: &[u8]) -> Result<Vec<u8>, String>

Owner-side: handle one method call. method_idx is a stable per-method index assigned by the implementor (usually generated by the #[hosted_rpc] macro; for a manual stub, the implementor picks the indices). args is the worker-supplied serialized payload. Return Ok(bytes) on success or Err(message) on failure — the message is surfaced to the calling worker as HostedRpcError::Dispatch.

Source

fn build_stub(channel: HostedRpcChannel) -> Self::Stub

Worker-side: build a Self::Stub over the channel that connects back to the parent’s owner. Called once per worker subprocess at startup, before any test body runs.

Contract — build_stub must be cheap and side-effect free. The runtime constructs one stub per registered HostedRpc dep at worker startup, before the worker has even received its first [crate::ipc::IpcCommand::RunTest]. In particular this means:

  • Do NOT call channel.call(...) from build_stub — there is no test in flight yet, so the parent’s command loop may legally send a RunTest while the stub is blocked waiting for a reply, and the IPC framing will desync.
  • Do NOT block, do I/O, or do expensive work — the stub is built unconditionally for every registered HostedRpc dep, even if the test filter doesn’t pull it into the suite.
  • Stash the channel and any small caches on Self::Stub; defer all RPC to actual method calls inside test bodies.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§