rust: list: Use "List::is_empty()" to perform checking when possible
PATCH 1/1 rust: list: Use "List::is_empty()" to perform checking when possible
"List::is_empty()" provides a straight forward convention to check whether a given "List" is empty or not. There're numerous places in the current implementation still use "self.first.is_null()" to perform the equivalent check, replace them with "List::is_empty()".
--- rust/kernel/list.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
rust/kernel/list.rs +5 -5
@@ -259,7 +259,7 @@ pub fn push_back(&mut self, item: ListArc<T, ID>) {// SAFETY: We have not yet called `post_remove`, so `list_links` is still valid. let item =unsafe {ListLinks ::fields (list_links) }; -if self.first.is_null () { +if self.is_empty () { self.first = item;// SAFETY: The caller just gave us ownership of these fields. // INVARIANT: A linked list with one item should be cyclic.
@@ -299,7 +299,7 @@ pub fn push_front(&mut self, item: ListArc<T, ID>) {// SAFETY: We have not yet called `post_remove`, so `list_links` is still valid. let item =unsafe {ListLinks ::fields (list_links) }; -if self.first.is_null () { +if self.is_empty () {// SAFETY: The caller just gave us ownership of these fields. // INVARIANT: A linked list with one item should be cyclic. unsafe {
@@ -325,7 +325,7 @@ pub fn push_front(&mut self, item: ListArc<T, ID>) {/// Removes the last item from this list. pub fn pop_back (&mut self) ->Option <ListArc <T ,ID >> { -if self.first.is_null () { +if self.is_empty () {return None ; }
@@ -337,7 +337,7 @@ pub fn pop_back(&mut self) -> Option<ListArc<T, ID>> {/// Removes the first item from this list. pub fn pop_front (&mut self) ->Option <ListArc <T ,ID >> { -if self.first.is_null () { +if self.is_empty () {return None ; }
@@ -493,7 +493,7 @@ pub fn push_all_back(&mut self, other: &mut List<T, ID>) {/// /// If the list is empty, this returns `None`. pub fn cursor_front (&mut self) ->Option <Cursor <'_,T ,ID >> { -if self.first.is_null () { +if self.is_empty () {None }else {Some (Cursor {
Signature · 1 line
2.43.0
Miguel Ojeda
maintainerQuoted 7 lines · On Mon, 10 Mar 2025 15:38:52 +0800 I Hsin Cheng <[email protected]> wrote:
"
List::is_empty()" provides a straight forward convention to check whether a given "List" is empty or not. There're numerous places in the current implementation still use "self.first.is_null()" to perform the equivalent check, replace them with "List::is_empty()".Signed-off-by I Hsin Cheng <[email protected]>
There are a couple cases that still apply here (i.e. after the cursor between elements change), so I will pick it up.
By the way, for some reason, your email did not reach my inbox.
Thanks!
Cheers,
Miguel
Quoted 7 lines · On Mon, 10 Mar 2025 15:38:52 +0800 I Hsin Cheng <[email protected]> wrote:
"
List::is_empty()" provides a straight forward convention to check whether a given "List" is empty or not. There're numerous places in the current implementation still use "self.first.is_null()" to perform the equivalent check, replace them with "List::is_empty()".Signed-off-by I Hsin Cheng <[email protected]>
Applied to rust-next -- thanks!
[ Rebased dropping the cases that do not apply anymore. - Miguel ]
Cheers,
Miguel
Quoted 9 lines · I Hsin Cheng
"
List::is_empty()" provides a straight forward convention to check whether a given "List" is empty or not. There're numerous places in the current implementation still use "self.first.is_null()" to perform the equivalent check, replace them with "List::is_empty()".Signed-off-by I Hsin Cheng <[email protected]>--- rust/kernel/list.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
---
Cheers,
Benno