#![cfg_attr(not(feature = "shm"), allow(dead_code, unused_imports))]
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::sync::OnceLock;
use std::sync::atomic::{AtomicBool, Ordering};
pub unsafe trait PlainType: Copy + Send + Sync + 'static {}
macro_rules! plain {
($($t:ty),*) => {
$(
unsafe impl PlainType for $t {}
)*
};
}
plain!(u8, i8, u16, i16, u32, i32, u64, i64, f32, f64);
unsafe impl<T: PlainType, const N: usize> PlainType for [T; N] {}
pub(crate) fn zeroed_box<T: PlainType>() -> Box<T> {
let layout = std::alloc::Layout::new::<T>();
if layout.size() == 0 {
return Box::new(unsafe { std::mem::zeroed() });
}
unsafe {
let p = std::alloc::alloc_zeroed(layout) as *mut T;
if p.is_null() {
std::alloc::handle_alloc_error(layout);
}
Box::from_raw(p)
}
}
#[derive(Debug, Default)]
pub(crate) struct LoanCheck {
pub(crate) header: OnceLock<Option<[u8; 4]>>,
verified: AtomicBool,
}
impl LoanCheck {
pub(crate) fn verified(&self) -> bool {
self.verified.load(Ordering::Relaxed)
}
pub(crate) fn set_verified(&self) {
self.verified.store(true, Ordering::Relaxed);
}
}
pub struct WriteLoan<T: PlainType> {
pub(crate) inner: WriteInner<T>,
}
pub(crate) enum WriteInner<T> {
#[cfg(feature = "shm")]
Shared(rustorb_rtps::shm::PoolSlot, PhantomData<T>),
Heap(Box<T>),
}
impl<T: PlainType> WriteLoan<T> {
pub(crate) fn heap() -> Self {
WriteLoan {
inner: WriteInner::Heap(zeroed_box()),
}
}
#[cfg(feature = "shm")]
pub(crate) fn value_of(slot: &rustorb_rtps::shm::PoolSlot) -> &T {
unsafe { &*(slot.bytes()[4..].as_ptr() as *const T) }
}
pub fn is_shared(&self) -> bool {
match &self.inner {
#[cfg(feature = "shm")]
WriteInner::Shared(..) => true,
WriteInner::Heap(_) => false,
}
}
}
impl<T: PlainType> std::fmt::Debug for WriteLoan<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WriteLoan")
.field("shared", &self.is_shared())
.finish()
}
}
impl<T: PlainType> Deref for WriteLoan<T> {
type Target = T;
fn deref(&self) -> &T {
match &self.inner {
#[cfg(feature = "shm")]
WriteInner::Shared(slot, _) => Self::value_of(slot),
WriteInner::Heap(b) => b,
}
}
}
impl<T: PlainType> DerefMut for WriteLoan<T> {
fn deref_mut(&mut self) -> &mut T {
match &mut self.inner {
#[cfg(feature = "shm")]
WriteInner::Shared(slot, _) => {
unsafe { &mut *(slot.bytes_mut()[4..].as_mut_ptr() as *mut T) }
}
WriteInner::Heap(b) => b,
}
}
}
pub struct ReadLoan<T: PlainType> {
inner: ReadInner<T>,
}
enum ReadInner<T> {
#[cfg(feature = "shm")]
Shared(rustorb_rtps::shm::PoolLoan, PhantomData<T>),
Owned(Box<T>),
}
impl<T: PlainType> ReadLoan<T> {
pub(crate) fn owned(v: T) -> Self {
ReadLoan {
inner: ReadInner::Owned(Box::new(v)),
}
}
#[cfg(feature = "shm")]
pub(crate) fn shared(l: &rustorb_rtps::shm::PoolLoan) -> Option<Self> {
let b = l.bytes();
let native = u8::from(cfg!(target_endian = "little"));
let fits = b.len() == 4 + std::mem::size_of::<T>()
&& b[0] == 0
&& b[1] & 1 == native
&& (b[4..].as_ptr() as usize).is_multiple_of(std::mem::align_of::<T>());
fits.then_some(ReadLoan {
inner: ReadInner::Shared(l.clone(), PhantomData),
})
}
pub fn is_shared(&self) -> bool {
match &self.inner {
#[cfg(feature = "shm")]
ReadInner::Shared(..) => true,
ReadInner::Owned(_) => false,
}
}
}
impl<T: PlainType> Deref for ReadLoan<T> {
type Target = T;
fn deref(&self) -> &T {
match &self.inner {
#[cfg(feature = "shm")]
ReadInner::Shared(l, _) => {
unsafe { &*(l.bytes()[4..].as_ptr() as *const T) }
}
ReadInner::Owned(v) => v,
}
}
}
impl<T: PlainType + std::fmt::Debug> std::fmt::Debug for ReadLoan<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
(**self).fmt(f)
}
}