iceoryx2_bb_elementary_traits/zero_copy_send.rs
1// Copyright (c) 2025 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/// Marks types that can be sent to another process in a zero-copy manner, i.e. the types can be
14/// safely used from within different process address spaces and can be uniquely identified by their
15/// [`ZeroCopySend::type_name()`] in an inter-process communication context.
16///
17/// # Safety
18///
19/// The user must ensure that
20/// * the types are self-contained, meaning they shall not contain pointers, references, indices
21/// or handles to resources that are not part of the type.
22/// Examples:
23/// * file descriptors point to resources that can be different in another process
24/// * a list with pointers into the heap
25/// * the types do not have references or pointer members; they shall not use pointers to manage
26/// their internal structure.
27/// Example:
28/// * a list must be implemented using indices to structure itself
29/// * the types must have a uniform memory representation, meaning they are annotated with
30/// `#[repr(C)]`.
31///
32pub unsafe trait ZeroCopySend {
33 /// The unique identifier of the type. It shall be used to identify a specific type across
34 /// processes and languages.
35 ///
36 /// # Safety
37 ///
38 /// * The user must guarantee that all types, also definitions in different languages, have
39 /// the same memory layout.
40 unsafe fn type_name() -> &'static str {
41 core::any::type_name::<Self>()
42 }
43
44 #[doc(hidden)]
45 /// used as dummy call in the derive macro to ensure at compile-time that all fields of
46 /// a struct implement ZeroCopySend
47 fn __is_zero_copy_send(&self) {}
48}
49
50unsafe impl ZeroCopySend for usize {}
51unsafe impl ZeroCopySend for u8 {}
52unsafe impl ZeroCopySend for u16 {}
53unsafe impl ZeroCopySend for u32 {}
54unsafe impl ZeroCopySend for u64 {}
55unsafe impl ZeroCopySend for u128 {}
56
57unsafe impl ZeroCopySend for isize {}
58unsafe impl ZeroCopySend for i8 {}
59unsafe impl ZeroCopySend for i16 {}
60unsafe impl ZeroCopySend for i32 {}
61unsafe impl ZeroCopySend for i64 {}
62unsafe impl ZeroCopySend for i128 {}
63
64unsafe impl ZeroCopySend for f32 {}
65unsafe impl ZeroCopySend for f64 {}
66
67unsafe impl ZeroCopySend for char {}
68unsafe impl ZeroCopySend for bool {}
69
70unsafe impl ZeroCopySend for () {}
71
72unsafe impl<T: ZeroCopySend> ZeroCopySend for [T] {}
73unsafe impl<T: ZeroCopySend, const N: usize> ZeroCopySend for [T; N] {}
74unsafe impl<T: ZeroCopySend> ZeroCopySend for core::mem::MaybeUninit<T> {}
75
76// Note: `ZeroCopySend` cannot be implemented for tuples because `#[repr(C)]` can only be applied
77// to structs, enums, and unions.