rust: alloc: add Vec::pop
Quoted 26 lines · Alice Ryhl
This introduces a basic method that our custom Vec is missing. I expect that it will be used in many places, but at the time of writing, Rust Binder has six calls to
Vec::pop.Signed-off-by Alice Ryhl <[email protected]>--- rust/kernel/alloc/kvec.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)rust/kernel/alloc/kvec.rs +9
-0@@ -302,6 +302,37 @@ pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {Ok (()) } +/// Removes the last element from a vector and returns it, or `None` if it is empty. +/// +/// # Examples +/// +/// ``` +/// let mut v = KVec::new(); +/// v.push(1, GFP_KERNEL)?; +/// v.push(2, GFP_KERNEL)?; +/// assert_eq!(&v, &[1, 2]);
rust/kernel/alloc/kvec.rs pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { · line 323
Quoted 10 lines
rust/kernel/alloc/kvec.rs +10
-0+/// +/// assert_eq!(v.pop(), Some(2)); +/// assert_eq!(v.pop(), Some(1)); +/// assert_eq!(v.pop(), None); +/// # Ok::<(), Error>(()) +/// ``` +pub fn pop (&mut self) ->Option <T > { +let Some (len_sub_1) = self.len.checked_sub (1 )else { +return None ; + };
Isn't it possible to do:?
let len_sub_1 = self.len.checked_sub(1)?;
rust/kernel/alloc/kvec.rs pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { · line 325
Quoted 2 lines
rust/kernel/alloc/kvec.rs +2
-0+ +// INVARIANT: If the first `len` elements are valid, then the first `len-1` elements are
Please add spaces around -.
rust/kernel/alloc/kvec.rs pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { · line 330
Quoted 5 lines
rust/kernel/alloc/kvec.rs +5
-0+// valid. + self.len = len_sub_1; + +// INVARIANT: This invalidates a value in this vector's allocation, but the Vec invariants +// do not require it to be valid because `self.len <= len_sub_1`.
I don't think this should be an INVARIANT comment. Maybe we don't even need it.
---
Cheers,
Benno
Quoted 8 lines
rust/kernel/alloc/kvec.rs +5
-0+// SAFETY: Since `len_sub_1` is less than the value `self.len` had at the beginning of +// `pop`, this index holds a valid value. +Some (unsafe { self.as_mut_ptr ().add (len_sub_1).read () }) + } +/// Creates a new [`Vec`] instance with at least the given capacity. /// /// # Examples
Alice Ryhl
reviewerQuoted 40 lines · Benno Lossin
Quoted 26 lines · Alice Ryhl
This introduces a basic method that our custom Vec is missing. I expect that it will be used in many places, but at the time of writing, Rust Binder has six calls to
Vec::pop.Signed-off-by Alice Ryhl <[email protected]>--- rust/kernel/alloc/kvec.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+)rust/kernel/alloc/kvec.rs +9
-0@@ -302,6 +302,37 @@ pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> {Ok (()) } +/// Removes the last element from a vector and returns it, or `None` if it is empty. +/// +/// # Examples +/// +/// ``` +/// let mut v = KVec::new(); +/// v.push(1, GFP_KERNEL)?; +/// v.push(2, GFP_KERNEL)?; +/// assert_eq!(&v, &[1, 2]); rust/kernel/alloc/kvec.rs pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { · line 323
Quoted 10 lines
rust/kernel/alloc/kvec.rs +10
-0+/// +/// assert_eq!(v.pop(), Some(2)); +/// assert_eq!(v.pop(), Some(1)); +/// assert_eq!(v.pop(), None); +/// # Ok::<(), Error>(()) +/// ``` +pub fn pop (&mut self) ->Option <T > { +let Some (len_sub_1) = self.len.checked_sub (1 )else { +return None ; + };Isn't it possible to do:?
Quoted 1 line
let len_sub_1 = self.len.checked_sub(1)?;
Yes, good catch.
Quoted 4 lines
rust/kernel/alloc/kvec.rs pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { · line 325
Quoted 2 lines
rust/kernel/alloc/kvec.rs +2
-0+ +// INVARIANT: If the first `len` elements are valid, then the first `len-1` elements are Please add spaces around
-.
I can do that, but does it really read better?
Quoted 8 lines
rust/kernel/alloc/kvec.rs pub fn push(&mut self, v: T, flags: Flags) -> Result<(), AllocError> { · line 330
Quoted 5 lines
rust/kernel/alloc/kvec.rs +5
-0+// valid. + self.len = len_sub_1; + +// INVARIANT: This invalidates a value in this vector's allocation, but the Vec invariants +// do not require it to be valid because `self.len <= len_sub_1`. I don't think this should be an
INVARIANTcomment. Maybe we don't even need it.
I can drop the INVARIANT: prefix.
Alice
Quoted 8 lines · Alice Ryhl
Quoted 5 lines · Benno Lossin
Review
Quoted 2 lines · Alice Ryhl
(quoted diff) +2
-0+ + // INVARIANT: If the first `len` elements are valid, then the first `len-1` elements arePlease add spaces around
-.I can do that, but does it really read better?
That's how rustfmt would format it and I do think it improves readability for me.
Quoted 10 lines
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +5
-0+ // valid. + self.len = len_sub_1; + + // INVARIANT: This invalidates a value in this vector's allocation, but the Vec invariants + // do not require it to be valid because `self.len <= len_sub_1`.I don't think this should be an
INVARIANTcomment. Maybe we don't even need it.I can drop the INVARIANT: prefix.
Sounds good.
---
Cheers,
Benno