indexbus_kit/typed/
mod.rs

1//! Typed-message helpers.
2
3mod kit;
4
5pub use kit::CodecKit;
6
7use crate::errors::{Error, Result};
8
9use indexbus_codec_raw::{RawCodec, RawPod};
10use indexbus_core::{FanoutConsumer, FanoutMpscProducer, FanoutProducer};
11use indexbus_typed::{
12    IndexbusMsgSpec, TypedFanoutConsumer, TypedFanoutMpscProducer, TypedFanoutProducer,
13};
14
15/// Default fast-path kit: raw POD codec.
16pub type RawKit = CodecKit<RawCodec>;
17
18#[inline]
19/// Construct the default typed-codec kit (`RawKit`).
20///
21/// This is the recommended entrypoint for most applications: it uses the raw POD codec and
22/// provides constructors for typed producers/consumers.
23pub fn raw_kit() -> RawKit {
24    CodecKit::new(indexbus_codec_raw::RawCodec)
25}
26
27// Back-compat helpers (raw codec). Prefer `raw_kit()` in new code.
28
29/// Back-compat alias for the raw typed codec.
30///
31/// New code should prefer `RawKit`/`raw_kit()`.
32pub type RawTypedCodec = RawCodec;
33
34#[inline]
35/// Construct the raw typed codec.
36///
37/// New code should prefer `raw_kit()`.
38pub fn raw_codec() -> RawTypedCodec {
39    indexbus_codec_raw::RawCodec
40}
41
42#[inline]
43/// Wrap a fanout producer with typed messaging using the raw codec.
44pub fn typed_fanout_producer<const N: usize>(
45    prod: FanoutProducer<N>,
46) -> TypedFanoutProducer<N, RawTypedCodec> {
47    TypedFanoutProducer::new(prod, raw_codec())
48}
49
50#[inline]
51/// Wrap a fanout consumer with typed messaging using the raw codec.
52pub fn typed_fanout_consumer<const N: usize>(
53    cons: FanoutConsumer<N>,
54) -> TypedFanoutConsumer<N, RawTypedCodec> {
55    TypedFanoutConsumer::new(cons, raw_codec())
56}
57
58#[inline]
59/// Wrap a fanout MPSC producer with typed messaging using the raw codec.
60pub fn typed_fanout_mpsc_producer<const N: usize>(
61    mpsc: FanoutMpscProducer<N>,
62) -> TypedFanoutMpscProducer<N, RawTypedCodec> {
63    TypedFanoutMpscProducer::new(mpsc, raw_codec())
64}
65
66#[inline]
67/// Publish a typed message as a raw POD frame.
68///
69/// This is a convenience wrapper around `TypedFanoutProducer::publish_msg` that maps errors into
70/// the kit's `Result` type.
71pub fn publish_rawpod<const N: usize, M>(
72    prod: &TypedFanoutProducer<N, RawTypedCodec>,
73    msg: &M,
74) -> Result<()>
75where
76    M: RawPod + IndexbusMsgSpec,
77{
78    prod.publish_msg::<M>(msg).map_err(Error::from)
79}
80
81#[inline]
82/// Attempt to receive the next typed raw POD frame.
83///
84/// Returns `Ok(None)` if no message is currently available.
85pub fn try_recv_rawpod<const N: usize, M>(
86    cons: &TypedFanoutConsumer<N, RawTypedCodec>,
87) -> Result<Option<(indexbus_msg::Header, M)>>
88where
89    M: RawPod + IndexbusMsgSpec,
90{
91    cons.try_recv_owned_msg::<M>().map_err(Error::from)
92}