linux-next/rust/kernel/seq_file.rs
Miguel Ojeda 27c7518e7f rust: finish using custom FFI integer types
In the last kernel cycle we migrated most of the `core::ffi` cases in
commit d072acda48 ("rust: use custom FFI integer types"):

    Currently FFI integer types are defined in libcore. This commit
    creates the `ffi` crate and asks bindgen to use that crate for FFI
    integer types instead of `core::ffi`.

    This commit is preparatory and no type changes are made in this
    commit yet.

Finish now the few remaining/new cases so that we perform the actual
remapping in the next commit as planned.

Acked-by: Jocelyn Falempe <jfalempe@redhat.com> # drm
Link: https://lore.kernel.org/rust-for-linux/CANiq72m_rg42SvZK=bF2f0yEoBLVA33UBhiAsv8THhVu=G2dPA@mail.gmail.com/
Link: https://lore.kernel.org/all/cc9253fa-9d5f-460b-9841-94948fb6580c@redhat.com/
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2024-12-16 21:48:45 +01:00

53 lines
1.7 KiB
Rust

// SPDX-License-Identifier: GPL-2.0
//! Seq file bindings.
//!
//! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h)
use crate::{bindings, c_str, types::NotThreadSafe, types::Opaque};
/// A utility for generating the contents of a seq file.
#[repr(transparent)]
pub struct SeqFile {
inner: Opaque<bindings::seq_file>,
_not_send: NotThreadSafe,
}
impl SeqFile {
/// Creates a new [`SeqFile`] from a raw pointer.
///
/// # Safety
///
/// The caller must ensure that for the duration of 'a the following is satisfied:
/// * The pointer points at a valid `struct seq_file`.
/// * The `struct seq_file` is not accessed from any other thread.
pub unsafe fn from_raw<'a>(ptr: *mut bindings::seq_file) -> &'a SeqFile {
// SAFETY: The caller ensures that the reference is valid for 'a. There's no way to trigger
// a data race by using the `&SeqFile` since this is the only thread accessing the seq_file.
//
// CAST: The layout of `struct seq_file` and `SeqFile` is compatible.
unsafe { &*ptr.cast() }
}
/// Used by the [`seq_print`] macro.
pub fn call_printf(&self, args: core::fmt::Arguments<'_>) {
// SAFETY: Passing a void pointer to `Arguments` is valid for `%pA`.
unsafe {
bindings::seq_printf(
self.inner.get(),
c_str!("%pA").as_char_ptr(),
&args as *const _ as *const crate::ffi::c_void,
);
}
}
}
/// Write to a [`SeqFile`] with the ordinary Rust formatting syntax.
#[macro_export]
macro_rules! seq_print {
($m:expr, $($arg:tt)+) => (
$m.call_printf(format_args!($($arg)+))
);
}
pub use seq_print;