Skip to content

Commit

Permalink
[eclipse-iceoryx#491] Rename trait to StringLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Nov 15, 2024
1 parent d833119 commit c25f804
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 137 deletions.
32 changes: 18 additions & 14 deletions iceoryx2-bb/derive-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,22 @@ pub fn placement_default_derive(input: TokenStream) -> TokenStream {
TokenStream::from(expanded)
}

#[proc_macro_derive(StaticStringRepresentation, attributes(StaticString))]
pub fn as_static_string_derive(input: TokenStream) -> TokenStream {
#[proc_macro_derive(StringLiteral, attributes(CustomString))]
pub fn string_literal_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

let static_string_impl = match input.data {
let string_literal_impl = match input.data {
Data::Enum(ref data_enum) => {
let match_arms = data_enum.variants.iter().map(|variant| {
let variant_ident = &variant.ident;

// Get the StaticString attribute if it exists
let static_string = variant
.attrs
.iter()
.find_map(|attr| {
if !attr.path().is_ident("StaticString") {
if !attr.path().is_ident("CustomString") {
return None;
}

Expand Down Expand Up @@ -152,40 +151,45 @@ pub fn as_static_string_derive(input: TokenStream) -> TokenStream {
match &variant.fields {
Fields::Unit => {
quote! {
Self::#variant_ident => concat!(#static_string, "\0")
Self::#variant_ident => concat!(#static_string, "\0").as_ptr()
}
}
Fields::Unnamed(_) => {
quote! {
Self::#variant_ident(..) => concat!(#static_string, "\0")
Self::#variant_ident(..) => concat!(#static_string, "\0").as_ptr()
}
}
Fields::Named(_) => {
quote! {
Self::#variant_ident{..} => concat!(#static_string, "\0")
Self::#variant_ident{..} => concat!(#static_string, "\0").as_ptr()
}
}
}
});

quote! {
fn as_static_str(&self) -> &'static str {
match self {
#(#match_arms,)*
fn as_str_literal(&self) -> &'static str {
unsafe {
std::str::from_utf8_unchecked(
std::ffi::CStr::from_ptr(match self {
#(#match_arms,)*
} as *const i8)
.to_bytes()
)
}
}
}
}
_ => {
let err =
syn::Error::new_spanned(&input, "AsStaticString can only be derived for enums");
syn::Error::new_spanned(&input, "AsStringLiteral can only be derived for enums");
return err.to_compile_error().into();
}
};

let expanded = quote! {
impl #impl_generics AsStaticString for #name #ty_generics #where_clause {
#static_string_impl
impl #impl_generics AsStringLiteral for #name #ty_generics #where_clause {
#string_literal_impl
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

pub trait AsStaticString {
fn as_static_str(&self) -> &'static str;
pub trait AsStringLiteral {
fn as_str_literal(&self) -> &'static str;
}
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_static_string;
pub use as_static_string::*;
mod as_string_literal;
pub use as_string_literal::*;

pub mod alignment;
pub mod allocator;
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 @@ -17,9 +17,9 @@ use core::ffi::{c_char, c_int};
use core::time::Duration;
use iceoryx2::config::{Config, ConfigCreationError};
use iceoryx2_bb_container::semantic_string::*;
use iceoryx2_bb_derive_macros::StaticStringRepresentation;
use iceoryx2_bb_derive_macros::StringLiteral;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStaticString;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_system_types::file_name::FileName;
use iceoryx2_bb_system_types::file_path::FilePath;
use iceoryx2_bb_system_types::path::Path;
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, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
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 @@ -140,7 +140,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_static_str().as_ptr() as *const c_char
error.as_str_literal().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 @@ -20,9 +20,9 @@ use crate::iox2_file_descriptor_ptr;

use iceoryx2::port::listener::Listener;
use iceoryx2::prelude::*;
use iceoryx2_bb_derive_macros::StaticStringRepresentation;
use iceoryx2_bb_derive_macros::StringLiteral;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStaticString;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_posix::file_descriptor::{FileDescriptor, FileDescriptorBased};
use iceoryx2_cal::event::ListenerWaitError;
use iceoryx2_ffi_macros::iceoryx2_ffi;
Expand All @@ -34,7 +34,7 @@ use core::time::Duration;
// BEGIN types definition

#[repr(C)]
#[derive(Copy, Clone, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
pub enum iox2_listener_wait_error_e {
CONTRACT_VIOLATION = IOX2_OK as isize + 1,
INTERNAL_FAILURE,
Expand Down Expand Up @@ -144,7 +144,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_static_str().as_ptr() as *const c_char
error.as_str_literal().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_derive_macros::StaticStringRepresentation;
use iceoryx2_bb_elementary::AsStaticString;
use iceoryx2_bb_derive_macros::StringLiteral;
use iceoryx2_bb_elementary::AsStringLiteral;

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

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

#[repr(C)]
#[derive(Copy, Clone, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
pub enum iox2_semantic_string_error_e {
INVALID_CONTENT = IOX2_OK as isize + 1,
EXCEEDS_MAXIMUM_LENGTH,
Expand Down Expand Up @@ -193,5 +193,5 @@ trait AssertNonNullHandle {
pub unsafe extern "C" fn iox2_semantic_string_error_string(
error: iox2_semantic_string_error_e,
) -> *const c_char {
error.as_static_str().as_ptr() as *const c_char
error.as_str_literal().as_ptr() as *const c_char
}
12 changes: 6 additions & 6 deletions iceoryx2-ffi/ffi/src/api/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::api::{

use iceoryx2::node::{NodeId, NodeListFailure, NodeView, NodeWaitFailure};
use iceoryx2::prelude::*;
use iceoryx2_bb_derive_macros::StaticStringRepresentation;
use iceoryx2_bb_derive_macros::StringLiteral;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStaticString;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_ffi_macros::iceoryx2_ffi;

use core::ffi::{c_char, c_int};
Expand All @@ -32,7 +32,7 @@ use std::time::Duration;
// BEGIN type definition

#[repr(C)]
#[derive(Copy, Clone, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
pub enum iox2_node_list_failure_e {
INSUFFICIENT_PERMISSIONS = IOX2_OK as isize + 1,
INTERRUPT,
Expand All @@ -52,7 +52,7 @@ impl IntoCInt for NodeListFailure {
}

#[repr(C)]
#[derive(Copy, Clone, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
pub enum iox2_node_wait_failure_e {
INTERRUPT = IOX2_OK as isize + 1,
TERMINATION_REQUEST,
Expand Down Expand Up @@ -191,14 +191,14 @@ 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_static_str().as_ptr() as *const c_char
error.as_str_literal().as_ptr() as *const c_char
}

#[no_mangle]
pub unsafe extern "C" fn iox2_node_wait_failure_string(
error: iox2_node_wait_failure_e,
) -> *const c_char {
error.as_static_str().as_ptr() as *const c_char
error.as_str_literal().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 @@ -19,9 +19,9 @@ use crate::api::{

use iceoryx2::node::NodeCreationFailure;
use iceoryx2::prelude::*;
use iceoryx2_bb_derive_macros::StaticStringRepresentation;
use iceoryx2_bb_derive_macros::StringLiteral;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStaticString;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_bb_log::fatal_panic;
use iceoryx2_ffi_macros::iceoryx2_ffi;

Expand All @@ -30,7 +30,7 @@ use core::ffi::{c_char, c_int};
// BEGIN types definition

#[repr(C)]
#[derive(Copy, Clone, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
pub enum iox2_node_creation_failure_e {
INSUFFICIENT_PERMISSIONS = IOX2_OK as isize + 1,
INTERNAL_ERROR,
Expand Down Expand Up @@ -105,7 +105,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_static_str().as_ptr() as *const c_char
error.as_str_literal().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 @@ -19,9 +19,9 @@ use crate::api::{

use iceoryx2::port::notifier::{Notifier, NotifierNotifyError};
use iceoryx2::prelude::*;
use iceoryx2_bb_derive_macros::StaticStringRepresentation;
use iceoryx2_bb_derive_macros::StringLiteral;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStaticString;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_ffi_macros::iceoryx2_ffi;

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

#[repr(C)]
#[derive(Copy, Clone, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
pub enum iox2_notifier_notify_error_e {
EVENT_ID_OUT_OF_BOUNDS = IOX2_OK as isize + 1,
}
Expand Down Expand Up @@ -135,7 +135,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_static_str().as_ptr() as *const c_char
error.as_str_literal().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 @@ -20,9 +20,9 @@ use crate::api::{
use iceoryx2::port::listener::ListenerCreateError;
use iceoryx2::prelude::*;
use iceoryx2::service::port_factory::listener::PortFactoryListener;
use iceoryx2_bb_derive_macros::StaticStringRepresentation;
use iceoryx2_bb_derive_macros::StringLiteral;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStaticString;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_ffi_macros::iceoryx2_ffi;

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

#[repr(C)]
#[derive(Copy, Clone, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
pub enum iox2_listener_create_error_e {
EXCEEDS_MAX_SUPPORTED_LISTENERS = IOX2_OK as isize + 1,
RESOURCE_CREATION_FAILED,
Expand Down Expand Up @@ -140,7 +140,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_static_str().as_ptr() as *const c_char
error.as_str_literal().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 @@ -20,9 +20,9 @@ use crate::api::{
use iceoryx2::port::notifier::NotifierCreateError;
use iceoryx2::prelude::*;
use iceoryx2::service::port_factory::notifier::PortFactoryNotifier;
use iceoryx2_bb_derive_macros::StaticStringRepresentation;
use iceoryx2_bb_derive_macros::StringLiteral;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStaticString;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_ffi_macros::iceoryx2_ffi;

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

#[repr(C)]
#[derive(Copy, Clone, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
pub enum iox2_notifier_create_error_e {
EXCEEDS_MAX_SUPPORTED_NOTIFIERS = IOX2_OK as isize + 1,
}
Expand Down Expand Up @@ -136,7 +136,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_static_str().as_ptr() as *const c_char
error.as_str_literal().as_ptr() as *const c_char
}

/// Sets the default event id for the builder
Expand Down
8 changes: 4 additions & 4 deletions iceoryx2-ffi/ffi/src/api/port_factory_publisher_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ use crate::api::{
use iceoryx2::port::publisher::PublisherCreateError;
use iceoryx2::prelude::*;
use iceoryx2::service::port_factory::publisher::PortFactoryPublisher;
use iceoryx2_bb_derive_macros::StaticStringRepresentation;
use iceoryx2_bb_derive_macros::StringLiteral;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_bb_elementary::AsStaticString;
use iceoryx2_bb_elementary::AsStringLiteral;
use iceoryx2_ffi_macros::iceoryx2_ffi;

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

#[repr(C)]
#[derive(Copy, Clone, StaticStringRepresentation)]
#[derive(Copy, Clone, StringLiteral)]
pub enum iox2_publisher_create_error_e {
EXCEEDS_MAX_SUPPORTED_PUBLISHERS = IOX2_OK as isize + 1,
UNABLE_TO_CREATE_DATA_SEGMENT,
Expand Down Expand Up @@ -179,7 +179,7 @@ impl HandleToType for iox2_port_factory_publisher_builder_h_ref {
pub unsafe extern "C" fn iox2_publisher_create_error_string(
error: iox2_publisher_create_error_e,
) -> *const c_char {
error.as_static_str().as_ptr() as *const c_char
error.as_str_literal().as_ptr() as *const c_char
}

/// Sets the max slice length for the publisher
Expand Down
Loading

0 comments on commit c25f804

Please sign in to comment.