forked from eclipse-iceoryx/iceoryx2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eclipse-iceoryx#224] Add PlacementNew derive macro
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) }; | ||
} | ||
} |