indexbus_kit/runtime/
tier_a.rs

1use crate::errors::{Error, Result};
2
3use indexbus_adapters_core::{Backoff, TryRecvBytes};
4
5/// Adapter implementing `TryRecvBytes` for an `indexbus-core` SPSC receiver.
6///
7/// This is used to plug the core receiver into the tier-a adapter utilities.
8pub struct CoreSpscRx(pub indexbus_core::SpscReceiver);
9
10impl TryRecvBytes for CoreSpscRx {
11    fn try_recv_into(
12        &mut self,
13        out: &mut [u8],
14    ) -> core::result::Result<Option<usize>, indexbus_adapters_core::TryRecvError> {
15        self.0
16            .try_recv_into(out)
17            .map_err(|_| indexbus_adapters_core::TryRecvError::Other)
18    }
19}
20
21/// Build a tier-a receive helper over an `indexbus-core` SPSC receiver.
22///
23/// Tier-a uses polling + backoff, making it deterministic and runtime-agnostic.
24pub fn recv_into_tier_a<'a, B: Backoff>(
25    rx: indexbus_core::SpscReceiver,
26    out: &'a mut [u8],
27    backoff: B,
28) -> indexbus_adapters::tier_a::RecvIntoTierA<'a, CoreSpscRx, B> {
29    indexbus_adapters::tier_a::RecvIntoTierA::new(CoreSpscRx(rx), out, backoff)
30}
31
32/// Map an arbitrary debug value into a kit `Error`.
33pub fn map_err(e: impl core::fmt::Debug) -> Error {
34    Error::msg(format!("{e:?}"))
35}
36
37/// Convenience wrapper: `Ok(v)`.
38pub fn ok<T>(v: T) -> Result<T> {
39    Ok(v)
40}