Skip to contentSkip to conversation

rust: alloc: cleanup imports and use "kernel vertical" style

PATCH Superseded Danilo Krummrich 3 participants 7 messages May 11 – May 12

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Danilo Krummrich

author maintainer
#0 lore raw

PATCH 1/2 rust: alloc: cleanup imports and use "kernel vertical" style

Change all imports in the alloc module to use the "kernel vertical" import style [1].

While at it, drop unnecessary imports covered by prelude::*.

---
 rust/kernel/alloc/allocator.rs      | 27 ++++++++++----
 rust/kernel/alloc/allocator/iter.rs |  8 +++-
 rust/kernel/alloc/kbox.rs           | 58 ++++++++++++++++++++---------
 rust/kernel/alloc/kvec.rs           | 49 +++++++++++++++++-------
 rust/kernel/alloc/kvec/errors.rs    |  6 ++-
 rust/kernel/alloc/layout.rs         |  5 ++-
 6 files changed, 109 insertions(+), 44 deletions(-)
rust/kernel/alloc/allocator.rs +19 -8
index 63bfb91b3671..af20332d59f7 100644
@@ -8,14 +8,25 @@
 //!
 //! Reference: <https://docs.kernel.org/core-api/memory-allocation.html>
 
-use super::Flags;
-use core::alloc::Layout;
-use core::ptr;
-use core::ptr::NonNull;
-
-use crate::alloc::{AllocError, Allocator, NumaNode};
-use crate::bindings;
-use crate::page;
+use super::{
+    AllocError,
+    Allocator,
+    Flags,
+    NumaNode, //
+};
+
+use crate::{
+    bindings,
+    page, //
+};
+
+use core::{
+    alloc::Layout,
+    ptr::{
+        self,
+        NonNull, //
+    }, //
+};
 
 const ARCH_KMALLOC_MINALIGN: usize = bindings::ARCH_KMALLOC_MINALIGN;
 
rust/kernel/alloc/allocator/iter.rs +6 -2
index e0a70b7a744a..02fda3ea5cae 100644
@@ -1,9 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
 
 use super::Vmalloc;
+
 use crate::page;
-use core::marker::PhantomData;
-use core::ptr::NonNull;
+
+use core::{
+    marker::PhantomData,
+    ptr::NonNull, //
+};
 
 /// An [`Iterator`] of [`page::BorrowedPage`] items owned by a [`Vmalloc`] allocation.
 ///
rust/kernel/alloc/kbox.rs +40 -18

64 lines not on this page · raw message

rust/kernel/alloc/kvec.rs +36 -13

65 lines not on this page · raw message

rust/kernel/alloc/kvec/errors.rs +4 -2
index 985c5f2c3962..aaca6446516a 100644
@@ -2,8 +2,10 @@
 
 //! Errors for the [`Vec`] type.
 
-use kernel::fmt;
-use kernel::prelude::*;
+use crate::{
+    fmt,
+    prelude::*, //
+};
 
 /// Error type for [`Vec::push_within_capacity`].
 pub struct PushError<T>(pub T);
rust/kernel/alloc/layout.rs +4 -1
index 9f8be72feb7a..b629da4aed07 100644
@@ -4,7 +4,10 @@
 //!
 //! Custom layout types extending or improving [`Layout`].
 
-use core::{alloc::Layout, marker::PhantomData};
+use core::{
+    alloc::Layout,
+    marker::PhantomData, //
+};
 
 /// Error when constructing an [`ArrayLayout`].
 pub struct LayoutError;

base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731

Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH 2/2 rust: alloc: cleanup doctest imports to "kernel vertical" style

Change all imports in the alloc module's doctests to use the "kernel vertical" import style [1].

While at it, drop imports that are automatically included in doctests.

---
 rust/kernel/alloc/allocator.rs |  7 +++++--
 rust/kernel/alloc/kbox.rs      | 17 +++++++++++------
 rust/kernel/alloc/kvec.rs      | 10 +++++++---
 rust/kernel/alloc/layout.rs    |  5 ++++-
 4 files changed, 27 insertions(+), 12 deletions(-)
rust/kernel/alloc/allocator.rs +5 -2
index af20332d59f7..562e41925ada 100644
@@ -174,8 +174,11 @@ impl Vmalloc {
     /// # Examples
     ///
     /// ```
-    /// # use core::ptr::{NonNull, from_mut};
-    /// # use kernel::{page, prelude::*};
+    /// # use core::ptr::{
+    /// #     from_mut,
+    /// #     NonNull, //
+    /// # };
+    /// # use kernel::page;
     /// use kernel::alloc::allocator::Vmalloc;
     ///
     /// let mut vbox = VBox::<[u8; page::PAGE_SIZE]>::new_uninit(GFP_KERNEL)?;
rust/kernel/alloc/kbox.rs +11 -6
index 0de45d8d235a..146d1b5e20bb 100644
@@ -296,7 +296,10 @@ pub fn pin(x: T, flags: Flags) -> Result<Pin<Box<T, A>>, AllocError>
     /// # Examples
     ///
     /// ```
-    /// use kernel::sync::{new_spinlock, SpinLock};
+    /// use kernel::sync::{
+    ///     new_spinlock,
+    ///     SpinLock, //
+    /// };
     ///
     /// struct Inner {
     ///     a: u32,
@@ -589,7 +592,6 @@ fn deref_mut(&mut self) -> &mut T {
 ///
 /// ```
 /// # use core::borrow::Borrow;
-/// # use kernel::alloc::KBox;
 /// struct Foo<B: Borrow<u32>>(B);
 ///
 /// // Owned instance.
@@ -617,7 +619,6 @@ fn borrow(&self) -> &T {
 ///
 /// ```
 /// # use core::borrow::BorrowMut;
-/// # use kernel::alloc::KBox;
 /// struct Foo<B: BorrowMut<u32>>(B);
 ///
 /// // Owned instance.
@@ -682,9 +683,13 @@ fn drop(&mut self) {
 /// # Examples
 ///
 /// ```
-/// # use kernel::prelude::*;
-/// use kernel::alloc::allocator::VmallocPageIter;
-/// use kernel::page::{AsPageIter, PAGE_SIZE};
+/// use kernel::{
+///     alloc::allocator::VmallocPageIter,
+///     page::{
+///         AsPageIter,
+///         PAGE_SIZE, //
+///     }, //
+/// };
 ///
 /// let mut vbox = VBox::new((), GFP_KERNEL)?;
 ///
rust/kernel/alloc/kvec.rs +7 -3
index 4340525f2672..932caa740327 100644
@@ -1169,9 +1169,13 @@ fn into_iter(self) -> Self::IntoIter {
 /// # Examples
 ///
 /// ```
-/// # use kernel::prelude::*;
-/// use kernel::alloc::allocator::VmallocPageIter;
-/// use kernel::page::{AsPageIter, PAGE_SIZE};
+/// use kernel::{
+///     alloc::allocator::VmallocPageIter,
+///     page::{
+///         AsPageIter,
+///         PAGE_SIZE, //
+///     }, //
+/// };
 ///
 /// let mut vec = VVec::<u8>::new();
 ///
rust/kernel/alloc/layout.rs +4 -1
index b629da4aed07..62a459c66baf 100644
@@ -50,7 +50,10 @@ pub const fn empty() -> Self {
     /// # Examples
     ///
     /// ```
-    /// # use kernel::alloc::layout::{ArrayLayout, LayoutError};
+    /// # use kernel::alloc::layout::{
+    /// #     ArrayLayout,
+    /// #     LayoutError, //
+    /// # };
     /// let layout = ArrayLayout::<i32>::new(15)?;
     /// assert_eq!(layout.len(), 15);
     ///
Signature · 1 line
2.54.0
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] rust: alloc: cleanup doctest imports to "kernel vertical" style

Quoted 8 lines · Danilo Krummrich

Change all imports in the alloc module's doctests to use the "kernel vertical" import style [1].

While at it, drop imports that are automatically included in doctests.

---

reviewer
#3 lore raw

Re: [PATCH 2/2] rust: alloc: cleanup doctest imports to "kernel vertical" style

In reply to Danilo Krummrich
Quoted 19 lines · Danilo Krummrich

Change all imports in the alloc module's doctests to use the "kernel vertical" import style [1].

While at it, drop imports that are automatically included in doctests.

---
 rust/kernel/alloc/allocator.rs |  7 +++++--
 rust/kernel/alloc/kbox.rs      | 17 +++++++++++------
 rust/kernel/alloc/kvec.rs      | 10 +++++++---
 rust/kernel/alloc/layout.rs    |  5 ++++-
 4 files changed, 27 insertions(+), 12 deletions(-)
rust/kernel/alloc/allocator.rs +0 -0
index af20332d59f7..562e41925ada 100644

rust/kernel/alloc/allocator.rs impl Vmalloc { · line 180

Quoted 9 lines
rust/kernel/alloc/allocator.rs +4 -2
@@ -174,8 +174,11 @@ impl Vmalloc {
     /// # Examples
     ///
     /// ```
-    /// # use core::ptr::{NonNull, from_mut};
-    /// # use kernel::{page, prelude::*};
+    /// # use core::ptr::{
+    /// #     from_mut,
+    /// #     NonNull, //
+    /// # };

I thought the consensus was that hidden lines don't need to use vertical import style. They're not prone to merge conflict, and it can be unnecessarily sparse especially if there're a lot of imports. Did I misremember it?

Best,
Gary

author maintainer
#4 lore raw

Re: [PATCH 2/2] rust: alloc: cleanup doctest imports to "kernel vertical" style

Quoted 14 lines · Gary Guo

rust/kernel/alloc/allocator.rs impl Vmalloc { · line 180

Quoted 10 lines · Danilo Krummrich
rust/kernel/alloc/allocator.rs +4 -2
@@ -174,8 +174,11 @@ impl Vmalloc {
     /// # Examples
     ///
     /// ```
-    /// # use core::ptr::{NonNull, from_mut};
-    /// # use kernel::{page, prelude::*};
+    /// # use core::ptr::{
+    /// #     from_mut,
+    /// #     NonNull, //
+    /// # };

I thought the consensus was that hidden lines don't need to use vertical import style.

Don't need to, but still can; I generally prefer to keep it consistent.

We can drop the - for doc-comments - unnecessary '//' though, which my muscle memory inserted regardless. :)

Reviewed-by on v1 1/2
#5 lore raw
In reply to Danilo Krummrich
Quoted 8 lines · Danilo Krummrich

Change all imports in the alloc module to use the "kernel vertical" import style [1].

While at it, drop unnecessary imports covered by prelude::*.

---

There are some non-vertical style imports in rust/kernel/alloc.rs, should those be changed too?

Quoted 26 lines
rust/kernel/alloc/kbox.rs +2 -18
@@ -3,24 +3,46 @@
 //! Implementation of [`Box`].
 
 #[allow(unused_imports)] // Used in doc comments.
-use super::allocator::{KVmalloc, Kmalloc, Vmalloc, VmallocPageIter};
-use super::{AllocError, Allocator, Flags, NumaNode};
-use core::alloc::Layout;
-use core::borrow::{Borrow, BorrowMut};
-use core::marker::PhantomData;
-use core::mem::ManuallyDrop;
-use core::mem::MaybeUninit;
-use core::ops::{Deref, DerefMut};
-use core::pin::Pin;
-use core::ptr::NonNull;
-use core::result::Result;
-
-use crate::ffi::c_void;
-use crate::fmt;
-use crate::init::InPlaceInit;
-use crate::page::AsPageIter;
-use crate::types::ForeignOwnable;
-use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
+use super::{
+    allocator::{

rust/kernel/alloc/kbox.rs line 17

Quoted 10 lines
rust/kernel/alloc/kbox.rs +10 -0
+        KVmalloc,
+        Kmalloc,
+        Vmalloc,
+        VmallocPageIter, //
+    },
+    AllocError,
+    Allocator,
+    Flags,
+    NumaNode, //
+};

This changes the scope of allow(unused_imports). Is this intentional?

Both nits so:

author maintainer
#6 lore raw
Quoted 4 lines · Eliot Courtney
Quoted 3 lines · Danilo Krummrich

Change all imports in the alloc module to use the "kernel vertical" import style [1].

Quoted 8 lines
Quoted 5 lines

While at it, drop unnecessary imports covered by prelude::*.

---

There are some non-vertical style imports in rust/kernel/alloc.rs, should those be changed too?

Yes, thanks for catching this, and the one below.

Quoted 42 lines
Quoted 26 lines
rust/kernel/alloc/kbox.rs +2 -18
@@ -3,24 +3,46 @@
 //! Implementation of [`Box`].
 
 #[allow(unused_imports)] // Used in doc comments.
-use super::allocator::{KVmalloc, Kmalloc, Vmalloc, VmallocPageIter};
-use super::{AllocError, Allocator, Flags, NumaNode};
-use core::alloc::Layout;
-use core::borrow::{Borrow, BorrowMut};
-use core::marker::PhantomData;
-use core::mem::ManuallyDrop;
-use core::mem::MaybeUninit;
-use core::ops::{Deref, DerefMut};
-use core::pin::Pin;
-use core::ptr::NonNull;
-use core::result::Result;
-
-use crate::ffi::c_void;
-use crate::fmt;
-use crate::init::InPlaceInit;
-use crate::page::AsPageIter;
-use crate::types::ForeignOwnable;
-use pin_init::{InPlaceWrite, Init, PinInit, ZeroableOption};
+use super::{
+    allocator::{

rust/kernel/alloc/kbox.rs line 17

Quoted 10 lines
rust/kernel/alloc/kbox.rs +10 -0
+        KVmalloc,
+        Kmalloc,
+        Vmalloc,
+        VmallocPageIter, //
+    },
+    AllocError,
+    Allocator,
+    Flags,
+    NumaNode, //
+};

This changes the scope of allow(unused_imports). Is this intentional?

Both nits so: