iceoryx2_ffi/api/
mod.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::prelude::*;
16use iceoryx2_bb_container::semantic_string::SemanticStringError;
17
18use core::ffi::{c_int, c_void};
19
20mod config;
21mod event_id;
22mod iceoryx2_settings;
23mod listener;
24mod log;
25mod message_type_details;
26mod node;
27mod node_builder;
28mod node_name;
29mod notifier;
30mod port_factory_event;
31mod port_factory_listener_builder;
32mod port_factory_notifier_builder;
33mod port_factory_pub_sub;
34mod port_factory_publisher_builder;
35mod port_factory_subscriber_builder;
36mod publish_subscribe_header;
37mod publisher;
38mod quirks_correction;
39mod sample;
40mod sample_mut;
41mod service;
42mod service_builder;
43mod service_builder_event;
44mod service_builder_pub_sub;
45mod service_name;
46mod static_config;
47mod static_config_event;
48mod static_config_publish_subscribe;
49mod subscriber;
50mod unique_listener_id;
51mod unique_notifier_id;
52mod unique_publisher_id;
53mod unique_subscriber_id;
54
55pub use config::*;
56pub use event_id::*;
57pub use iceoryx2_settings::*;
58pub use listener::*;
59pub use message_type_details::*;
60pub use node::*;
61pub use node_builder::*;
62pub use node_name::*;
63pub use notifier::*;
64pub use port_factory_event::*;
65pub use port_factory_listener_builder::*;
66pub use port_factory_notifier_builder::*;
67pub use port_factory_pub_sub::*;
68pub use port_factory_publisher_builder::*;
69pub use port_factory_subscriber_builder::*;
70pub use publish_subscribe_header::*;
71pub use publisher::*;
72pub use quirks_correction::*;
73pub use sample::*;
74pub use sample_mut::*;
75pub use service::*;
76pub use service_builder::*;
77pub use service_builder_event::*;
78pub use service_builder_pub_sub::*;
79pub use service_name::*;
80pub use static_config::*;
81pub use static_config_event::*;
82pub use static_config_publish_subscribe::*;
83pub use subscriber::*;
84pub use unique_listener_id::*;
85pub use unique_notifier_id::*;
86pub use unique_publisher_id::*;
87pub use unique_subscriber_id::*;
88
89/// This constant signals an successful function call
90pub const IOX2_OK: c_int = 0;
91
92/// An alias to a `void *` which can be used to pass arbitrary data to the callback
93pub type iox2_callback_context = *mut c_void;
94
95#[repr(C)]
96#[derive(Copy, Clone)]
97pub enum iox2_callback_progression_e {
98    STOP = 0,
99    CONTINUE,
100}
101
102impl From<iox2_callback_progression_e> for CallbackProgression {
103    fn from(value: iox2_callback_progression_e) -> Self {
104        match value {
105            iox2_callback_progression_e::STOP => CallbackProgression::Stop,
106            iox2_callback_progression_e::CONTINUE => CallbackProgression::Continue,
107        }
108    }
109}
110
111#[repr(C)]
112#[derive(Copy, Clone)]
113pub enum iox2_semantic_string_error_e {
114    INVALID_CONTENT = IOX2_OK as isize + 1,
115    EXCEEDS_MAXIMUM_LENGTH,
116}
117
118impl IntoCInt for SemanticStringError {
119    fn into_c_int(self) -> c_int {
120        (match self {
121            SemanticStringError::InvalidContent => iox2_semantic_string_error_e::INVALID_CONTENT,
122            SemanticStringError::ExceedsMaximumLength => {
123                iox2_semantic_string_error_e::EXCEEDS_MAXIMUM_LENGTH
124            }
125        }) as c_int
126    }
127}
128
129/// This is a trait to convert a Rust error enum into the corresponding C error enum and then to a c_int in one go
130///
131/// # Example
132///
133/// ```no_run
134/// use core::ffi::c_int;
135/// use iceoryx2_ffi::IOX2_OK;
136///
137/// trait IntoCInt {
138///     fn into_c_int(self) -> c_int;
139/// }
140///
141/// enum FooError {
142///     BAR,
143///     BAZ
144/// }
145///
146/// #[repr(C)]
147/// #[derive(Copy, Clone)]
148/// pub enum iox2_foo_error_e {
149///     BAR = IOX2_OK as isize + 1, // start `IOX2_OK + 1` to prevent ambiguous values
150///     BAZ,
151/// }
152///
153/// impl IntoCInt for FooError {
154///     fn into_c_int(self) -> c_int {
155///         (match self {
156///             FooError::BAR => iox2_foo_error_e::BAR,
157///             FooError::BAZ => iox2_foo_error_e::BAZ,
158///         }) as c_int
159///     }
160/// }
161/// ```
162trait IntoCInt {
163    fn into_c_int(self) -> c_int;
164}
165
166trait HandleToType {
167    type Target;
168
169    // NOTE in this case, the handle `self` is already a `*mut`. Passing by value means a copy
170    // of the pointer; passing by reference make the implementation more error prone since one
171    // has to remember to de-reference `self` in order to get the `*mut`
172    #[allow(clippy::wrong_self_convention)]
173    fn as_type(self) -> Self::Target;
174}
175
176trait AssertNonNullHandle {
177    fn assert_non_null(self);
178}