From: Tamir Duberstein <tamird@gmail.com>
To: "Danilo Krummrich" <dakr@kernel.org>,
"Andrew Ballance" <andrewjballance@gmail.com>,
"Alice Ryhl" <aliceryhl@google.com>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <benno.lossin@proton.me>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Trevor Gross" <tmgross@umich.edu>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org,
Tamir Duberstein <tamird@gmail.com>
Subject: [PATCH v4 3/4] rust: alloc: refactor `Vec::truncate` using `dec_len`
Date: Wed, 16 Apr 2025 13:15:42 -0400 [thread overview]
Message-ID: <20250416-vec-set-len-v4-3-112b222604cd@gmail.com> (raw)
In-Reply-To: <20250416-vec-set-len-v4-0-112b222604cd@gmail.com>
Use `checked_sub` to satisfy the safety requirements of `dec_len` and
replace nearly the whole body of `truncate` with a call to `dec_len`.
Reviewed-by: Andrew Ballance <andrewjballance@gmail.com>
Reviewed-by: Alice Ryhl <aliceryhl@google.com>
Signed-off-by: Tamir Duberstein <tamird@gmail.com>
---
rust/kernel/alloc/kvec.rs | 24 ++++++++----------------
1 file changed, 8 insertions(+), 16 deletions(-)
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index a84a907acae4..87dc37ecb94d 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -488,23 +488,15 @@ pub fn reserve(&mut self, additional: usize, flags: Flags) -> Result<(), AllocEr
/// # Ok::<(), Error>(())
/// ```
pub fn truncate(&mut self, len: usize) {
- if len >= self.len() {
- return;
+ if let Some(count) = self.len().checked_sub(len) {
+ // SAFETY: `count` is `self.len() - len` so it is guaranteed to be less than or
+ // equal to `self.len()`.
+ let ptr: *mut [T] = unsafe { self.dec_len(count) };
+
+ // SAFETY: the contract of `dec_len` guarantees that the elements in `ptr` are
+ // valid elements whose ownership has been transferred to the caller.
+ unsafe { ptr::drop_in_place(ptr) };
}
-
- let drop_range = len..self.len();
-
- // SAFETY: `drop_range` is a subrange of `[0, len)` by the bounds check above.
- let ptr: *mut [T] = unsafe { self.get_unchecked_mut(drop_range) };
-
- // SAFETY: By the above bounds check, it is guaranteed that `len < self.capacity()`.
- unsafe { self.set_len(len) };
-
- // SAFETY:
- // - the dropped values are valid `T`s by the type invariant
- // - we are allowed to invalidate [`new_len`, `old_len`) because we just changed the
- // len, therefore we have exclusive access to [`new_len`, `old_len`)
- unsafe { ptr::drop_in_place(ptr) };
}
}
--
2.49.0
next prev parent reply other threads:[~2025-04-16 17:15 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-16 17:15 [PATCH v4 0/4] rust: alloc: split `Vec::set_len` into `Vec::{inc,dec}_len` Tamir Duberstein
2025-04-16 17:15 ` [PATCH v4 1/4] rust: alloc: add Vec::len() <= Vec::capacity invariant Tamir Duberstein
2025-04-23 10:00 ` Alice Ryhl
2025-04-16 17:15 ` [PATCH v4 2/4] rust: alloc: add `Vec::dec_len` Tamir Duberstein
2025-04-16 17:15 ` Tamir Duberstein [this message]
2025-04-16 17:15 ` [PATCH v4 4/4] rust: alloc: replace `Vec::set_len` with `inc_len` Tamir Duberstein
2025-04-23 10:10 ` [PATCH v4 0/4] rust: alloc: split `Vec::set_len` into `Vec::{inc,dec}_len` Danilo Krummrich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250416-vec-set-len-v4-3-112b222604cd@gmail.com \
--to=tamird@gmail.com \
--cc=a.hindborg@kernel.org \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=andrewjballance@gmail.com \
--cc=benno.lossin@proton.me \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=linux-kernel@vger.kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tmgross@umich.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).