From 64d98f8ee037282c35007b64c2649055c56af1db Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:19:03 +0200 Subject: Merging upstream version 1.68.2+dfsg1. Signed-off-by: Daniel Baumann --- src/doc/nomicon/src/borrow-splitting.md | 4 ++-- src/doc/nomicon/src/ffi.md | 3 ++- src/doc/nomicon/src/lifetime-mismatch.md | 2 +- src/doc/nomicon/src/vec/vec-raw.md | 19 +++++++------------ 4 files changed, 12 insertions(+), 16 deletions(-) (limited to 'src/doc/nomicon') diff --git a/src/doc/nomicon/src/borrow-splitting.md b/src/doc/nomicon/src/borrow-splitting.md index 3d13ff954..08b6d0d48 100644 --- a/src/doc/nomicon/src/borrow-splitting.md +++ b/src/doc/nomicon/src/borrow-splitting.md @@ -159,7 +159,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option { - let slice = mem::replace(&mut self.0, &mut []); + let slice = mem::take(&mut self.0); if slice.is_empty() { return None; } let (l, r) = slice.split_at_mut(1); @@ -170,7 +170,7 @@ impl<'a, T> Iterator for IterMut<'a, T> { impl<'a, T> DoubleEndedIterator for IterMut<'a, T> { fn next_back(&mut self) -> Option { - let slice = mem::replace(&mut self.0, &mut []); + let slice = mem::take(&mut self.0); if slice.is_empty() { return None; } let new_len = slice.len() - 1; diff --git a/src/doc/nomicon/src/ffi.md b/src/doc/nomicon/src/ffi.md index 9b0ff98e6..8d1a882b3 100644 --- a/src/doc/nomicon/src/ffi.md +++ b/src/doc/nomicon/src/ffi.md @@ -659,7 +659,8 @@ Certain Rust types are defined to never be `null`. This includes references (`&T `&mut T`), boxes (`Box`), and function pointers (`extern "abi" fn()`). When interfacing with C, pointers that might be `null` are often used, which would seem to require some messy `transmute`s and/or unsafe code to handle conversions to/from Rust types. -However, the language provides a workaround. +However, trying to construct/work with these invalid values **is undefined behavior**, +so you should use the following workaround instead. As a special case, an `enum` is eligible for the "nullable pointer optimization" if it contains exactly two variants, one of which contains no data and the other contains a field of one of the diff --git a/src/doc/nomicon/src/lifetime-mismatch.md b/src/doc/nomicon/src/lifetime-mismatch.md index bc53f06f8..ecb6cf205 100644 --- a/src/doc/nomicon/src/lifetime-mismatch.md +++ b/src/doc/nomicon/src/lifetime-mismatch.md @@ -65,7 +65,7 @@ fn main() { The lifetime system is forced to extend the `&mut foo` to have lifetime `'c`, due to the lifetime of `loan` and `mutate_and_share`'s signature. Then when we -try to call `share`, and it sees we're trying to alias that `&'c mut foo` and +try to call `share`, it sees we're trying to alias that `&'c mut foo` and blows up in our face! This program is clearly correct according to the reference semantics we actually diff --git a/src/doc/nomicon/src/vec/vec-raw.md b/src/doc/nomicon/src/vec/vec-raw.md index 728feaa58..0bca2daf8 100644 --- a/src/doc/nomicon/src/vec/vec-raw.md +++ b/src/doc/nomicon/src/vec/vec-raw.md @@ -28,18 +28,13 @@ impl RawVec { } fn grow(&mut self) { - let (new_cap, new_layout) = if self.cap == 0 { - (1, Layout::array::(1).unwrap()) - } else { - // This can't overflow because we ensure self.cap <= isize::MAX. - let new_cap = 2 * self.cap; - - // Layout::array checks that the number of bytes is <= usize::MAX, - // but this is redundant since old_layout.size() <= isize::MAX, - // so the `unwrap` should never fail. - let new_layout = Layout::array::(new_cap).unwrap(); - (new_cap, new_layout) - }; + // This can't overflow because we ensure self.cap <= isize::MAX. + let new_cap = if self.cap == 0 { 1 } else { 2 * self.cap }; + + // Layout::array checks that the number of bytes is <= usize::MAX, + // but this is redundant since old_layout.size() <= isize::MAX, + // so the `unwrap` should never fail. + let new_layout = Layout::array::(new_cap).unwrap(); // Ensure that the new allocation doesn't exceed `isize::MAX` bytes. assert!(new_layout.size() <= isize::MAX as usize, "Allocation too large"); -- cgit v1.2.3