Crate indexbus_typed

Crate indexbus_typed 

Source
Expand description

Typed structured-messaging adapters over indexbus-core.

The core remains bytes-first; this crate adds a validated envelope + codec-based typing.

§Feature flags

  • std (default): enables std::error::Error and Display for Error.
  • alloc: enables codecs/adapters that require allocation (this crate itself does not require allocation for the raw/borrowed paths).

§Envelope and partial effects

Messages are encoded as a v1 header prefix (indexbus_msg) followed by codec-defined payload bytes.

Receive-side helpers follow indexbus-core semantics: if a message is dequeued and decoding or validation fails, the message is considered consumed. This prevents a single malformed message from poisoning the queue.

§Minimal typed SPSC (custom codec)

use indexbus_abi::layouts::SharedLayout;
use indexbus_codec::{CodecError, CodecId, DecodeFrom, EncodeInto};
use indexbus_core::{init_shared_layout, split_spsc, SharedLayoutCell};
use indexbus_typed::{meta, IndexbusMsgSpec, MsgMeta, TypedSpscReceiver, TypedSpscSender};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct MyMsg(u32);

impl IndexbusMsgSpec for MyMsg {
    const META: MsgMeta = meta(1, 7, 1);
}

#[derive(Clone, Copy)]
struct U32LeCodec;

impl CodecId for U32LeCodec {
    const CODEC_ID: u8 = 99;
}

impl EncodeInto<MyMsg> for U32LeCodec {
    fn encode_into(&self, value: &MyMsg, out: &mut [u8]) -> Result<usize, CodecError> {
        if out.len() < 4 {
            return Err(CodecError::BufferTooSmall {
                required: 4,
                provided: out.len(),
            });
        }
        out[..4].copy_from_slice(&value.0.to_le_bytes());
        Ok(4)
    }
}

impl DecodeFrom<MyMsg> for U32LeCodec {
    fn decode_from(&self, input: &[u8]) -> Result<MyMsg, CodecError> {
        if input.len() < 4 {
            return Err(CodecError::InvalidData);
        }
        let mut b = [0u8; 4];
        b.copy_from_slice(&input[..4]);
        Ok(MyMsg(u32::from_le_bytes(b)))
    }
}

// Allocate the shared layout on the heap (large type).
let mut shared: Box<SharedLayout> = Box::new(unsafe { core::mem::zeroed() });
init_shared_layout(&mut shared);
let cell = SharedLayoutCell::from_mut(&mut shared);
let (tx0, rx0) = split_spsc(cell).unwrap();

let tx = TypedSpscSender::new(tx0, U32LeCodec);
let rx = TypedSpscReceiver::new(rx0, U32LeCodec);

tx.publish_msg(&MyMsg(42)).unwrap();
let (_hdr, got) = rx.try_recv_owned_msg::<MyMsg>().unwrap().unwrap();
assert_eq!(got, MyMsg(42));

Modules§

prelude
Curated prelude.

Structs§

MsgMeta
Expected message identity for receive-side filtering.
TypedFanoutConsumer
Typed fanout consumer that decodes v1-framed messages using codec C.
TypedFanoutMpscProducer
Typed fanout MPSC producer that publishes into the producer→router MPSC queue.
TypedFanoutProducer
Typed fanout SPSC producer that publishes into the producer→router queue.
TypedMpscConsumer
Typed MPSC consumer that decodes v1-framed messages using codec C.
TypedMpscProducer
Typed MPSC producer that publishes v1-framed messages using codec C.
TypedSpscReceiver
Typed SPSC receiver that decodes v1-framed messages using codec C.
TypedSpscSender
Typed SPSC sender that publishes v1-framed messages using codec C.

Enums§

Error
Typed-layer error.

Traits§

CodecId
Stable, compile-time codec identity.
HasCodecId
Stable, compile-time codec identity.
IndexbusMsgSpec
Compile-time message identity.

Functions§

meta
Const helper for constructing MsgMeta with the common default flags = 0.