Skip to content

Commit 5d840b4

Browse files
tamirdojeda
authored andcommitted
rust: list: add impl_list_item! examples
There's a comprehensive example in `rust/kernel/list.rs` but it doesn't exercise the `using ListLinksSelfPtr` variant nor the generic cases. Add that here. Generalize `impl_has_list_links_self_ptr` to handle nested fields in the same manner as `impl_has_list_links`. Tested-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Signed-off-by: Tamir Duberstein <tamird@gmail.com> Link: https://lore.kernel.org/r/20250709-list-no-offset-v4-5-a429e75840a9@gmail.com [ Fixed Rust < 1.82 build by enabling the `offset_of_nested` feature. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 6a13057 commit 5d840b4

2 files changed

Lines changed: 96 additions & 5 deletions

File tree

rust/kernel/list/impl_list_item_mod.rs

Lines changed: 93 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,20 @@ macro_rules! impl_has_list_links_self_ptr {
8282
($(impl$({$($generics:tt)*})?
8383
HasSelfPtr<$item_type:ty $(, $id:tt)?>
8484
for $self:ty
85-
{ self.$field:ident }
85+
{ self$(.$field:ident)* }
8686
)*) => {$(
8787
// SAFETY: The implementation of `raw_get_list_links` only compiles if the field has the
8888
// right type.
8989
unsafe impl$(<$($generics)*>)? $crate::list::HasSelfPtr<$item_type $(, $id)?> for $self {}
9090

9191
unsafe impl$(<$($generics)*>)? $crate::list::HasListLinks$(<$id>)? for $self {
92-
const OFFSET: usize = ::core::mem::offset_of!(Self, $field) as usize;
92+
const OFFSET: usize = ::core::mem::offset_of!(Self, $($field).*) as usize;
9393

9494
#[inline]
9595
unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut $crate::list::ListLinks$(<$id>)? {
9696
// SAFETY: The caller promises that the pointer is not dangling.
9797
let ptr: *mut $crate::list::ListLinksSelfPtr<$item_type $(, $id)?> =
98-
unsafe { ::core::ptr::addr_of_mut!((*ptr).$field) };
98+
unsafe { ::core::ptr::addr_of_mut!((*ptr)$(.$field)*) };
9999
ptr.cast()
100100
}
101101
}
@@ -109,6 +109,96 @@ pub use impl_has_list_links_self_ptr;
109109
/// implement that trait.
110110
///
111111
/// [`ListItem`]: crate::list::ListItem
112+
///
113+
/// # Examples
114+
///
115+
/// ```
116+
/// #[pin_data]
117+
/// struct SimpleListItem {
118+
/// value: u32,
119+
/// #[pin]
120+
/// links: kernel::list::ListLinks,
121+
/// }
122+
///
123+
/// kernel::list::impl_has_list_links! {
124+
/// impl HasListLinks<0> for SimpleListItem { self.links }
125+
/// }
126+
///
127+
/// kernel::list::impl_list_arc_safe! {
128+
/// impl ListArcSafe<0> for SimpleListItem { untracked; }
129+
/// }
130+
///
131+
/// kernel::list::impl_list_item! {
132+
/// impl ListItem<0> for SimpleListItem { using ListLinks; }
133+
/// }
134+
///
135+
/// struct ListLinksHolder {
136+
/// inner: kernel::list::ListLinks,
137+
/// }
138+
///
139+
/// #[pin_data]
140+
/// struct ComplexListItem<T, U> {
141+
/// value: Result<T, U>,
142+
/// #[pin]
143+
/// links: ListLinksHolder,
144+
/// }
145+
///
146+
/// kernel::list::impl_has_list_links! {
147+
/// impl{T, U} HasListLinks<0> for ComplexListItem<T, U> { self.links.inner }
148+
/// }
149+
///
150+
/// kernel::list::impl_list_arc_safe! {
151+
/// impl{T, U} ListArcSafe<0> for ComplexListItem<T, U> { untracked; }
152+
/// }
153+
///
154+
/// kernel::list::impl_list_item! {
155+
/// impl{T, U} ListItem<0> for ComplexListItem<T, U> { using ListLinks; }
156+
/// }
157+
/// ```
158+
///
159+
/// ```
160+
/// #[pin_data]
161+
/// struct SimpleListItem {
162+
/// value: u32,
163+
/// #[pin]
164+
/// links: kernel::list::ListLinksSelfPtr<SimpleListItem>,
165+
/// }
166+
///
167+
/// kernel::list::impl_list_arc_safe! {
168+
/// impl ListArcSafe<0> for SimpleListItem { untracked; }
169+
/// }
170+
///
171+
/// kernel::list::impl_has_list_links_self_ptr! {
172+
/// impl HasSelfPtr<SimpleListItem> for SimpleListItem { self.links }
173+
/// }
174+
///
175+
/// kernel::list::impl_list_item! {
176+
/// impl ListItem<0> for SimpleListItem { using ListLinksSelfPtr; }
177+
/// }
178+
///
179+
/// struct ListLinksSelfPtrHolder<T, U> {
180+
/// inner: kernel::list::ListLinksSelfPtr<ComplexListItem<T, U>>,
181+
/// }
182+
///
183+
/// #[pin_data]
184+
/// struct ComplexListItem<T, U> {
185+
/// value: Result<T, U>,
186+
/// #[pin]
187+
/// links: ListLinksSelfPtrHolder<T, U>,
188+
/// }
189+
///
190+
/// kernel::list::impl_list_arc_safe! {
191+
/// impl{T, U} ListArcSafe<0> for ComplexListItem<T, U> { untracked; }
192+
/// }
193+
///
194+
/// kernel::list::impl_has_list_links_self_ptr! {
195+
/// impl{T, U} HasSelfPtr<ComplexListItem<T, U>> for ComplexListItem<T, U> { self.links.inner }
196+
/// }
197+
///
198+
/// kernel::list::impl_list_item! {
199+
/// impl{T, U} ListItem<0> for ComplexListItem<T, U> { using ListLinksSelfPtr; }
200+
/// }
201+
/// ```
112202
#[macro_export]
113203
macro_rules! impl_list_item {
114204
(

scripts/Makefile.build

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,14 @@ $(obj)/%.lst: $(obj)/%.c FORCE
309309
# The features in this list are the ones allowed for non-`rust/` code.
310310
#
311311
# - Stable since Rust 1.81.0: `feature(lint_reasons)`.
312-
# - Stable since Rust 1.82.0: `feature(asm_const)`, `feature(raw_ref_op)`.
312+
# - Stable since Rust 1.82.0: `feature(asm_const)`,
313+
# `feature(offset_of_nested)`, `feature(raw_ref_op)`.
313314
# - Stable since Rust 1.87.0: `feature(asm_goto)`.
314315
# - Expected to become stable: `feature(arbitrary_self_types)`.
315316
#
316317
# Please see https://github.com/Rust-for-Linux/linux/issues/2 for details on
317318
# the unstable features in use.
318-
rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,raw_ref_op
319+
rust_allowed_features := asm_const,asm_goto,arbitrary_self_types,lint_reasons,offset_of_nested,raw_ref_op
319320

320321
# `--out-dir` is required to avoid temporaries being created by `rustc` in the
321322
# current working directory, which may be not accessible in the out-of-tree

0 commit comments

Comments
 (0)