iceoryx2 0.10.0

iceoryx2: Lock-Free Zero-Copy Interprocess Communication
Documentation
// Copyright (c) 2023 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! # Example
//!
//! ```
//! use iceoryx2::prelude::*;
//! use iceoryx2::service::header::publish_subscribe::Header;
//!
//! # fn main() -> Result<(), Box<dyn core::error::Error>> {
//! # let node = NodeBuilder::new().create::<ipc::Service>()?;
//! let service = node.service_builder(&"My/Funk/ServiceName".try_into()?)
//!     .publish_subscribe::<u64>()
//!     .open_or_create()?;
//!
//! let subscriber = service.subscriber_builder().create()?;
//!
//! while let Some(sample) = subscriber.receive()? {
//!     println!("header: {:?}", sample.header());
//! }
//! # Ok(())
//! # }
//! ```

use iceoryx2_bb_derive_macros::ZeroCopySend;
use iceoryx2_bb_elementary_traits::zero_copy_send::ZeroCopySend;

use crate::{
    identifiers::{UniqueNodeId, UniquePublisherId},
    service::header::payload_header::PayloadHeader,
};

/// Sample header used by
/// [`MessagingPattern::PublishSubscribe`](crate::service::messaging_pattern::MessagingPattern::PublishSubscribe)
#[derive(Debug, Copy, Clone, ZeroCopySend, PartialEq, Eq)]
#[repr(C)]
pub struct Header {
    node_id: UniqueNodeId,
    publisher_port_id: UniquePublisherId,
    pub(crate) number_of_elements: u64,
    pub(crate) payload_offset: u64,
}

impl PayloadHeader for Header {
    fn node_id(&self) -> UniqueNodeId {
        self.node_id
    }

    fn number_of_elements(&self) -> u64 {
        self.number_of_elements
    }
}

impl Header {
    pub(crate) fn new(
        node_id: UniqueNodeId,
        publisher_port_id: UniquePublisherId,
        number_of_elements: u64,
    ) -> Self {
        Self {
            node_id,
            publisher_port_id,
            number_of_elements,
            payload_offset: 0,
        }
    }

    /// Returns the [`UniquePublisherId`] of the source
    /// [`Publisher`](crate::port::publisher::Publisher).
    pub fn publisher_id(&self) -> UniquePublisherId {
        self.publisher_port_id
    }

    /// Returns the payload offset.
    pub fn payload_offset(&self) -> u64 {
        self.payload_offset
    }
}