iceoryx2_bb_elementary_traits/testing/abandonable.rs
1// Copyright (c) 2026 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
13use crate::non_null::NonNullCompat;
14use core::ptr::NonNull;
15
16/// **Only for testing purposes!**
17///
18/// Marks types that can abandon resources required for cleanup tests.
19/// The system resource is abandon but the process local internal constructs
20/// are still cleaned up properly. Those internal constructs could be:
21///
22/// * file descriptors
23/// * memory mappings
24/// * ...
25pub trait Abandonable: Sized {
26 fn abandon(mut self) {
27 unsafe { Self::abandon_in_place(NonNull::iox2_from_mut(&mut self)) };
28 core::mem::forget(self);
29 }
30
31 /// Abandon a resource in place. Shall be used when a struct of multiple resources
32 /// shall be abandoned and the resources cannot be moved out of the struct.
33 ///
34 /// # Safety
35 ///
36 /// * `this` must pointing to a valid, constructed `Self`.
37 /// * `this` must be non-null and properly aligned
38 /// * `this` must not be dropped before.
39 /// * `this` cannot be used after this operation, you should most likely call
40 /// [`core::mem::forget()`] afterwards.
41 ///
42 unsafe fn abandon_in_place(this: NonNull<Self>);
43}