Skip to content

Commit a8cb471

Browse files
ojedafbq
authored andcommitted
rust: init: make doctests compilable/testable
Rust documentation tests are going to be build/run-tested with the KUnit integration added in a future patch, thus update them to make them compilable/testable so that we may start enforcing it. Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com> Reviewed-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com> Reviewed-by: David Gow <davidgow@google.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Björn Roy Baron <bjorn3_gh@protonmail.com> Signed-off-by: Miguel Ojeda <ojeda@kernel.org> Link: https://lore.kernel.org/r/20230718052752.1045248-3-ojeda@kernel.org
1 parent f3c800f commit a8cb471

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

rust/kernel/init.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,24 @@
120120
//! `slot` gets called.
121121
//!
122122
//! ```rust
123-
//! use kernel::{prelude::*, init};
123+
//! # #![allow(unreachable_pub, clippy::disallowed_names)]
124+
//! use kernel::{prelude::*, init, types::Opaque};
124125
//! use core::{ptr::addr_of_mut, marker::PhantomPinned, pin::Pin};
125126
//! # mod bindings {
127+
//! # #![allow(non_camel_case_types)]
126128
//! # pub struct foo;
127129
//! # pub unsafe fn init_foo(_ptr: *mut foo) {}
128130
//! # pub unsafe fn destroy_foo(_ptr: *mut foo) {}
129131
//! # pub unsafe fn enable_foo(_ptr: *mut foo, _flags: u32) -> i32 { 0 }
130132
//! # }
133+
//! # // `Error::from_errno` is `pub(crate)` in the `kernel` crate, thus provide a workaround.
134+
//! # trait FromErrno {
135+
//! # fn from_errno(errno: core::ffi::c_int) -> Error {
136+
//! # // Dummy error that can be constructed outside the `kernel` crate.
137+
//! # Error::from(core::fmt::Error)
138+
//! # }
139+
//! # }
140+
//! # impl FromErrno for Error {}
131141
//! /// # Invariants
132142
//! ///
133143
//! /// `foo` is always initialized
@@ -158,7 +168,7 @@
158168
//! if err != 0 {
159169
//! // Enabling has failed, first clean up the foo and then return the error.
160170
//! bindings::destroy_foo(Opaque::raw_get(foo));
161-
//! return Err(Error::from_kernel_errno(err));
171+
//! return Err(Error::from_errno(err));
162172
//! }
163173
//!
164174
//! // All fields of `RawFoo` have been initialized, since `_p` is a ZST.
@@ -227,8 +237,7 @@ pub mod macros;
227237
///
228238
/// ```rust
229239
/// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)]
230-
/// # use kernel::{init, pin_init, stack_pin_init, init::*, sync::Mutex, new_mutex};
231-
/// # use macros::pin_data;
240+
/// # use kernel::{init, macros::pin_data, pin_init, stack_pin_init, init::*, sync::Mutex, new_mutex};
232241
/// # use core::pin::Pin;
233242
/// #[pin_data]
234243
/// struct Foo {
@@ -278,7 +287,7 @@ macro_rules! stack_pin_init {
278287
///
279288
/// # Examples
280289
///
281-
/// ```rust
290+
/// ```rust,ignore
282291
/// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)]
283292
/// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex};
284293
/// # use macros::pin_data;
@@ -304,7 +313,7 @@ macro_rules! stack_pin_init {
304313
/// pr_info!("a: {}", &*foo.a.lock());
305314
/// ```
306315
///
307-
/// ```rust
316+
/// ```rust,ignore
308317
/// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)]
309318
/// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex};
310319
/// # use macros::pin_data;
@@ -518,7 +527,7 @@ macro_rules! stack_try_pin_init {
518527
///
519528
/// ```rust
520529
/// # use kernel::pin_init;
521-
/// # use macros::{Zeroable, pin_data};
530+
/// # use kernel::macros::{Zeroable, pin_data};
522531
/// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
523532
/// #[pin_data]
524533
/// #[derive(Zeroable)]
@@ -684,7 +693,7 @@ macro_rules! init {
684693
/// # Examples
685694
///
686695
/// ```rust
687-
/// use kernel::{init::PinInit, error::Error, InPlaceInit};
696+
/// use kernel::{init::{PinInit, zeroed}, error::Error};
688697
/// struct BigBuf {
689698
/// big: Box<[u8; 1024 * 1024 * 1024]>,
690699
/// small: [u8; 1024 * 1024],

0 commit comments

Comments
 (0)