Skip to main content

Module list

Module list 

Source
Expand description

Rust interface for C doubly circular intrusive linked lists.

This module provides Rust abstractions for iterating over C list_head-based linked lists. It should only be used for cases where C and Rust code share direct access to the same linked list through a C interop interface.

Note: This must not be used by Rust components that just need a linked list primitive. Use kernel::list::List instead.

§Examples

use kernel::{
    bindings,
    interop::list::clist_create,
    types::Opaque,
};

/// Rust wrapper for the C struct.
///
/// The list item struct in this example is defined in C code as:
///
/// ```c
/// struct SampleItemC {
///     int value;
///     struct list_head link;
/// };
/// ```
#[repr(transparent)]
pub struct Item(Opaque<SampleItemC>);

impl Item {
    pub fn value(&self) -> i32 {
        // SAFETY: `Item` has the same layout as `SampleItemC`.
        unsafe { (*self.0.get()).value }
    }
}

// Create typed [`CList`] from sentinel head.
// SAFETY: `head` is valid and initialized, items are `SampleItemC` with
// embedded `link` field, and `Item` is `#[repr(transparent)]` over `SampleItemC`.
let list = unsafe { clist_create!(head, Item, SampleItemC, link) };

// Iterate directly over typed items.
let mut found_0 = false;
let mut found_10 = false;
let mut found_20 = false;

for item in list.iter() {
    let val = item.value();
    if val == 0 { found_0 = true; }
    if val == 10 { found_10 = true; }
    if val == 20 { found_20 = true; }
}

assert!(found_0 && found_10 && found_20);

Macros§

clist_create
Create a C doubly-circular linked list interface CList from a raw list_head pointer.

Structs§

CList
A typed C linked list with a sentinel head intended for FFI use-cases where a C subsystem manages a linked list that Rust code needs to read. Generally required only for special cases.
CListHead
FFI wrapper for a C list_head object used in intrusive linked lists.
CListIter
High-level iterator over typed list items.