Skip to content

Commit

Permalink
[eclipse-iceoryx#224] Add PlacementNew derive macro
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed May 27, 2024
1 parent 212ecfc commit 9c6e398
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"iceoryx2-bb/lock-free/",
"iceoryx2-bb/threadsafe/",
"iceoryx2-bb/container",
"iceoryx2-bb/derive-macros",
"iceoryx2-bb/elementary",
"iceoryx2-bb/log",
"iceoryx2-bb/memory",
Expand Down Expand Up @@ -40,6 +41,7 @@ version = "0.3.0"
iceoryx2-bb-threadsafe = { version = "0.3.0", path = "iceoryx2-bb/threadsafe/" }
iceoryx2-bb-lock-free = { version = "0.3.0", path = "iceoryx2-bb/lock-free/" }
iceoryx2-bb-container = { version = "0.3.0", path = "iceoryx2-bb/container/" }
iceoryx2-bb-derive-macros = { version = "0.3.0", path = "iceoryx2-bb/derive-macros/" }
iceoryx2-bb-elementary = { version = "0.3.0", path = "iceoryx2-bb/elementary/" }
iceoryx2-bb-log = { version = "0.3.0", path = "iceoryx2-bb/log/" }
iceoryx2-bb-memory = { version = "0.3.0", path = "iceoryx2-bb/memory/" }
Expand Down Expand Up @@ -68,8 +70,11 @@ log = { version = "0.4.21" }
once_cell = { version = "1.19.0" }
ouroboros = { version = "0.18.4" }
pin-init = { version = "0.2.0" }
proc-macro2 = { version = "1.0.84" }
quote = { version = "1.0.36" }
serde = { version = "1.0.203", features = ["derive"] }
sha1_smol = { version = "1.0.0" }
syn = { version = "2.0.66", features = ["full"] }
termsize = { version = "0.1.6" }
tiny-fn = { version = "0.1.5" }
toml = { version = "0.8.13" }
Expand Down
21 changes: 21 additions & 0 deletions iceoryx2-bb/derive-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "iceoryx2-bb-derive-macros"
categories = { workspace = true }
description = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
keywords = { workspace = true }
license = { workspace = true }
readme = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
version = { workspace = true }

[lib]
proc-macro = true

[dependencies]
proc-macro2 = { workspace = true }
quote = { workspace = true }
syn = { workspace = true }
iceoryx2-bb-elementary = { workspace = true }
41 changes: 41 additions & 0 deletions iceoryx2-bb/derive-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Fields};

#[proc_macro_derive(PlacementDefault)]
pub fn placement_default_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = &input.ident;

let place_new_impl = match input.data {
Data::Struct(ref data_struct) => match data_struct.fields {
Fields::Named(ref fields_named) => {
let field_inits = fields_named.named.iter().map(|f| {
let field_type = &f.ty;
let name = &f.ident;
quote! {
#field_type::placement_default(&mut (*ptr).#name);
}
});

quote! {
unsafe fn placement_default(ptr: *mut #name) {
#(#field_inits)*
}
}
}
_ => unimplemented!(),
},
_ => unimplemented!(),
};

let expanded = quote! {
impl iceoryx2_bb_elementary::placement_new::PlacementDefault for #name {
#place_new_impl
}
};

TokenStream::from(expanded)
}
1 change: 1 addition & 0 deletions iceoryx2-bb/trait-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ version = { workspace = true }

[dev-dependencies]
iceoryx2-bb-container = { workspace = true }
iceoryx2-bb-derive-macros = { workspace = true }
iceoryx2-bb-elementary = { workspace = true }
iceoryx2-bb-memory = { workspace = true }
iceoryx2-bb-lock-free = { workspace = true }
Expand Down
23 changes: 23 additions & 0 deletions iceoryx2-bb/trait-tests/tests/placement_new_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[cfg(test)]
mod placement_new {
use std::alloc::{alloc, dealloc, Layout};

use iceoryx2_bb_derive_macros::PlacementDefault;
use iceoryx2_bb_elementary::placement_new::PlacementDefault;

#[derive(Default, PlacementDefault)]
struct TestStruct {
a: i32,
b: u64,
}

#[test]
fn whatever() {
let layout = Layout::new::<TestStruct>();
let memory = unsafe { alloc(layout) } as *mut TestStruct;
let sut = TestStruct::default();
unsafe { TestStruct::placement_default(memory) };

unsafe { dealloc(memory.cast(), layout) };
}
}

0 comments on commit 9c6e398

Please sign in to comment.