iceoryx2_ffi/api/
unique_subscriber_id.rs

1// Copyright (c) 2024 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13#![allow(non_camel_case_types)]
14
15use iceoryx2::port::port_identifiers::UniqueSubscriberId;
16use iceoryx2_bb_elementary::static_assert::static_assert_ge;
17use iceoryx2_ffi_macros::iceoryx2_ffi;
18
19use crate::api::{AssertNonNullHandle, HandleToType};
20
21// BEGIN types definition
22
23/// The system-wide unique id of a `iox2_subscriber_t`.
24#[repr(C)]
25#[repr(align(4))] // core::mem::align_of::<UniqueSubscriberId>()
26pub struct iox2_unique_subscriber_id_storage_t {
27    internal: [u8; 20], // core::mem::size_of::<Option<UniqueSubscriberId>>()
28}
29
30#[repr(C)]
31#[iceoryx2_ffi(UniqueSubscriberId)]
32pub struct iox2_unique_subscriber_id_t {
33    pub value: iox2_unique_subscriber_id_storage_t,
34    pub(super) deleter: fn(*mut iox2_unique_subscriber_id_t),
35}
36
37impl iox2_unique_subscriber_id_t {
38    pub(super) fn init(
39        &mut self,
40        value: UniqueSubscriberId,
41        deleter: fn(*mut iox2_unique_subscriber_id_t),
42    ) {
43        self.value.init(value);
44        self.deleter = deleter;
45    }
46}
47
48pub struct iox2_unique_subscriber_id_h_t;
49/// The owning handle for [`iox2_unique_subscriber_id_t`]. Passing the handle to an function transfers the ownership.
50pub type iox2_unique_subscriber_id_h = *mut iox2_unique_subscriber_id_h_t;
51/// The non-owning handle for [`iox2_unique_subscriber_id_t`]. Passing the handle to an function does not transfers the ownership.
52pub type iox2_unique_subscriber_id_h_ref = *const iox2_unique_subscriber_id_h;
53
54impl AssertNonNullHandle for iox2_unique_subscriber_id_h {
55    fn assert_non_null(self) {
56        debug_assert!(!self.is_null());
57    }
58}
59
60impl AssertNonNullHandle for iox2_unique_subscriber_id_h_ref {
61    fn assert_non_null(self) {
62        debug_assert!(!self.is_null());
63        unsafe {
64            debug_assert!(!(*self).is_null());
65        }
66    }
67}
68
69impl HandleToType for iox2_unique_subscriber_id_h {
70    type Target = *mut iox2_unique_subscriber_id_t;
71
72    fn as_type(self) -> Self::Target {
73        self as *mut _ as _
74    }
75}
76
77impl HandleToType for iox2_unique_subscriber_id_h_ref {
78    type Target = *mut iox2_unique_subscriber_id_t;
79
80    fn as_type(self) -> Self::Target {
81        unsafe { *self as *mut _ as _ }
82    }
83}
84
85// END types definition
86
87// BEGIN C API
88
89/// This function needs to be called to destroy the unique subscriber id!
90///
91/// # Arguments
92///
93/// * `handle` - A valid [`iox2_unique_subscriber_id_h`]
94///
95/// # Safety
96///
97/// * The `handle` is invalid after the return of this function and leads to undefined behavior if used in another function call!
98#[no_mangle]
99pub unsafe extern "C" fn iox2_unique_subscriber_id_drop(handle: iox2_unique_subscriber_id_h) {
100    debug_assert!(!handle.is_null());
101
102    let h = &mut *handle.as_type();
103    core::ptr::drop_in_place(h.value.as_option_mut());
104    (h.deleter)(h);
105}
106
107/// Checks two [`iox2_unique_subscriber_id_t`] for equality.
108///
109/// # Safety
110///
111/// * `lhs` - Must be a valid [`iox2_unique_subscriber_id_h_ref`]
112/// * `rhs` - Must be a valid [`iox2_unique_subscriber_id_h_ref`]
113#[no_mangle]
114pub unsafe extern "C" fn iox2_unique_subscriber_id_eq(
115    lhs: iox2_unique_subscriber_id_h_ref,
116    rhs: iox2_unique_subscriber_id_h_ref,
117) -> bool {
118    lhs.assert_non_null();
119    rhs.assert_non_null();
120
121    let lhs = &mut *lhs.as_type();
122    let rhs = &mut *rhs.as_type();
123
124    lhs.value.as_ref() == rhs.value.as_ref()
125}
126
127/// Checks the ordering of two [`iox2_unique_subscriber_id_t`].
128///
129/// # Safety
130///
131/// * `lhs` - Must be a valid [`iox2_unique_subscriber_id_h_ref`]
132/// * `rhs` - Must be a valid [`iox2_unique_subscriber_id_h_ref`]
133#[no_mangle]
134pub unsafe extern "C" fn iox2_unique_subscriber_id_less(
135    lhs: iox2_unique_subscriber_id_h_ref,
136    rhs: iox2_unique_subscriber_id_h_ref,
137) -> bool {
138    lhs.assert_non_null();
139    rhs.assert_non_null();
140
141    let lhs = &mut *lhs.as_type();
142    let rhs = &mut *rhs.as_type();
143
144    lhs.value.as_ref() < rhs.value.as_ref()
145}
146
147// END C API