fuchsia_rcu_traits/lib.rs
1// Copyright 2026 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Foundational trait definitions for RCU-safe reclamation.
6//!
7//! This crate defines the [`RcuDroppable`] marker trait and the `#[derive(RcuDroppable)]`
8//! proc-macro re-export.
9//!
10//! It is separate from `fuchsia-rcu` so that foundational crates can implement `RcuDroppable` for
11//! their types without depending on the RCU runtime.
12
13#![no_std]
14
15pub use fuchsia_rcu_derive::RcuDroppable;
16
17/// Marker trait for types that are safe to be dropped on the RCU worker / drop execution path.
18///
19/// # Safety
20/// Types implementing this trait MUST NOT have `Drop` side-effects that block or be time or
21/// thread-sensitive.
22pub unsafe trait RcuDroppable: Send + 'static {}
23
24macro_rules! impl_rcu_droppable {
25 ($($t:ty),* $(,)?) => {
26 $(
27 // SAFETY: Built in numeric types, atomics, strings, and standard types without
28 // custom blocking Drop logic do not block or produce thread-sensitive drop side effects.
29 unsafe impl RcuDroppable for $t {}
30 )*
31 };
32}
33
34impl_rcu_droppable!(
35 (),
36 bool,
37 char,
38 u8,
39 u16,
40 u32,
41 u64,
42 u128,
43 usize,
44 i8,
45 i16,
46 i32,
47 i64,
48 i128,
49 isize,
50 f32,
51 f64,
52 str,
53);
54
55// SAFETY: `lock_api::Mutex<R, T>` has no custom Drop logic; dropping it only drops the raw
56// lock `R` and the inner value `T`, both of which are `RcuDroppable`.
57unsafe impl<R: RcuDroppable + lock_api::RawMutex, T: ?Sized + RcuDroppable> RcuDroppable
58 for lock_api::Mutex<R, T>
59{
60}
61
62// SAFETY: `lock_api::RwLock<R, T>` has no custom Drop logic; dropping it only drops the raw
63// lock `R` and the inner value `T`, both of which are `RcuDroppable`.
64unsafe impl<R: RcuDroppable + lock_api::RawRwLock, T: ?Sized + RcuDroppable> RcuDroppable
65 for lock_api::RwLock<R, T>
66{
67}
68
69#[cfg(test)]
70mod tests {
71 use crate::RcuDroppable;
72
73 fn assert_rcu_droppable<T: RcuDroppable>() {}
74
75 #[derive(RcuDroppable)]
76 struct UnitStruct;
77
78 #[test]
79 fn test_rcu_droppable_trait() {
80 assert_rcu_droppable::<UnitStruct>();
81 }
82}