Skip to content

Commit a30e94c

Browse files
ojedashuahkh
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 <[email protected]> Reviewed-by: Vincenzo Palazzo <[email protected]> Reviewed-by: David Gow <[email protected]> Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Björn Roy Baron <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent df3cb7a commit a30e94c

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

rust/kernel/init.rs

Lines changed: 17 additions & 9 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.
@@ -226,8 +236,7 @@ pub mod macros;
226236
///
227237
/// ```rust
228238
/// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)]
229-
/// # use kernel::{init, pin_init, stack_pin_init, init::*, sync::Mutex, new_mutex};
230-
/// # use macros::pin_data;
239+
/// # use kernel::{init, macros::pin_data, pin_init, stack_pin_init, init::*, sync::Mutex, new_mutex};
231240
/// # use core::pin::Pin;
232241
/// #[pin_data]
233242
/// struct Foo {
@@ -277,7 +286,7 @@ macro_rules! stack_pin_init {
277286
///
278287
/// # Examples
279288
///
280-
/// ```rust
289+
/// ```rust,ignore
281290
/// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)]
282291
/// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex};
283292
/// # use macros::pin_data;
@@ -303,7 +312,7 @@ macro_rules! stack_pin_init {
303312
/// pr_info!("a: {}", &*foo.a.lock());
304313
/// ```
305314
///
306-
/// ```rust
315+
/// ```rust,ignore
307316
/// # #![allow(clippy::disallowed_names, clippy::new_ret_no_self)]
308317
/// # use kernel::{init, pin_init, stack_try_pin_init, init::*, sync::Mutex, new_mutex};
309318
/// # use macros::pin_data;
@@ -513,8 +522,7 @@ macro_rules! stack_try_pin_init {
513522
/// For instance:
514523
///
515524
/// ```rust
516-
/// # use kernel::pin_init;
517-
/// # use macros::pin_data;
525+
/// # use kernel::{macros::pin_data, pin_init};
518526
/// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
519527
/// #[pin_data]
520528
/// struct Buf {
@@ -841,7 +849,7 @@ macro_rules! init {
841849
/// # Examples
842850
///
843851
/// ```rust
844-
/// use kernel::{init::PinInit, error::Error, InPlaceInit};
852+
/// use kernel::{init::{PinInit, zeroed}, error::Error};
845853
/// struct BigBuf {
846854
/// big: Box<[u8; 1024 * 1024 * 1024]>,
847855
/// small: [u8; 1024 * 1024],

0 commit comments

Comments
 (0)