summaryrefslogtreecommitdiffstats
path: root/src/doc/nomicon
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:32 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:18:32 +0000
commit4547b622d8d29df964fa2914213088b148c498fc (patch)
tree9fc6b25f3c3add6b745be9a2400a6e96140046e9 /src/doc/nomicon
parentReleasing progress-linux version 1.66.0+dfsg1-1~progress7.99u1. (diff)
downloadrustc-4547b622d8d29df964fa2914213088b148c498fc.tar.xz
rustc-4547b622d8d29df964fa2914213088b148c498fc.zip
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/doc/nomicon')
-rw-r--r--src/doc/nomicon/src/lifetime-mismatch.md2
-rw-r--r--src/doc/nomicon/src/unchecked-uninit.md13
-rw-r--r--src/doc/nomicon/src/vec/vec-alloc.md1
-rw-r--r--src/doc/nomicon/src/vec/vec-final.md2
-rw-r--r--src/doc/nomicon/src/vec/vec-insert-remove.md16
-rw-r--r--src/doc/nomicon/src/vec/vec-into-iter.md14
-rw-r--r--src/doc/nomicon/src/vec/vec-layout.md19
-rw-r--r--src/doc/nomicon/src/vec/vec-raw.md2
-rw-r--r--src/doc/nomicon/src/vec/vec-zsts.md5
9 files changed, 33 insertions, 41 deletions
diff --git a/src/doc/nomicon/src/lifetime-mismatch.md b/src/doc/nomicon/src/lifetime-mismatch.md
index 1da2d285c..bc53f06f8 100644
--- a/src/doc/nomicon/src/lifetime-mismatch.md
+++ b/src/doc/nomicon/src/lifetime-mismatch.md
@@ -74,7 +74,7 @@ care about, but the lifetime system is too coarse-grained to handle that.
## Improperly reduced borrows
The following code fails to compile, because Rust sees that a variable, `map`,
-is borrowed twice, and can not infer that the first borrow stops to be needed
+is borrowed twice, and can not infer that the first borrow ceases to be needed
before the second one occurs. This is caused by Rust conservatively falling back
to using a whole scope for the first borrow. This will eventually get fixed.
diff --git a/src/doc/nomicon/src/unchecked-uninit.md b/src/doc/nomicon/src/unchecked-uninit.md
index c61415c97..b3dd31cf1 100644
--- a/src/doc/nomicon/src/unchecked-uninit.md
+++ b/src/doc/nomicon/src/unchecked-uninit.md
@@ -11,7 +11,7 @@ Unsafe Rust gives us a powerful tool to handle this problem:
[`MaybeUninit`]. This type can be used to handle memory that has not been fully
initialized yet.
-With `MaybeUninit`, we can initialize an array element-for-element as follows:
+With `MaybeUninit`, we can initialize an array element by element as follows:
```rust
use std::mem::{self, MaybeUninit};
@@ -79,8 +79,7 @@ This code proceeds in three steps:
acknowledge that by providing appropriate methods).
It's worth spending a bit more time on the loop in the middle, and in particular
-the assignment operator and its interaction with `drop`. If we would have
-written something like:
+the assignment operator and its interaction with `drop`. If we wrote something like:
<!-- ignore: simplified code -->
```rust,ignore
@@ -88,7 +87,7 @@ written something like:
```
we would actually overwrite a `Box<u32>`, leading to `drop` of uninitialized
-data, which will cause much sadness and pain.
+data, which would cause much sadness and pain.
The correct alternative, if for some reason we cannot use `MaybeUninit::new`, is
to use the [`ptr`] module. In particular, it provides three functions that allow
@@ -97,7 +96,7 @@ us to assign bytes to a location in memory without dropping the old value:
* `ptr::write(ptr, val)` takes a `val` and moves it into the address pointed
to by `ptr`.
-* `ptr::copy(src, dest, count)` copies the bits that `count` T's would occupy
+* `ptr::copy(src, dest, count)` copies the bits that `count` T items would occupy
from src to dest. (this is equivalent to C's memmove -- note that the argument
order is reversed!)
* `ptr::copy_nonoverlapping(src, dest, count)` does what `copy` does, but a
@@ -105,8 +104,8 @@ us to assign bytes to a location in memory without dropping the old value:
(this is equivalent to C's memcpy -- note that the argument order is reversed!)
It should go without saying that these functions, if misused, will cause serious
-havoc or just straight up Undefined Behavior. The only things that these
-functions *themselves* require is that the locations you want to read and write
+havoc or just straight up Undefined Behavior. The only requirement of these
+functions *themselves* is that the locations you want to read and write
are allocated and properly aligned. However, the ways writing arbitrary bits to
arbitrary locations of memory can break things are basically uncountable!
diff --git a/src/doc/nomicon/src/vec/vec-alloc.md b/src/doc/nomicon/src/vec/vec-alloc.md
index 6c69c9379..249547339 100644
--- a/src/doc/nomicon/src/vec/vec-alloc.md
+++ b/src/doc/nomicon/src/vec/vec-alloc.md
@@ -28,7 +28,6 @@ impl<T> Vec<T> {
ptr: NonNull::dangling(),
len: 0,
cap: 0,
- _marker: PhantomData,
}
}
}
diff --git a/src/doc/nomicon/src/vec/vec-final.md b/src/doc/nomicon/src/vec/vec-final.md
index 1f8af37ad..696391d10 100644
--- a/src/doc/nomicon/src/vec/vec-final.md
+++ b/src/doc/nomicon/src/vec/vec-final.md
@@ -10,7 +10,6 @@ use std::ptr::{self, NonNull};
struct RawVec<T> {
ptr: NonNull<T>,
cap: usize,
- _marker: PhantomData<T>,
}
unsafe impl<T: Send> Send for RawVec<T> {}
@@ -25,7 +24,6 @@ impl<T> RawVec<T> {
RawVec {
ptr: NonNull::dangling(),
cap: cap,
- _marker: PhantomData,
}
}
diff --git a/src/doc/nomicon/src/vec/vec-insert-remove.md b/src/doc/nomicon/src/vec/vec-insert-remove.md
index 57283f990..722e20c45 100644
--- a/src/doc/nomicon/src/vec/vec-insert-remove.md
+++ b/src/doc/nomicon/src/vec/vec-insert-remove.md
@@ -22,9 +22,11 @@ pub fn insert(&mut self, index: usize, elem: T) {
unsafe {
// ptr::copy(src, dest, len): "copy from src to dest len elems"
- ptr::copy(self.ptr.as_ptr().add(index),
- self.ptr.as_ptr().add(index + 1),
- self.len - index);
+ ptr::copy(
+ self.ptr.as_ptr().add(index),
+ self.ptr.as_ptr().add(index + 1),
+ self.len - index,
+ );
ptr::write(self.ptr.as_ptr().add(index), elem);
self.len += 1;
}
@@ -42,9 +44,11 @@ pub fn remove(&mut self, index: usize) -> T {
unsafe {
self.len -= 1;
let result = ptr::read(self.ptr.as_ptr().add(index));
- ptr::copy(self.ptr.as_ptr().add(index + 1),
- self.ptr.as_ptr().add(index),
- self.len - index);
+ ptr::copy(
+ self.ptr.as_ptr().add(index + 1),
+ self.ptr.as_ptr().add(index),
+ self.len - index,
+ );
result
}
}
diff --git a/src/doc/nomicon/src/vec/vec-into-iter.md b/src/doc/nomicon/src/vec/vec-into-iter.md
index 61782e3b8..a3a3c8cb9 100644
--- a/src/doc/nomicon/src/vec/vec-into-iter.md
+++ b/src/doc/nomicon/src/vec/vec-into-iter.md
@@ -49,7 +49,6 @@ pub struct IntoIter<T> {
cap: usize,
start: *const T,
end: *const T,
- _marker: PhantomData<T>,
}
```
@@ -61,13 +60,13 @@ impl<T> IntoIterator for Vec<T> {
type Item = T;
type IntoIter = IntoIter<T>;
fn into_iter(self) -> IntoIter<T> {
- // Can't destructure Vec since it's Drop
- let ptr = self.ptr;
- let cap = self.cap;
- let len = self.len;
-
// Make sure not to drop Vec since that would free the buffer
- mem::forget(self);
+ let vec = ManuallyDrop::new(self);
+
+ // Can't destructure Vec since it's Drop
+ let ptr = vec.ptr;
+ let cap = vec.cap;
+ let len = vec.len;
unsafe {
IntoIter {
@@ -80,7 +79,6 @@ impl<T> IntoIterator for Vec<T> {
} else {
ptr.as_ptr().add(len)
},
- _marker: PhantomData,
}
}
}
diff --git a/src/doc/nomicon/src/vec/vec-layout.md b/src/doc/nomicon/src/vec/vec-layout.md
index c1c1afcc7..9129952a5 100644
--- a/src/doc/nomicon/src/vec/vec-layout.md
+++ b/src/doc/nomicon/src/vec/vec-layout.md
@@ -15,13 +15,10 @@ pub struct Vec<T> {
}
```
-And indeed this would compile. Unfortunately, it would be incorrect. First, the
+And indeed this would compile. Unfortunately, it would be too strict. The
compiler will give us too strict variance. So a `&Vec<&'static str>`
-couldn't be used where an `&Vec<&'a str>` was expected. More importantly, it
-will give incorrect ownership information to the drop checker, as it will
-conservatively assume we don't own any values of type `T`. See [the chapter
-on ownership and lifetimes][ownership] for all the details on variance and
-drop check.
+couldn't be used where a `&Vec<&'a str>` was expected. See [the chapter
+on ownership and lifetimes][ownership] for all the details on variance.
As we saw in the ownership chapter, the standard library uses `Unique<T>` in place of
`*mut T` when it has a raw pointer to an allocation that it owns. Unique is unstable,
@@ -30,16 +27,16 @@ so we'd like to not use it if possible, though.
As a recap, Unique is a wrapper around a raw pointer that declares that:
* We are covariant over `T`
-* We may own a value of type `T` (for drop check)
+* We may own a value of type `T` (this is not relevant for our example here, but see
+ [the chapter on PhantomData][phantom-data] on why the real `std::vec::Vec<T>` needs this)
* We are Send/Sync if `T` is Send/Sync
* Our pointer is never null (so `Option<Vec<T>>` is null-pointer-optimized)
We can implement all of the above requirements in stable Rust. To do this, instead
of using `Unique<T>` we will use [`NonNull<T>`][NonNull], another wrapper around a
raw pointer, which gives us two of the above properties, namely it is covariant
-over `T` and is declared to never be null. By adding a `PhantomData<T>` (for drop
-check) and implementing Send/Sync if `T` is, we get the same results as using
-`Unique<T>`:
+over `T` and is declared to never be null. By implementing Send/Sync if `T` is,
+we get the same results as using `Unique<T>`:
```rust
use std::ptr::NonNull;
@@ -49,7 +46,6 @@ pub struct Vec<T> {
ptr: NonNull<T>,
cap: usize,
len: usize,
- _marker: PhantomData<T>,
}
unsafe impl<T: Send> Send for Vec<T> {}
@@ -58,4 +54,5 @@ unsafe impl<T: Sync> Sync for Vec<T> {}
```
[ownership]: ../ownership.html
+[phantom-data]: ../phantom-data.md
[NonNull]: ../../std/ptr/struct.NonNull.html
diff --git a/src/doc/nomicon/src/vec/vec-raw.md b/src/doc/nomicon/src/vec/vec-raw.md
index e86537b81..728feaa58 100644
--- a/src/doc/nomicon/src/vec/vec-raw.md
+++ b/src/doc/nomicon/src/vec/vec-raw.md
@@ -13,7 +13,6 @@ allocating, growing, and freeing:
struct RawVec<T> {
ptr: NonNull<T>,
cap: usize,
- _marker: PhantomData<T>,
}
unsafe impl<T: Send> Send for RawVec<T> {}
@@ -25,7 +24,6 @@ impl<T> RawVec<T> {
RawVec {
ptr: NonNull::dangling(),
cap: 0,
- _marker: PhantomData,
}
}
diff --git a/src/doc/nomicon/src/vec/vec-zsts.md b/src/doc/nomicon/src/vec/vec-zsts.md
index 73a97ba49..8f2529727 100644
--- a/src/doc/nomicon/src/vec/vec-zsts.md
+++ b/src/doc/nomicon/src/vec/vec-zsts.md
@@ -33,14 +33,13 @@ method of `RawVec`.
```rust,ignore
impl<T> RawVec<T> {
fn new() -> Self {
- // !0 is usize::MAX. This branch should be stripped at compile time.
- let cap = if mem::size_of::<T>() == 0 { !0 } else { 0 };
+ // This branch should be stripped at compile time.
+ let cap = if mem::size_of::<T>() == 0 { usize::MAX } else { 0 };
// `NonNull::dangling()` doubles as "unallocated" and "zero-sized allocation"
RawVec {
ptr: NonNull::dangling(),
cap: cap,
- _marker: PhantomData,
}
}