Skip to content

Commit

Permalink
[#491] Rename trait and derive macro for expressiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Dec 23, 2024
1 parent c67f030 commit ee01d63
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@

use std::ffi::CStr;

pub trait AsStringLiteral {
fn as_str_literal(&self) -> &'static CStr;
/// Trait for types that can be represented as a C-style string.
///
/// This trait provides a method to obtain a reference to a static C-style string
/// representation of the implementing type.
///
/// # Safety
///
/// Implementations of this trait must ensure that the returned `CStr` is valid
/// and remains valid for the entire lifetime of the program.
pub trait AsCStr {
fn as_const_cstr(&self) -> &'static CStr;
}
4 changes: 2 additions & 2 deletions iceoryx2-bb/elementary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
#[macro_use]
pub mod enum_gen;

mod as_string_literal;
pub use as_string_literal::*;
mod as_cstr;
pub use as_cstr::*;

pub mod alignment;
pub mod allocator;
Expand Down
28 changes: 14 additions & 14 deletions iceoryx2-ffi/ffi-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,31 +228,31 @@ fn parse_attribute_args(args: TokenStream) -> Args {
///
/// # Example
/// ```
/// use iceoryx2_ffi_macros::StringLiteral;
/// use iceoryx2_bb_elementary::AsStringLiteral;
/// use iceoryx2_ffi_macros::CStrRepr;
/// use iceoryx2_bb_elementary::AsCStr;
///
/// #[derive(StringLiteral)]
/// #[derive(CStrRepr)]
/// enum MyEnum {
/// #[CustomString = "custom variant one"]
/// #[CStr = "custom variant one"]
/// VariantOne,
/// VariantTwo,
/// }
///
/// let v1 = MyEnum::VariantOne;
/// assert_eq!(v1.as_str_literal(), c"custom variant one");
/// assert_eq!(v1.as_const_cstr(), c"custom variant one");
///
/// let v2 = MyEnum::VariantTwo;
/// assert_eq!(v2.as_str_literal(), c"variant two");
/// assert_eq!(v2.as_const_cstr(), c"variant two");
/// ```
///
#[proc_macro_derive(StringLiteral, attributes(CustomString))]
#[proc_macro_derive(CStrRepr, attributes(CStr))]
pub fn string_literal_derive(input: TokenStream) -> TokenStream {

let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let (impl_generics, type_generics, where_clause) = input.generics.split_for_impl();

// Generate implementation of `AsStringLiteral` for all enum variants.
// Generate implementation of `AsCStrRepr` for all enum variants.
let as_string_literal_impl = match input.data {
Data::Enum(ref data_enum) => {
let enum_to_string_mapping = data_enum.variants.iter().map(|variant| {
Expand All @@ -261,9 +261,9 @@ pub fn string_literal_derive(input: TokenStream) -> TokenStream {
.attrs
.iter()
.find_map(|attr| {
// Use provided `CustomString` if present, otherwise return None to trigger
// Use provided `CStr` if present, otherwise return None to trigger
// `or_else` logic.
if !attr.path().is_ident("CustomString") {
if !attr.path().is_ident("CStr") {
return None;
}
match attr.meta.require_name_value() {
Expand All @@ -286,7 +286,7 @@ pub fn string_literal_derive(input: TokenStream) -> TokenStream {
}
})
.unwrap_or_else(|| {
// No `CustomString` provided. Convert the variant name from
// No `CStr` provided. Convert the variant name from
// "UpperCamelCase" to "lowercase with spaces".
let enum_string_literal = enum_name
.to_string()
Expand Down Expand Up @@ -334,22 +334,22 @@ pub fn string_literal_derive(input: TokenStream) -> TokenStream {
});

quote! {
fn as_str_literal(&self) -> &'static ::std::ffi::CStr {
fn as_const_cstr(&self) -> &'static ::std::ffi::CStr {
match self {
#(#enum_to_string_mapping,)*
}
}
}
}
_ => {
let err = syn::Error::new_spanned(&input, "AsStringLiteral can only be derived for enums");
let err = syn::Error::new_spanned(&input, "AsCStrRepr can only be derived for enums");
return err.to_compile_error().into();
}
};

// Provide the generated trait implementation for the enum.
let expanded = quote! {
impl #impl_generics AsStringLiteral for #name #type_generics #where_clause {
impl #impl_generics AsCStr for #name #type_generics #where_clause {
#as_string_literal_impl
}
};
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-ffi/ffi/src/api/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use core::time::Duration;
use iceoryx2::config::{Config, ConfigCreationError};
use iceoryx2_bb_container::semantic_string::*;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_elementary::AsCStr;
use iceoryx2_bb_system_types::file_name::FileName;
use iceoryx2_bb_system_types::file_path::FilePath;
use iceoryx2_bb_system_types::path::Path;
use iceoryx2_ffi_macros::iceoryx2_ffi;
use iceoryx2_ffi_macros::StringLiteral;
use iceoryx2_ffi_macros::CStrRepr;
use std::mem::ManuallyDrop;

use crate::IOX2_OK;
Expand All @@ -34,7 +34,7 @@ use super::{HandleToType, IntoCInt};

/// Failures occurring while creating a new [`iox2_config_t`] object with [`iox2_config_from_file()`].
#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_config_creation_error_e {
/// The config file could not be opened.
FAILED_TO_OPEN_CONFIG_FILE = IOX2_OK as isize + 1,
Expand Down Expand Up @@ -154,7 +154,7 @@ impl HandleToType for iox2_config_h_ref {
pub unsafe extern "C" fn iox2_config_creation_error_string(
error: iox2_config_creation_error_e,
) -> *const c_char {
error.as_str_literal().as_ptr() as *const c_char
error.as_const_cstr().as_ptr() as *const c_char
}

/// This function casts a [`iox2_config_h`] into a [`iox2_config_ptr`]
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-ffi/ffi/src/api/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use crate::iox2_file_descriptor_ptr;
use iceoryx2::port::listener::Listener;
use iceoryx2::prelude::*;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_elementary::AsCStr;
use iceoryx2_bb_posix::file_descriptor::{FileDescriptor, FileDescriptorBased};
use iceoryx2_cal::event::ListenerWaitError;
use iceoryx2_ffi_macros::iceoryx2_ffi;
use iceoryx2_ffi_macros::StringLiteral;
use iceoryx2_ffi_macros::CStrRepr;

use core::ffi::{c_char, c_int};
use core::mem::ManuallyDrop;
Expand All @@ -34,7 +34,7 @@ use core::time::Duration;
// BEGIN types definition

#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_listener_wait_error_e {
CONTRACT_VIOLATION = IOX2_OK as isize + 1,
INTERNAL_FAILURE,
Expand Down Expand Up @@ -158,7 +158,7 @@ pub type iox2_listener_wait_all_callback =
pub unsafe extern "C" fn iox2_listener_wait_error_string(
error: iox2_listener_wait_error_e,
) -> *const c_char {
error.as_str_literal().as_ptr() as *const c_char
error.as_const_cstr().as_ptr() as *const c_char
}

/// This function needs to be called to destroy the listener!
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-ffi/ffi/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

use iceoryx2::prelude::*;
use iceoryx2_bb_container::semantic_string::SemanticStringError;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_ffi_macros::StringLiteral;
use iceoryx2_bb_elementary::AsCStr;
use iceoryx2_ffi_macros::CStrRepr;

use core::ffi::{c_char, c_int, c_void};

Expand Down Expand Up @@ -133,7 +133,7 @@ impl From<iox2_callback_progression_e> for CallbackProgression {
}

#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_semantic_string_error_e {
INVALID_CONTENT = IOX2_OK as isize + 1,
EXCEEDS_MAXIMUM_LENGTH,
Expand Down Expand Up @@ -219,5 +219,5 @@ trait AssertNonNullHandle {
pub unsafe extern "C" fn iox2_semantic_string_error_string(
error: iox2_semantic_string_error_e,
) -> *const c_char {
error.as_str_literal().as_ptr() as *const c_char
error.as_const_cstr().as_ptr() as *const c_char
}
14 changes: 7 additions & 7 deletions iceoryx2-ffi/ffi/src/api/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use iceoryx2::node::{
use iceoryx2::prelude::*;
use iceoryx2_bb_container::semantic_string::SemanticString;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_elementary::AsCStr;
use iceoryx2_ffi_macros::iceoryx2_ffi;
use iceoryx2_ffi_macros::StringLiteral;
use iceoryx2_ffi_macros::CStrRepr;

use core::ffi::{c_char, c_int};
use core::mem::ManuallyDrop;
Expand All @@ -39,7 +39,7 @@ use super::{iox2_config_h_ref, iox2_node_id_h_ref, iox2_node_id_ptr, iox2_signal

/// The failures that can occur when a list of node states is created with [`iox2_node_list()`].
#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_node_list_failure_e {
/// A list of all Nodes could not be created since the process does not have sufficient permissions.
INSUFFICIENT_PERMISSIONS = IOX2_OK as isize + 1,
Expand All @@ -62,7 +62,7 @@ impl IntoCInt for NodeListFailure {
}

#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_node_wait_failure_e {
INTERRUPT = IOX2_OK as isize + 1,
TERMINATION_REQUEST,
Expand All @@ -80,7 +80,7 @@ impl IntoCInt for NodeWaitFailure {
/// Failures of [`iox2_dead_node_remove_stale_resources()`] that occur when the stale resources of
/// a dead node are removed.
#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_node_cleanup_failure_e {
/// The process received an interrupt signal while cleaning up all stale resources of a dead node.
INTERRUPT = IOX2_OK as isize + 1,
Expand Down Expand Up @@ -232,7 +232,7 @@ pub type iox2_node_list_callback = extern "C" fn(
pub unsafe extern "C" fn iox2_node_list_failure_string(
error: iox2_node_list_failure_e,
) -> *const c_char {
error.as_str_literal().as_ptr() as *const c_char
error.as_const_cstr().as_ptr() as *const c_char
}

/// Returns a string representation of the [`iox2_node_wait_failure_e`] error code.
Expand All @@ -253,7 +253,7 @@ pub unsafe extern "C" fn iox2_node_list_failure_string(
pub unsafe extern "C" fn iox2_node_wait_failure_string(
error: iox2_node_wait_failure_e,
) -> *const c_char {
error.as_str_literal().as_ptr() as *const c_char
error.as_const_cstr().as_ptr() as *const c_char
}

/// Returns the [`iox2_node_name_ptr`](crate::iox2_node_name_ptr), an immutable pointer to the node name.
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-ffi/ffi/src/api/node_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ use crate::api::{
use iceoryx2::node::NodeCreationFailure;
use iceoryx2::prelude::*;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_elementary::AsCStr;
use iceoryx2_bb_log::fatal_panic;
use iceoryx2_ffi_macros::iceoryx2_ffi;
use iceoryx2_ffi_macros::StringLiteral;
use iceoryx2_ffi_macros::CStrRepr;

use core::ffi::{c_char, c_int};

Expand All @@ -32,7 +32,7 @@ use super::iox2_signal_handling_mode_e;
// BEGIN types definition

#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_node_creation_failure_e {
INSUFFICIENT_PERMISSIONS = IOX2_OK as isize + 1,
INTERNAL_ERROR,
Expand Down Expand Up @@ -121,7 +121,7 @@ impl HandleToType for iox2_node_builder_h_ref {
pub unsafe extern "C" fn iox2_node_creation_failure_string(
error: iox2_node_creation_failure_e,
) -> *const c_char {
error.as_str_literal().as_ptr() as *const c_char
error.as_const_cstr().as_ptr() as *const c_char
}

/// Creates a builder for nodes
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-ffi/ffi/src/api/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ use crate::api::{
use iceoryx2::port::notifier::{Notifier, NotifierNotifyError};
use iceoryx2::prelude::*;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_elementary::AsCStr;
use iceoryx2_ffi_macros::iceoryx2_ffi;
use iceoryx2_ffi_macros::StringLiteral;
use iceoryx2_ffi_macros::CStrRepr;

use core::ffi::{c_char, c_int};
use core::mem::ManuallyDrop;

// BEGIN types definition

#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_notifier_notify_error_e {
EVENT_ID_OUT_OF_BOUNDS = IOX2_OK as isize + 1,
}
Expand Down Expand Up @@ -149,7 +149,7 @@ impl HandleToType for iox2_notifier_h_ref {
pub unsafe extern "C" fn iox2_notifier_notify_error_string(
error: iox2_notifier_notify_error_e,
) -> *const c_char {
error.as_str_literal().as_ptr() as *const c_char
error.as_const_cstr().as_ptr() as *const c_char
}

/// Returns the unique port id of the notifier.
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-ffi/ffi/src/api/port_factory_listener_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ use iceoryx2::port::listener::ListenerCreateError;
use iceoryx2::prelude::*;
use iceoryx2::service::port_factory::listener::PortFactoryListener;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_elementary::AsCStr;
use iceoryx2_ffi_macros::iceoryx2_ffi;
use iceoryx2_ffi_macros::StringLiteral;
use iceoryx2_ffi_macros::CStrRepr;

use core::ffi::{c_char, c_int};
use core::mem::ManuallyDrop;

// BEGIN types definition

#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_listener_create_error_e {
EXCEEDS_MAX_SUPPORTED_LISTENERS = IOX2_OK as isize + 1,
RESOURCE_CREATION_FAILED,
Expand Down Expand Up @@ -154,7 +154,7 @@ impl HandleToType for iox2_port_factory_listener_builder_h_ref {
pub unsafe extern "C" fn iox2_listener_create_error_string(
error: iox2_listener_create_error_e,
) -> *const c_char {
error.as_str_literal().as_ptr() as *const c_char
error.as_const_cstr().as_ptr() as *const c_char
}

// TODO [#210] add all the other setter methods
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-ffi/ffi/src/api/port_factory_notifier_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ use iceoryx2::port::notifier::NotifierCreateError;
use iceoryx2::prelude::*;
use iceoryx2::service::port_factory::notifier::PortFactoryNotifier;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_elementary::AsCStr;
use iceoryx2_ffi_macros::iceoryx2_ffi;
use iceoryx2_ffi_macros::StringLiteral;
use iceoryx2_ffi_macros::CStrRepr;

use core::ffi::{c_char, c_int};
use core::mem::ManuallyDrop;

// BEGIN types definition

#[repr(C)]
#[derive(Copy, Clone, StringLiteral)]
#[derive(Copy, Clone, CStrRepr)]
pub enum iox2_notifier_create_error_e {
EXCEEDS_MAX_SUPPORTED_NOTIFIERS = IOX2_OK as isize + 1,
}
Expand Down Expand Up @@ -150,7 +150,7 @@ impl HandleToType for iox2_port_factory_notifier_builder_h_ref {
pub unsafe extern "C" fn iox2_notifier_create_error_string(
error: iox2_notifier_create_error_e,
) -> *const c_char {
error.as_str_literal().as_ptr() as *const c_char
error.as_const_cstr().as_ptr() as *const c_char
}

/// Sets the default event id for the builder
Expand Down
Loading

0 comments on commit ee01d63

Please sign in to comment.