#![warn(missing_docs)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
use std::borrow::Cow;
#[cfg(feature = "zenoh")]
pub mod zenoh;
type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
pub trait CommunicationLayer: Send + Sync {
fn publisher(&mut self, topic: &str) -> Result<Box<dyn Publisher>, BoxError>;
fn subscribe(&mut self, topic: &str) -> Result<Box<dyn Subscriber>, BoxError>;
}
pub trait Publisher: Send + Sync {
fn prepare(&self, len: usize) -> Result<Box<dyn PublishSample + '_>, BoxError>;
fn dyn_clone(&self) -> Box<dyn Publisher>;
fn publish(&self, data: &[u8]) -> Result<(), BoxError> {
let mut sample = self.prepare(data.len())?;
sample.as_mut_slice().copy_from_slice(data);
sample.publish()?;
Ok(())
}
}
pub trait PublishSample<'a>: Send + Sync {
fn as_mut_slice(&mut self) -> &mut [u8];
fn publish(self: Box<Self>) -> Result<(), BoxError>;
}
pub trait Subscriber: Send + Sync {
fn recv(&mut self) -> Result<Option<Box<dyn ReceivedSample>>, BoxError>;
}
pub trait ReceivedSample: Send + Sync {
fn get(&self) -> Cow<[u8]>;
}