rust: alloc: add Vec::drain_all
Quoted 4 lines · Alice Ryhl
This is like the stdlib method drain, except that it's hard-coded to use the entire vector's range. Rust Binder uses it in the range allocator to take ownership of everything in a vector in a case where reusing the vector is desirable.
Is the reason for not implementing drain complexity?
Quoted 4 lines
Implementing
DrainAllin terms ofslice::IterMutlets us reuse some nice optimizations in core for the case where T is a ZST.Signed-off-by Alice Ryhl <[email protected]>
The code is good, but I'd like to know the answer to the above question before giving my RB.
Quoted 20 lines
--- rust/kernel/alloc/kvec.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)rust/kernel/alloc/kvec.rs +8
-0@@ -564,6 +564,30 @@ pub fn truncate(&mut self, len: usize) {// len, therefore we have exclusive access to [`new_len`, `old_len`) unsafe { ptr::drop_in_place (ptr) }; } + +/// Takes ownership of all items in this vector without consuming the allocation. +/// +/// # Examples +/// +/// ``` +/// let mut v = kernel::kvec![0, 1, 2, 3]?; +///
rust/kernel/alloc/kvec.rs pub fn truncate(&mut self, len: usize) { · line 584
Quoted 10 lines
rust/kernel/alloc/kvec.rs +10
-0+/// for (i, j) in v.drain_all().enumerate() { +/// assert_eq!(i, j); +/// } +/// +/// assert!(v.capacity() >= 4); +/// ``` +pub fn drain_all (&mut self) ->DrainAll <'_,T > { +let len = self.len (); +// INVARIANT: The first 0 elements are valid. + self.len =0 ;
Why not set_len?
Quoted 32 lines
rust/kernel/alloc/kvec.rs +25
-0+// INVARIANT: The first `len` elements of the spare capacity are valid values, and as we +// just set the length to zero, we may transfer ownership to the `DrainAll` object. +DrainAll { + elements: self.spare_capacity_mut ()[..len].iter_mut (), + } + } }impl <T :Clone ,A :Allocator >Vec <T ,A > {@@ -1049,3 +1073,36 @@ fn into_iter(self) -> Self::IntoIter { } } } + +/// An iterator that owns all items in a vector, but does not own its allocation. +/// +/// # Invariants +/// +/// Every `&mut MaybeUninit<T>` returned by the iterator contains a valid `T` owned by this +/// `DrainAll`. +pub struct DrainAll <'vec,T > { + elements: slice::IterMut <'vec,MaybeUninit <T >>, +} + +impl <'vec,T >Iterator for DrainAll <'vec,T > { +type Item =T ; + +fn next (&mut self) ->Option <T > { +let elem = self.elements.next ()?; +// SAFETY: By the type invariants, we may take ownership of the value in this +// `MaybeUninit<T>`. +Some (unsafe { elem.assume_init_read () })
rust/kernel/alloc/kvec.rs fn into_iter(self) -> Self::IntoIter { · line 1104
Quoted 10 lines
rust/kernel/alloc/kvec.rs +10
-0+ } + +fn size_hint (&self) -> (usize ,Option <usize >) { + self.elements.size_hint () + } +} + +impl <'vec,T >Drop for DrainAll <'vec,T > { +fn drop (&mut self) { +if core::mem::needs_drop ::<T >() {
This is neat!
---
Cheers,
Benno
Quoted 4 lines
rust/kernel/alloc/kvec.rs +4
-0+while self.next ().is_some () {} + } + } +}
Alice Ryhl
reviewerQuoted 7 lines · Benno Lossin
Quoted 4 lines · Alice Ryhl
This is like the stdlib method drain, except that it's hard-coded to use the entire vector's range. Rust Binder uses it in the range allocator to take ownership of everything in a vector in a case where reusing the vector is desirable.
Is the reason for not implementing
draincomplexity?
Yes.
Quoted 32 lines
Quoted 4 lines
Implementing
DrainAllin terms ofslice::IterMutlets us reuse some nice optimizations in core for the case where T is a ZST.Signed-off-by Alice Ryhl <[email protected]>The code is good, but I'd like to know the answer to the above question before giving my RB.
Quoted 24 lines
--- rust/kernel/alloc/kvec.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)rust/kernel/alloc/kvec.rs +12
-0@@ -564,6 +564,30 @@ pub fn truncate(&mut self, len: usize) {// len, therefore we have exclusive access to [`new_len`, `old_len`) unsafe { ptr::drop_in_place (ptr) }; } + +/// Takes ownership of all items in this vector without consuming the allocation. +/// +/// # Examples +/// +/// ``` +/// let mut v = kernel::kvec![0, 1, 2, 3]?; +/// +/// for (i, j) in v.drain_all().enumerate() { +/// assert_eq!(i, j); +/// } +///
Quoted 8 lines
rust/kernel/alloc/kvec.rs pub fn truncate(&mut self, len: usize) { · line 584
Quoted 6 lines
rust/kernel/alloc/kvec.rs +6
-0+/// assert!(v.capacity() >= 4); +/// ``` +pub fn drain_all (&mut self) ->DrainAll <'_,T > { +let len = self.len (); +// INVARIANT: The first 0 elements are valid. + self.len =0 ;Why not
set_len?
I can use set_len.
Alice
Quoted 10 lines · Alice Ryhl
Quoted 7 lines · Benno Lossin
Quoted 4 lines · Alice Ryhl
This is like the stdlib method drain, except that it's hard-coded to use the entire vector's range. Rust Binder uses it in the range allocator to take ownership of everything in a vector in a case where reusing the vector is desirable.
Is the reason for not implementing
draincomplexity?Yes.
I thought more about it and as long as the person implementing drain, removes drain_all, I have no complaints. (will give my RB in reply to the patch in hopes that the in-reply-to header is set correctly)
---
Cheers,
Benno