Skip to content

Commit a3307ad

Browse files
committed
[#224] Document derive macro; add release notes
1 parent 648f91d commit a3307ad

File tree

7 files changed

+75
-4
lines changed

7 files changed

+75
-4
lines changed

doc/release-notes/iceoryx2-unreleased.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
* Introduce `IoxAtomic` that supports up to 128bit atomics on 32-bit architecture with a ReadWriteLock
4545
* add CI targets to officially support 32-bit
4646
* Example that demonstrates publish-subscribe communication with dynamic data [#205](https://github.com/eclipse-iceoryx/iceoryx2/issues/205)
47-
47+
* Introduce `PlacementNew` trait and derive proc-macro [#224](https://github.com/eclipse-iceoryx/iceoryx2/issues/224)
4848

4949
### Bugfixes
5050

iceoryx2-bb/derive-macros/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "iceoryx2-bb-derive-macros"
3+
description = "iceoryx2: [internal] helper derive proc-macros"
34
categories = { workspace = true }
4-
description = { workspace = true }
55
edition = { workspace = true }
66
homepage = { workspace = true }
77
keywords = { workspace = true }

iceoryx2-bb/derive-macros/src/lib.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
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+
//! Contains helper derive macros for iceoryx2.
14+
115
extern crate proc_macro;
216

317
use proc_macro::TokenStream;
418
use quote::quote;
519
use syn::{parse_macro_input, Data, DeriveInput, Fields};
620

21+
/// Implements the [`iceoryx2_bb_elementary::placement_default::PlacementDefault`] trait when all
22+
/// fields of the struct implement it.
23+
///
24+
/// ```
25+
/// use iceoryx2_bb_derive_macros::PlacementDefault;
26+
/// use iceoryx2_bb_elementary::placement_default::PlacementDefault;
27+
/// use std::alloc::{alloc, dealloc, Layout};
28+
///
29+
/// #[derive(PlacementDefault)]
30+
/// struct MyLargeType {
31+
/// value_1: u64,
32+
/// value_2: Option<usize>,
33+
/// value_3: [u8; 10485760],
34+
/// }
35+
///
36+
/// let layout = Layout::new::<MyLargeType>();
37+
/// let raw_memory = unsafe { alloc(layout) } as *mut MyLargeType;
38+
/// unsafe { MyLargeType::placement_default(raw_memory) };
39+
///
40+
/// unsafe { &mut *raw_memory }.value_3[123] = 31;
41+
///
42+
/// unsafe { core::ptr::drop_in_place(raw_memory) };
43+
/// unsafe { dealloc(raw_memory.cast(), layout) };
44+
45+
/// ```
746
#[proc_macro_derive(PlacementDefault)]
847
pub fn placement_default_derive(input: TokenStream) -> TokenStream {
948
let input = parse_macro_input!(input as DeriveInput);
@@ -17,7 +56,7 @@ pub fn placement_default_derive(input: TokenStream) -> TokenStream {
1756
let name = &f.ident;
1857
quote! {
1958
let field_address = core::ptr::addr_of_mut!((*ptr).#name);
20-
PlacementDefault::placement_default(field_address);
59+
iceoryx2_bb_elementary::placement_default::PlacementDefault::placement_default(field_address);
2160
}
2261
});
2362

@@ -32,7 +71,7 @@ pub fn placement_default_derive(input: TokenStream) -> TokenStream {
3271
let index = syn::Index::from(i);
3372
quote! {
3473
let field_address = core::ptr::addr_of_mut!((*ptr).#index);
35-
PlacementDefault::placement_default(field_address);
74+
iceoryx2_bb_elementary::placement_default::PlacementDefault::placement_default(field_address);
3675
}
3776
});
3877

iceoryx2-bb/elementary/src/placement_default.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
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+
//! Trait to perform placement new construction on a given pointer via [`Default::default()`].
14+
//! See [`PlacementDefault`] for example.
15+
116
use iceoryx2_pal_concurrency_sync::iox_atomic::*;
217

318
/// A trait that allows types to perform a placement new based on their

iceoryx2-bb/elementary/src/pointer_trait.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
//
1111
// SPDX-License-Identifier: Apache-2.0 OR MIT
1212

13+
//! Trait which describes a form of pointer. Required to distinguish normal pointers from
14+
//! relocatable pointers.
15+
1316
/// Trait which describes a form of pointer. Required to distinguish normal pointers from
1417
/// relocatable pointers.
1518
pub trait PointerTrait<T> {

iceoryx2-bb/elementary/src/relocatable_container.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
// SPDX-License-Identifier: Apache-2.0 OR MIT
1212

13+
//! Describes a container which can shared between processes.
14+
1315
use crate::{allocator::AllocationError, allocator::BaseAllocator};
1416

1517
/// Describes a container which can shared between processes. Since the shared memory is often

iceoryx2-bb/trait-tests/tests/placement_new_tests.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
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+
113
#[cfg(test)]
214
mod placement_new {
315
use std::{

0 commit comments

Comments
 (0)