Skip to main content

Adapt

Struct Adapt 

Source
pub struct Adapt<F, H> { /* private fields */ }
Expand description

Lightweight adapter that decodes a wire-format event into a domain type before dispatching to an inner handler.

Implements Handler<Wire> by calling decode(Wire) -> Option<T>, then forwarding T to the inner Handler<T>. Skips dispatch when decode returns None (wrong template, decode error, filtered, etc.).

The decode function takes Wire by value. For reference types like SBE flyweight decoders (ReadBuf<'a>), this is already a borrow - no double indirection.

Both the decode function and inner handler are concrete types — monomorphizes to a direct call chain with no vtable overhead.

§Examples

use nexus_rt::{WorldBuilder, ResMut, IntoHandler, Handler};
use nexus_rt::Adapt;

// Wire event — in practice this would be a decoder/buffer type.
struct WireMsg(u32);

// Decode takes Wire by value. For reference-type wire events
// (e.g. SBE flyweight decoders like MessageHeaderDecoder<ReadBuf<'a>>),
// this is already a borrow — no double indirection.
fn decode_wire(wire: &WireMsg) -> Option<u64> {
    Some(wire.0 as u64)
}

fn accumulate(mut counter: ResMut<u64>, event: u64) {
    *counter += event;
}

let mut builder = WorldBuilder::new();
builder.register::<u64>(0);
let mut world = builder.build();

let handler = accumulate.into_handler(world.registry());
let mut adapted: Adapt<_, _> = Adapt::new(decode_wire, handler);

// Wire type is &WireMsg — reference type taken by value.
adapted.run(&mut world, &WireMsg(10));
adapted.run(&mut world, &WireMsg(5));
assert_eq!(*world.resource::<u64>(), 15);

Implementations§

Source§

impl<F, H> Adapt<F, H>

Source

pub fn new(decode: F, inner: H) -> Self

Create a new adapter from a decode function and an inner handler.

Trait Implementations§

Source§

impl<Wire, T, F, H> Handler<Wire> for Adapt<F, H>
where F: FnMut(Wire) -> Option<T> + Send, H: Handler<T>,

Source§

fn run(&mut self, world: &mut World, event: Wire)

Run this handler with the given event.
Source§

fn name(&self) -> &'static str

Returns the handler’s name. Read more

Auto Trait Implementations§

§

impl<F, H> Freeze for Adapt<F, H>
where F: Freeze, H: Freeze,

§

impl<F, H> RefUnwindSafe for Adapt<F, H>

§

impl<F, H> Send for Adapt<F, H>
where F: Send, H: Send,

§

impl<F, H> Sync for Adapt<F, H>
where F: Sync, H: Sync,

§

impl<F, H> Unpin for Adapt<F, H>
where F: Unpin, H: Unpin,

§

impl<F, H> UnsafeUnpin for Adapt<F, H>
where F: UnsafeUnpin, H: UnsafeUnpin,

§

impl<F, H> UnwindSafe for Adapt<F, H>
where F: UnwindSafe, H: UnwindSafe,

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