#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#![allow(unsafe_code)]
#[cfg(feature = "alloc")]
extern crate alloc;
#[cfg(feature = "std")]
mod allocator;
#[cfg(feature = "std")]
mod backend;
#[cfg(feature = "iceoryx2-bridge")]
mod iceoryx;
#[cfg(feature = "alloc")]
mod locator;
#[cfg(feature = "posix-mmap")]
mod posix;
#[cfg(feature = "std")]
mod pubsub;
mod slot;
#[cfg(feature = "std")]
pub use allocator::{InMemorySlotAllocator, SlotError, SlotHandle};
#[cfg(feature = "std")]
pub use backend::SlotBackend;
#[cfg(feature = "iceoryx2-bridge")]
pub use iceoryx::{Iceoryx2Error, Iceoryx2Publisher, Iceoryx2Subscriber};
#[cfg(feature = "alloc")]
pub use locator::{LocatorError, ShmLocator, fnv1a_32, is_same_host};
#[cfg(feature = "posix-mmap")]
pub use posix::{PosixSlotAllocator, PosixSlotError};
#[cfg(feature = "std")]
pub use pubsub::{FlatReader, FlatSampleRef, FlatSlot, FlatWriter};
pub use slot::{ReaderMask, SLOT_HEADER_SIZE, SlotHeader};
pub unsafe trait FlatStruct: Copy + 'static + Send + Sync {
const WIRE_SIZE: usize = core::mem::size_of::<Self>();
const TYPE_HASH: [u8; 16];
#[must_use]
fn as_bytes(&self) -> &[u8] {
unsafe {
core::slice::from_raw_parts(core::ptr::from_ref(self).cast::<u8>(), Self::WIRE_SIZE)
}
}
#[must_use]
unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self {
debug_assert!(bytes.len() >= Self::WIRE_SIZE);
unsafe { core::ptr::read_unaligned(bytes.as_ptr().cast::<Self>()) }
}
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used)]
mod tests {
use super::*;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(C)]
struct Pose {
x: i64,
y: i64,
z: i64,
}
unsafe impl FlatStruct for Pose {
const TYPE_HASH: [u8; 16] = [0x42; 16];
}
#[test]
fn wire_size_matches_size_of() {
assert_eq!(Pose::WIRE_SIZE, core::mem::size_of::<Pose>());
assert_eq!(Pose::WIRE_SIZE, 24);
}
#[test]
fn as_bytes_roundtrip() {
let p = Pose { x: 1, y: 2, z: 3 };
let bytes = p.as_bytes();
assert_eq!(bytes.len(), 24);
let p2: Pose = unsafe { Pose::from_bytes_unchecked(bytes) };
assert_eq!(p, p2);
}
#[test]
fn type_hash_is_consistent() {
assert_eq!(Pose::TYPE_HASH, [0x42; 16]);
}
}