Skip to content

Commit 5e8e657

Browse files
committed
rust: alloc: vec: Import .drain() / Drain from rust library
Contains the implementation from https://github.com/rust-lang/rust/blob/1.82.0/library/alloc/src/vec/mod.rs and the Drain struct from https://github.com/rust-lang/rust/blob/1.82.0/library/alloc/src/vec/drain.rs modified for the Kernel. Signed-off-by: Janne Grunau <j@jannau.net>
1 parent a8edf2a commit 5e8e657

4 files changed

Lines changed: 303 additions & 0 deletions

File tree

rust/kernel/alloc.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
#[cfg(not(any(test, testlib)))]
66
pub mod allocator;
7+
pub mod drain;
78
pub mod kbox;
89
pub mod kvec;
910
pub mod layout;

rust/kernel/alloc/drain.rs

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
//! Rust standard library vendored code.
2+
//!
3+
//! The contents of this file come from the Rust standard library, hosted in
4+
//! the <https://github.com/rust-lang/rust> repository, licensed under
5+
//! "Apache-2.0 OR MIT" and adapted for kernel use. For copyright details,
6+
//! see <https://github.com/rust-lang/rust/blob/master/COPYRIGHT>.
7+
#![allow(clippy::undocumented_unsafe_blocks)]
8+
9+
use core::fmt;
10+
use core::iter::FusedIterator;
11+
use core::mem::{self, ManuallyDrop, SizedTypeProperties};
12+
use core::ptr::{self, NonNull};
13+
use core::slice::{self};
14+
15+
use super::{kvec::Vec, Allocator};
16+
17+
/// A draining iterator for `Vec<T>`.
18+
///
19+
/// This `struct` is created by [`Vec::drain`].
20+
/// See its documentation for more.
21+
///
22+
/// # Example
23+
///
24+
/// ```
25+
/// let mut v = vec![0, 1, 2];
26+
/// let iter: std::vec::Drain<'_, _> = v.drain(..);
27+
/// ```
28+
// #[stable(feature = "drain", since = "1.6.0")]
29+
pub struct Drain<
30+
'a,
31+
T: 'a,
32+
A: Allocator,
33+
// #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global,
34+
> {
35+
/// Index of tail to preserve
36+
pub(super) tail_start: usize,
37+
/// Length of tail
38+
pub(super) tail_len: usize,
39+
/// Current remaining range to remove
40+
pub(super) iter: slice::Iter<'a, T>,
41+
pub(super) vec: NonNull<Vec<T, A>>,
42+
}
43+
44+
// #[stable(feature = "collection_debug", since = "1.17.0")]
45+
impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
46+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47+
f.debug_tuple("Drain").field(&self.iter.as_slice()).finish()
48+
}
49+
}
50+
51+
impl<'a, T, A: Allocator> Drain<'a, T, A> {
52+
/// Returns the remaining items of this iterator as a slice.
53+
///
54+
/// # Examples
55+
///
56+
/// ```
57+
/// let mut vec = vec!['a', 'b', 'c'];
58+
/// let mut drain = vec.drain(..);
59+
/// assert_eq!(drain.as_slice(), &['a', 'b', 'c']);
60+
/// let _ = drain.next().unwrap();
61+
/// assert_eq!(drain.as_slice(), &['b', 'c']);
62+
/// ```
63+
#[must_use]
64+
// #[stable(feature = "vec_drain_as_slice", since = "1.46.0")]
65+
pub fn as_slice(&self) -> &[T] {
66+
self.iter.as_slice()
67+
}
68+
69+
/// Keep unyielded elements in the source `Vec`.
70+
///
71+
/// # Examples
72+
///
73+
/// ```
74+
/// #![feature(drain_keep_rest)]
75+
///
76+
/// let mut vec = vec!['a', 'b', 'c'];
77+
/// let mut drain = vec.drain(..);
78+
///
79+
/// assert_eq!(drain.next().unwrap(), 'a');
80+
///
81+
/// // This call keeps 'b' and 'c' in the vec.
82+
/// drain.keep_rest();
83+
///
84+
/// // If we wouldn't call `keep_rest()`,
85+
/// // `vec` would be empty.
86+
/// assert_eq!(vec, ['b', 'c']);
87+
/// ```
88+
// #[unstable(feature = "drain_keep_rest", issue = "101122")]
89+
pub fn keep_rest(self) {
90+
// At this moment layout looks like this:
91+
//
92+
// [head] [yielded by next] [unyielded] [yielded by next_back] [tail]
93+
// ^-- start \_________/-- unyielded_len \____/-- self.tail_len
94+
// ^-- unyielded_ptr ^-- tail
95+
//
96+
// Normally `Drop` impl would drop [unyielded] and then move [tail] to the `start`.
97+
// Here we want to
98+
// 1. Move [unyielded] to `start`
99+
// 2. Move [tail] to a new start at `start + len(unyielded)`
100+
// 3. Update length of the original vec to `len(head) + len(unyielded) + len(tail)`
101+
// a. In case of ZST, this is the only thing we want to do
102+
// 4. Do *not* drop self, as everything is put in a consistent state already, there is nothing to do
103+
let mut this = ManuallyDrop::new(self);
104+
105+
unsafe {
106+
let source_vec = this.vec.as_mut();
107+
108+
let start = source_vec.len();
109+
let tail = this.tail_start;
110+
111+
let unyielded_len = this.iter.len();
112+
let unyielded_ptr = this.iter.as_slice().as_ptr();
113+
114+
// ZSTs have no identity, so we don't need to move them around.
115+
if !T::IS_ZST {
116+
let start_ptr = source_vec.as_mut_ptr().add(start);
117+
118+
// memmove back unyielded elements
119+
if unyielded_ptr != start_ptr {
120+
let src = unyielded_ptr;
121+
let dst = start_ptr;
122+
123+
ptr::copy(src, dst, unyielded_len);
124+
}
125+
126+
// memmove back untouched tail
127+
if tail != (start + unyielded_len) {
128+
let src = source_vec.as_ptr().add(tail);
129+
let dst = start_ptr.add(unyielded_len);
130+
ptr::copy(src, dst, this.tail_len);
131+
}
132+
}
133+
134+
source_vec.set_len(start + unyielded_len + this.tail_len);
135+
}
136+
}
137+
}
138+
139+
// #[stable(feature = "vec_drain_as_slice", since = "1.46.0")]
140+
impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A> {
141+
fn as_ref(&self) -> &[T] {
142+
self.as_slice()
143+
}
144+
}
145+
146+
// #[stable(feature = "drain", since = "1.6.0")]
147+
unsafe impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A> {}
148+
// #[stable(feature = "drain", since = "1.6.0")]
149+
unsafe impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A> {}
150+
151+
// #[stable(feature = "drain", since = "1.6.0")]
152+
impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
153+
type Item = T;
154+
155+
#[inline]
156+
fn next(&mut self) -> Option<T> {
157+
self.iter
158+
.next()
159+
.map(|elt| unsafe { ptr::read(elt as *const _) })
160+
}
161+
162+
fn size_hint(&self) -> (usize, Option<usize>) {
163+
self.iter.size_hint()
164+
}
165+
}
166+
167+
// #[stable(feature = "drain", since = "1.6.0")]
168+
impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
169+
#[inline]
170+
fn next_back(&mut self) -> Option<T> {
171+
self.iter
172+
.next_back()
173+
.map(|elt| unsafe { ptr::read(elt as *const _) })
174+
}
175+
}
176+
177+
// #[stable(feature = "drain", since = "1.6.0")]
178+
impl<T, A: Allocator> Drop for Drain<'_, T, A> {
179+
fn drop(&mut self) {
180+
/// Moves back the un-`Drain`ed elements to restore the original `Vec`.
181+
struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>);
182+
183+
impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> {
184+
fn drop(&mut self) {
185+
if self.0.tail_len > 0 {
186+
unsafe {
187+
let source_vec = self.0.vec.as_mut();
188+
// memmove back untouched tail, update to new length
189+
let start = source_vec.len();
190+
let tail = self.0.tail_start;
191+
if tail != start {
192+
let src = source_vec.as_ptr().add(tail);
193+
let dst = source_vec.as_mut_ptr().add(start);
194+
ptr::copy(src, dst, self.0.tail_len);
195+
}
196+
source_vec.set_len(start + self.0.tail_len);
197+
}
198+
}
199+
}
200+
}
201+
202+
let iter = mem::take(&mut self.iter);
203+
let drop_len = iter.len();
204+
205+
let mut vec = self.vec;
206+
207+
if T::IS_ZST {
208+
// ZSTs have no identity, so we don't need to move them around, we only need to drop the correct amount.
209+
// this can be achieved by manipulating the Vec length instead of moving values out from `iter`.
210+
unsafe {
211+
let vec = vec.as_mut();
212+
let old_len = vec.len();
213+
vec.set_len(old_len + drop_len + self.tail_len);
214+
vec.truncate(old_len + self.tail_len);
215+
}
216+
217+
return;
218+
}
219+
220+
// ensure elements are moved back into their appropriate places, even when drop_in_place panics
221+
let _guard = DropGuard(self);
222+
223+
if drop_len == 0 {
224+
return;
225+
}
226+
227+
// as_slice() must only be called when iter.len() is > 0 because
228+
// it also gets touched by vec::Splice which may turn it into a dangling pointer
229+
// which would make it and the vec pointer point to different allocations which would
230+
// lead to invalid pointer arithmetic below.
231+
let drop_ptr = iter.as_slice().as_ptr();
232+
233+
unsafe {
234+
// drop_ptr comes from a slice::Iter which only gives us a &[T] but for drop_in_place
235+
// a pointer with mutable provenance is necessary. Therefore we must reconstruct
236+
// it from the original vec but also avoid creating a &mut to the front since that could
237+
// invalidate raw pointers to it which some unsafe code might rely on.
238+
let vec_ptr = vec.as_mut().as_mut_ptr();
239+
let drop_offset = drop_ptr.sub_ptr(vec_ptr);
240+
let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len);
241+
ptr::drop_in_place(to_drop);
242+
}
243+
}
244+
}
245+
246+
// #[stable(feature = "fused", since = "1.26.0")]
247+
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}

rust/kernel/alloc/kvec.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
use super::{
66
allocator::{KVmalloc, Kmalloc, Vmalloc},
7+
drain::Drain,
78
layout::ArrayLayout,
89
AllocError, Allocator, Box, Flags,
910
};
@@ -15,6 +16,7 @@ use core::{
1516
ops::DerefMut,
1617
ops::Index,
1718
ops::IndexMut,
19+
ops::{Range, RangeBounds},
1820
ptr,
1921
ptr::NonNull,
2022
slice,
@@ -546,6 +548,56 @@ where
546548
ptr::drop_in_place(s);
547549
}
548550
}
551+
552+
/// Removes the specified range from the vector in bulk, returning all
553+
/// removed elements as an iterator. If the iterator is dropped before
554+
/// being fully consumed, it drops the remaining removed elements.
555+
///
556+
/// The returned iterator keeps a mutable borrow on the vector to optimize
557+
/// its implementation.
558+
///
559+
/// # Panics
560+
///
561+
/// Panics if the starting point is greater than the end point or if
562+
/// the end point is greater than the length of the vector.
563+
///
564+
/// # Leaking
565+
///
566+
/// If the returned iterator goes out of scope without being dropped (due to
567+
/// [`mem::forget`], for example), the vector may have lost and leaked
568+
/// elements arbitrarily, including elements outside the range.
569+
///
570+
/// # Examples
571+
///
572+
/// ```
573+
/// let mut v = vec![1, 2, 3];
574+
/// let u: Vec<_> = v.drain(1..).collect();
575+
/// assert_eq!(v, &[1]);
576+
/// assert_eq!(u, &[2, 3]);
577+
///
578+
/// // A full range clears the vector, like `clear()` does
579+
/// v.drain(..);
580+
/// assert_eq!(v, &[]);
581+
/// ```
582+
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A>
583+
where
584+
R: RangeBounds<usize>,
585+
{
586+
let len = self.len();
587+
let Range { start, end } = slice::range(range, ..len);
588+
589+
unsafe {
590+
// set self.vec length's to start, to be safe in case Drain is leaked
591+
self.set_len(start);
592+
let range_slice = slice::from_raw_parts(self.as_ptr().add(start), end - start);
593+
Drain {
594+
tail_start: end,
595+
tail_len: len - end,
596+
iter: range_slice.iter(),
597+
vec: NonNull::from(self),
598+
}
599+
}
600+
}
549601
}
550602

551603
impl<T: Clone, A: Allocator> Vec<T, A> {

rust/kernel/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#![cfg_attr(not(CONFIG_RUSTC_HAS_COERCE_POINTEE), feature(unsize))]
2020
#![feature(inline_const)]
2121
#![feature(lint_reasons)]
22+
#![feature(ptr_sub_ptr)]
23+
#![feature(sized_type_properties)]
24+
#![feature(slice_range)]
2225
// Stable in Rust 1.83
2326
#![feature(const_maybe_uninit_as_mut_ptr)]
2427
#![feature(const_mut_refs)]

0 commit comments

Comments
 (0)