foyer_intrusive/lib.rs
1// Copyright 2024 foyer Project Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![expect(clippy::new_without_default)]
16#![warn(missing_docs)]
17#![warn(clippy::allow_attributes)]
18
19//! Intrusive data structures and utils for foyer.
20
21/// Unsafe macro to get a raw pointer to an outer object from a pointer to one
22/// of its fields.
23///
24/// # Examples
25///
26/// ```
27/// use foyer_intrusive::container_of;
28///
29/// struct S { x: u32, y: u32 };
30/// let mut container = S { x: 1, y: 2 };
31/// let field = &mut container.x;
32/// let container2: *mut S = unsafe { container_of!(field, S, x) };
33/// assert_eq!(&mut container as *mut S, container2);
34/// ```
35///
36/// # Safety
37///
38/// This is unsafe because it assumes that the given expression is a valid
39/// pointer to the specified field of some container type.
40#[macro_export]
41macro_rules! container_of {
42 ($ptr:expr, $container:path, $field:ident) => {
43 ($ptr as *mut _ as *const u8).sub(std::mem::offset_of!($container, $field)) as *mut $container
44 };
45}
46
47pub mod adapter;
48pub mod dlist;