indexbus_kit/typed/
kit.rs

1use indexbus_core::{
2    FanoutConsumer, FanoutMpscProducer, FanoutProducer, MpscConsumer, MpscProducer, SpscReceiver,
3    SpscSender,
4};
5use indexbus_typed::{
6    TypedFanoutConsumer, TypedFanoutMpscProducer, TypedFanoutProducer, TypedMpscConsumer,
7    TypedMpscProducer, TypedSpscReceiver, TypedSpscSender,
8};
9
10/// Generic typed-codec “kit” that builds typed producers/consumers for any codec.
11///
12/// The kit is intentionally tiny: it mostly provides constructors and a couple of
13/// ergonomic helpers. The underlying typed wrappers (`indexbus-typed`) are the source of truth.
14#[derive(Debug, Clone, Copy)]
15pub struct CodecKit<C> {
16    /// The codec instance used by the typed wrappers created by this kit.
17    pub codec: C,
18}
19
20impl<C> CodecKit<C> {
21    /// Create a new codec kit from a codec instance.
22    pub const fn new(codec: C) -> Self {
23        Self { codec }
24    }
25}
26
27impl<C: Copy> CodecKit<C> {
28    #[inline]
29    /// Wrap an SPSC sender with typed messaging.
30    pub fn spsc_sender(self, inner: SpscSender) -> TypedSpscSender<C> {
31        TypedSpscSender::new(inner, self.codec)
32    }
33
34    #[inline]
35    /// Wrap an SPSC receiver with typed messaging.
36    pub fn spsc_receiver(self, inner: SpscReceiver) -> TypedSpscReceiver<C> {
37        TypedSpscReceiver::new(inner, self.codec)
38    }
39
40    #[inline]
41    /// Wrap an MPSC producer with typed messaging.
42    pub fn mpsc_producer(self, inner: MpscProducer) -> TypedMpscProducer<C> {
43        TypedMpscProducer::new(inner, self.codec)
44    }
45
46    #[inline]
47    /// Wrap an MPSC consumer with typed messaging.
48    pub fn mpsc_consumer(self, inner: MpscConsumer) -> TypedMpscConsumer<C> {
49        TypedMpscConsumer::new(inner, self.codec)
50    }
51
52    #[inline]
53    /// Wrap a fanout producer with typed messaging.
54    pub fn fanout_producer<const N: usize>(
55        self,
56        inner: FanoutProducer<N>,
57    ) -> TypedFanoutProducer<N, C> {
58        TypedFanoutProducer::new(inner, self.codec)
59    }
60
61    #[inline]
62    /// Wrap a fanout consumer with typed messaging.
63    pub fn fanout_consumer<const N: usize>(
64        self,
65        inner: FanoutConsumer<N>,
66    ) -> TypedFanoutConsumer<N, C> {
67        TypedFanoutConsumer::new(inner, self.codec)
68    }
69
70    #[inline]
71    /// Wrap a fanout MPSC producer with typed messaging.
72    pub fn fanout_mpsc_producer<const N: usize>(
73        self,
74        inner: FanoutMpscProducer<N>,
75    ) -> TypedFanoutMpscProducer<N, C> {
76        TypedFanoutMpscProducer::new(inner, self.codec)
77    }
78}