use core::{fmt::Debug, ops::Deref};
use iceoryx2_bb_elementary_traits::zero_copy_send::ZeroCopySend;
use iceoryx2_bb_posix::unique_system_id::UniqueSystemId;
use iceoryx2_cal::arc_sync_policy::ArcSyncPolicy;
use iceoryx2_cal::zero_copy_connection::ChannelId;
use crate::port::details::chunk_details::ChunkDetails;
use crate::port::port_identifiers::UniquePublisherId;
use crate::port::subscriber::SubscriberSharedState;
use crate::raw_sample::RawSample;
use crate::service::header::publish_subscribe::Header;
pub struct Sample<
Service: crate::service::Service,
Payload: Debug + ?Sized + ZeroCopySend,
UserHeader: ZeroCopySend,
> {
pub(crate) ptr: RawSample<Header, UserHeader, Payload>,
pub(crate) subscriber_shared_state:
Service::ArcThreadSafetyPolicy<SubscriberSharedState<Service>>,
pub(crate) details: ChunkDetails,
}
unsafe impl<
Service: crate::service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: ZeroCopySend,
> Send for Sample<Service, Payload, UserHeader>
where
Service::ArcThreadSafetyPolicy<SubscriberSharedState<Service>>: Send + Sync,
{
}
impl<
Service: crate::service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: ZeroCopySend,
> Debug for Sample<Service, Payload, UserHeader>
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"Sample<{}, {}, {}> {{ ptr: {:?}, details: {:?} }}",
core::any::type_name::<Payload>(),
core::any::type_name::<UserHeader>(),
core::any::type_name::<Service>(),
self.ptr,
self.details,
)
}
}
impl<
Service: crate::service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: ZeroCopySend,
> Deref for Sample<Service, Payload, UserHeader>
{
type Target = Payload;
fn deref(&self) -> &Self::Target {
self.ptr.as_payload_ref()
}
}
impl<
Service: crate::service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: ZeroCopySend,
> Drop for Sample<Service, Payload, UserHeader>
{
fn drop(&mut self) {
self.subscriber_shared_state
.lock()
.receiver
.release_offset(&self.details, ChannelId::new(0));
}
}
impl<
Service: crate::service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: ZeroCopySend,
> Sample<Service, Payload, UserHeader>
{
pub fn payload(&self) -> &Payload {
self.ptr.as_payload_ref()
}
pub fn user_header(&self) -> &UserHeader {
self.ptr.as_user_header_ref()
}
pub fn header(&self) -> &Header {
self.ptr.as_header_ref()
}
pub fn origin(&self) -> UniquePublisherId {
UniquePublisherId(UniqueSystemId::from(self.details.origin))
}
}