Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions library/alloc/src/collections/vec_deque/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,8 +1449,8 @@ impl<T, A: Allocator> VecDeque<T, A> {
// Safe because:
//
// * Any slice passed to `drop_in_place` is valid; the second case has
// `len <= front.len()` and returning on `len > self.len()` ensures
// `begin <= back.len()` in the first case
// `len <= front.len()` and returning on `len >= self.len()` ensures
// `begin < back.len()` in the first case
// * The head of the VecDeque is moved before calling `drop_in_place`,
// so no value is dropped twice if `drop_in_place` panics
unsafe {
Expand Down
11 changes: 6 additions & 5 deletions library/alloc/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1457,8 +1457,8 @@ impl String {

/// Shortens this `String` to the specified length.
///
/// If `new_len` is greater than or equal to the string's current length, this has no
/// effect.
/// If `new_len` is greater than or equal to the string's current length,
/// this has no effect.
///
/// Note that this method has no effect on the allocated capacity
/// of the string
Expand All @@ -1480,10 +1480,11 @@ impl String {
#[stable(feature = "rust1", since = "1.0.0")]
#[track_caller]
pub fn truncate(&mut self, new_len: usize) {
if new_len <= self.len() {
assert!(self.is_char_boundary(new_len));
self.vec.truncate(new_len)
if new_len >= self.len() {
return;
}
assert!(self.is_char_boundary(new_len));
self.vec.truncate(new_len)
}

/// Removes the last character from the string buffer and returns it.
Expand Down
17 changes: 8 additions & 9 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,8 +1790,8 @@ impl<T, A: Allocator> Vec<T, A> {
/// assert_eq!(vec, [1, 2]);
/// ```
///
/// No truncation occurs when `len` is greater than the vector's current
/// length:
/// No truncation occurs when `len` is greater than or equal to the
/// vector's current length:
///
/// ```
/// let mut vec = vec![1, 2, 3];
Expand All @@ -1817,16 +1817,13 @@ impl<T, A: Allocator> Vec<T, A> {

// This is safe because:
//
// * the slice passed to `drop_in_place` is valid; the `len > self.len`
// * the slice passed to `drop_in_place` is valid; the `len >= self.len`
// case avoids creating an invalid slice, and
// * the `len` of the vector is shrunk before calling `drop_in_place`,
// such that no value will be dropped twice in case `drop_in_place`
// were to panic once (if it panics twice, the program aborts).
unsafe {
// Note: It's intentional that this is `>` and not `>=`.
// Changing it to `>=` has negative performance
// implications in some cases. See #78884 for more.
if len > self.len {
if len >= self.len {
return;
}
let remaining_len = self.len - len;
Expand Down Expand Up @@ -3151,7 +3148,8 @@ impl<T, A: Allocator> Vec<T, A> {
/// calling the closure `f`. The return values from `f` will end up
/// in the `Vec` in the order they have been generated.
///
/// If `new_len` is less than `len`, the `Vec` is simply truncated.
/// If `new_len` is less than or equal to `len`, the `Vec` is simply
/// truncated.
///
/// This method uses a closure to create new values on every push. If
/// you'd rather [`Clone`] a given value, use [`Vec::resize`]. If you
Expand Down Expand Up @@ -3501,7 +3499,8 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
///
/// If `new_len` is greater than `len`, the `Vec` is extended by the
/// difference, with each additional slot filled with `value`.
/// If `new_len` is less than `len`, the `Vec` is simply truncated.
/// If `new_len` is less than or equal to `len`, the `Vec` is simply
/// truncated.
///
/// This method requires `T` to implement [`Clone`],
/// in order to be able to clone the passed value.
Expand Down
11 changes: 6 additions & 5 deletions library/alloc/src/wtf8/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,18 +342,19 @@ impl Wtf8Buf {

/// Shortens a string to the specified length.
///
/// If `new_len` is greater than the string's current length, this has no
/// effect.
/// If `new_len` is greater than or equal to the string's current length,
/// this has no effect.
///
/// # Panics
///
/// Panics if `new_len` does not lie on a code point boundary.
#[inline]
pub fn truncate(&mut self, new_len: usize) {
if new_len <= self.len() {
assert!(self.is_code_point_boundary(new_len));
self.bytes.truncate(new_len)
if new_len >= self.len() {
return;
}
assert!(self.is_code_point_boundary(new_len));
self.bytes.truncate(new_len)
}

/// Consumes the WTF-8 string and tries to convert it to a vec of bytes.
Expand Down
13 changes: 7 additions & 6 deletions library/std/src/ffi/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,8 +576,8 @@ impl OsString {

/// Truncate the `OsString` to the specified length.
///
/// If `new_len` is greater than the string's current length, this has no
/// effect.
/// If `new_len` is greater than or equal to the string's current length,
/// this has no effect.
///
/// # Panics
///
Expand All @@ -586,11 +586,12 @@ impl OsString {
#[inline]
#[unstable(feature = "os_string_truncate", issue = "133262")]
pub fn truncate(&mut self, len: usize) {
if len <= self.len() {
self.as_os_str().inner.check_public_boundary(len);
// SAFETY: The length was just checked to be at a valid boundary.
unsafe { self.inner.truncate_unchecked(len) };
if len >= self.len() {
return;
}
self.as_os_str().inner.check_public_boundary(len);
// SAFETY: The length was just checked to be at a valid boundary.
unsafe { self.inner.truncate_unchecked(len) };
}

/// Provides plumbing to `Vec::extend_from_slice` without giving full
Expand Down
Loading