Skip to content

Commit 06d8c1d

Browse files
committed
add definition of 'allocated object', and link it from relevant method docs
1 parent 7cf4405 commit 06d8c1d

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

core/src/ptr/const_ptr.rs

+19-18
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ impl<T: ?Sized> *const T {
184184
/// Behavior:
185185
///
186186
/// * Both the starting and resulting pointer must be either in bounds or one
187-
/// byte past the end of the same allocated object. Note that in Rust,
188-
/// every (stack-allocated) variable is considered a separate allocated object.
187+
/// byte past the end of the same [allocated object].
189188
///
190189
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
191190
///
@@ -210,6 +209,7 @@ impl<T: ?Sized> *const T {
210209
/// enables more aggressive compiler optimizations.
211210
///
212211
/// [`wrapping_offset`]: #method.wrapping_offset
212+
/// [allocated object]: crate::ptr#allocated-object
213213
///
214214
/// # Examples
215215
///
@@ -245,9 +245,8 @@ impl<T: ?Sized> *const T {
245245
///
246246
/// This operation itself is always safe, but using the resulting pointer is not.
247247
///
248-
/// The resulting pointer remains attached to the same allocated object that `self` points to.
249-
/// It may *not* be used to access a different allocated object. Note that in Rust, every
250-
/// (stack-allocated) variable is considered a separate allocated object.
248+
/// The resulting pointer remains attached to the same [allocated object] that `self` points to.
249+
/// It may *not* be used to access a different allocated object.
251250
///
252251
/// In other words, `let z = x.wrapping_offset((y as isize) - (x as isize))` does *not* make `z`
253252
/// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
@@ -269,6 +268,7 @@ impl<T: ?Sized> *const T {
269268
/// do the arithmetic there.
270269
///
271270
/// [`offset`]: #method.offset
271+
/// [allocated object]: crate::ptr#allocated-object
272272
///
273273
/// # Examples
274274
///
@@ -314,8 +314,7 @@ impl<T: ?Sized> *const T {
314314
/// Behavior:
315315
///
316316
/// * Both the starting and other pointer must be either in bounds or one
317-
/// byte past the end of the same allocated object. Note that in Rust,
318-
/// every (stack-allocated) variable is considered a separate allocated object.
317+
/// byte past the end of the same [allocated object].
319318
///
320319
/// * Both pointers must be *derived from* a pointer to the same object.
321320
/// (See below for an example.)
@@ -345,6 +344,7 @@ impl<T: ?Sized> *const T {
345344
/// such large allocations either.)
346345
///
347346
/// [`add`]: #method.add
347+
/// [allocated object]: crate::ptr#allocated-object
348348
///
349349
/// # Panics
350350
///
@@ -468,8 +468,7 @@ impl<T: ?Sized> *const T {
468468
/// Behavior:
469469
///
470470
/// * Both the starting and resulting pointer must be either in bounds or one
471-
/// byte past the end of the same allocated object. Note that in Rust,
472-
/// every (stack-allocated) variable is considered a separate allocated object.
471+
/// byte past the end of the same [allocated object].
473472
///
474473
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
475474
///
@@ -494,6 +493,7 @@ impl<T: ?Sized> *const T {
494493
/// enables more aggressive compiler optimizations.
495494
///
496495
/// [`wrapping_add`]: #method.wrapping_add
496+
/// [allocated object]: crate::ptr#allocated-object
497497
///
498498
/// # Examples
499499
///
@@ -532,8 +532,7 @@ impl<T: ?Sized> *const T {
532532
/// Behavior:
533533
///
534534
/// * Both the starting and resulting pointer must be either in bounds or one
535-
/// byte past the end of the same allocated object. Note that in Rust,
536-
/// every (stack-allocated) variable is considered a separate allocated object.
535+
/// byte past the end of the same [allocated object].
537536
///
538537
/// * The computed offset cannot exceed `isize::MAX` **bytes**.
539538
///
@@ -558,6 +557,7 @@ impl<T: ?Sized> *const T {
558557
/// enables more aggressive compiler optimizations.
559558
///
560559
/// [`wrapping_sub`]: #method.wrapping_sub
560+
/// [allocated object]: crate::ptr#allocated-object
561561
///
562562
/// # Examples
563563
///
@@ -594,9 +594,8 @@ impl<T: ?Sized> *const T {
594594
///
595595
/// This operation itself is always safe, but using the resulting pointer is not.
596596
///
597-
/// The resulting pointer remains attached to the same allocated object that `self` points to.
598-
/// It may *not* be used to access a different allocated object. Note that in Rust, every
599-
/// (stack-allocated) variable is considered a separate allocated object.
597+
/// The resulting pointer remains attached to the same [allocated object] that `self` points to.
598+
/// It may *not* be used to access a different allocated object.
600599
///
601600
/// In other words, `let z = x.wrapping_add((y as usize) - (x as usize))` does *not* make `z`
602601
/// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
@@ -618,6 +617,7 @@ impl<T: ?Sized> *const T {
618617
/// do the arithmetic there.
619618
///
620619
/// [`add`]: #method.add
620+
/// [allocated object]: crate::ptr#allocated-object
621621
///
622622
/// # Examples
623623
///
@@ -659,9 +659,8 @@ impl<T: ?Sized> *const T {
659659
///
660660
/// This operation itself is always safe, but using the resulting pointer is not.
661661
///
662-
/// The resulting pointer remains attached to the same allocated object that `self` points to.
663-
/// It may *not* be used to access a different allocated object. Note that in Rust, every
664-
/// (stack-allocated) variable is considered a separate allocated object.
662+
/// The resulting pointer remains attached to the same [allocated object] that `self` points to.
663+
/// It may *not* be used to access a different allocated object.
665664
///
666665
/// In other words, `let z = x.wrapping_sub((x as usize) - (y as usize))` does *not* make `z`
667666
/// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
@@ -683,6 +682,7 @@ impl<T: ?Sized> *const T {
683682
/// do the arithmetic there.
684683
///
685684
/// [`sub`]: #method.sub
685+
/// [allocated object]: crate::ptr#allocated-object
686686
///
687687
/// # Examples
688688
///
@@ -997,7 +997,7 @@ impl<T> *const [T] {
997997
/// * The pointer must be [valid] for reads for `ptr.len() * mem::size_of::<T>()` many bytes,
998998
/// and it must be properly aligned. This means in particular:
999999
///
1000-
/// * The entire memory range of this slice must be contained within a single allocated object!
1000+
/// * The entire memory range of this slice must be contained within a single [allocated object]!
10011001
/// Slices can never span across multiple allocated objects.
10021002
///
10031003
/// * The pointer must be aligned even for zero-length slices. One
@@ -1019,6 +1019,7 @@ impl<T> *const [T] {
10191019
/// See also [`slice::from_raw_parts`][].
10201020
///
10211021
/// [valid]: crate::ptr#safety
1022+
/// [allocated object]: crate::ptr#allocated-object
10221023
#[inline]
10231024
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
10241025
pub unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {

core/src/ptr/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
//! has size 0, i.e., even if memory is not actually touched. Consider using
5656
//! [`NonNull::dangling`] in such cases.
5757
//!
58+
//! ## Allocated object
59+
//!
60+
//! For several operations, such as [`offset`] or field projections (`expr.field`), the notion of an
61+
//! "allocated object" becomes relevant. An allocated object is a contiguous region of memory.
62+
//! Common examples of allocated objects include stack-allocated variables (each variable is a
63+
//! separate allocated object), heap allocations (each allocation created by the global allocator is
64+
//! a separate allocated object), and `static` variables.
65+
//!
5866
//! [aliasing]: ../../nomicon/aliasing.html
5967
//! [book]: ../../book/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer
6068
//! [ub]: ../../reference/behavior-considered-undefined.html

core/src/ptr/mut_ptr.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ impl<T: ?Sized> *mut T {
189189
/// Behavior:
190190
///
191191
/// * Both the starting and resulting pointer must be either in bounds or one
192-
/// byte past the end of the same allocated object. Note that in Rust,
193-
/// every (stack-allocated) variable is considered a separate allocated object.
192+
/// byte past the end of the same [allocated object].
194193
///
195194
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
196195
///
@@ -215,6 +214,7 @@ impl<T: ?Sized> *mut T {
215214
/// enables more aggressive compiler optimizations.
216215
///
217216
/// [`wrapping_offset`]: #method.wrapping_offset
217+
/// [allocated object]: crate::ptr#allocated-object
218218
///
219219
/// # Examples
220220
///
@@ -251,9 +251,8 @@ impl<T: ?Sized> *mut T {
251251
///
252252
/// This operation itself is always safe, but using the resulting pointer is not.
253253
///
254-
/// The resulting pointer remains attached to the same allocated object that `self` points to.
255-
/// It may *not* be used to access a different allocated object. Note that in Rust, every
256-
/// (stack-allocated) variable is considered a separate allocated object.
254+
/// The resulting pointer remains attached to the same [allocated object] that `self` points to.
255+
/// It may *not* be used to access a different allocated object.
257256
///
258257
/// In other words, `let z = x.wrapping_offset((y as isize) - (x as isize))` does *not* make `z`
259258
/// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
@@ -275,6 +274,7 @@ impl<T: ?Sized> *mut T {
275274
/// do the arithmetic there.
276275
///
277276
/// [`offset`]: #method.offset
277+
/// [allocated object]: crate::ptr#allocated-object
278278
///
279279
/// # Examples
280280
///
@@ -485,8 +485,7 @@ impl<T: ?Sized> *mut T {
485485
/// Behavior:
486486
///
487487
/// * Both the starting and other pointer must be either in bounds or one
488-
/// byte past the end of the same allocated object. Note that in Rust,
489-
/// every (stack-allocated) variable is considered a separate allocated object.
488+
/// byte past the end of the same [allocated object].
490489
///
491490
/// * Both pointers must be *derived from* a pointer to the same object.
492491
/// (See below for an example.)
@@ -516,6 +515,7 @@ impl<T: ?Sized> *mut T {
516515
/// such large allocations either.)
517516
///
518517
/// [`add`]: #method.add
518+
/// [allocated object]: crate::ptr#allocated-object
519519
///
520520
/// # Panics
521521
///
@@ -575,8 +575,7 @@ impl<T: ?Sized> *mut T {
575575
/// Behavior:
576576
///
577577
/// * Both the starting and resulting pointer must be either in bounds or one
578-
/// byte past the end of the same allocated object. Note that in Rust,
579-
/// every (stack-allocated) variable is considered a separate allocated object.
578+
/// byte past the end of the same [allocated object].
580579
///
581580
/// * The computed offset, **in bytes**, cannot overflow an `isize`.
582581
///
@@ -639,8 +638,7 @@ impl<T: ?Sized> *mut T {
639638
/// Behavior:
640639
///
641640
/// * Both the starting and resulting pointer must be either in bounds or one
642-
/// byte past the end of the same allocated object. Note that in Rust,
643-
/// every (stack-allocated) variable is considered a separate allocated object.
641+
/// byte past the end of the same [allocated object].
644642
///
645643
/// * The computed offset cannot exceed `isize::MAX` **bytes**.
646644
///
@@ -665,6 +663,7 @@ impl<T: ?Sized> *mut T {
665663
/// enables more aggressive compiler optimizations.
666664
///
667665
/// [`wrapping_sub`]: #method.wrapping_sub
666+
/// [allocated object]: crate::ptr#allocated-object
668667
///
669668
/// # Examples
670669
///
@@ -701,9 +700,8 @@ impl<T: ?Sized> *mut T {
701700
///
702701
/// This operation itself is always safe, but using the resulting pointer is not.
703702
///
704-
/// The resulting pointer remains attached to the same allocated object that `self` points to.
705-
/// It may *not* be used to access a different allocated object. Note that in Rust, every
706-
/// (stack-allocated) variable is considered a separate allocated object.
703+
/// The resulting pointer remains attached to the same [allocated object] that `self` points to.
704+
/// It may *not* be used to access a different allocated object.
707705
///
708706
/// In other words, `let z = x.wrapping_add((y as usize) - (x as usize))` does *not* make `z`
709707
/// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
@@ -725,6 +723,7 @@ impl<T: ?Sized> *mut T {
725723
/// do the arithmetic there.
726724
///
727725
/// [`add`]: #method.add
726+
/// [allocated object]: crate::ptr#allocated-object
728727
///
729728
/// # Examples
730729
///
@@ -766,9 +765,8 @@ impl<T: ?Sized> *mut T {
766765
///
767766
/// This operation itself is always safe, but using the resulting pointer is not.
768767
///
769-
/// The resulting pointer remains attached to the same allocated object that `self` points to.
770-
/// It may *not* be used to access a different allocated object. Note that in Rust, every
771-
/// (stack-allocated) variable is considered a separate allocated object.
768+
/// The resulting pointer remains attached to the same [allocated object] that `self` points to.
769+
/// It may *not* be used to access a different allocated object.
772770
///
773771
/// In other words, `let z = x.wrapping_sub((x as usize) - (y as usize))` does *not* make `z`
774772
/// the same as `y` even if we assume `T` has size `1` and there is no overflow: `z` is still
@@ -790,6 +788,7 @@ impl<T: ?Sized> *mut T {
790788
/// do the arithmetic there.
791789
///
792790
/// [`sub`]: #method.sub
791+
/// [allocated object]: crate::ptr#allocated-object
793792
///
794793
/// # Examples
795794
///
@@ -1261,7 +1260,7 @@ impl<T> *mut [T] {
12611260
/// * The pointer must be [valid] for reads for `ptr.len() * mem::size_of::<T>()` many bytes,
12621261
/// and it must be properly aligned. This means in particular:
12631262
///
1264-
/// * The entire memory range of this slice must be contained within a single allocated object!
1263+
/// * The entire memory range of this slice must be contained within a single [allocated object]!
12651264
/// Slices can never span across multiple allocated objects.
12661265
///
12671266
/// * The pointer must be aligned even for zero-length slices. One
@@ -1283,6 +1282,7 @@ impl<T> *mut [T] {
12831282
/// See also [`slice::from_raw_parts`][].
12841283
///
12851284
/// [valid]: crate::ptr#safety
1285+
/// [allocated object]: crate::ptr#allocated-object
12861286
#[inline]
12871287
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
12881288
pub unsafe fn as_uninit_slice<'a>(self) -> Option<&'a [MaybeUninit<T>]> {
@@ -1311,7 +1311,7 @@ impl<T> *mut [T] {
13111311
/// * The pointer must be [valid] for reads and writes for `ptr.len() * mem::size_of::<T>()`
13121312
/// many bytes, and it must be properly aligned. This means in particular:
13131313
///
1314-
/// * The entire memory range of this slice must be contained within a single allocated object!
1314+
/// * The entire memory range of this slice must be contained within a single [allocated object]!
13151315
/// Slices can never span across multiple allocated objects.
13161316
///
13171317
/// * The pointer must be aligned even for zero-length slices. One
@@ -1333,6 +1333,7 @@ impl<T> *mut [T] {
13331333
/// See also [`slice::from_raw_parts_mut`][].
13341334
///
13351335
/// [valid]: crate::ptr#safety
1336+
/// [allocated object]: crate::ptr#allocated-object
13361337
#[inline]
13371338
#[unstable(feature = "ptr_as_uninit", issue = "75402")]
13381339
pub unsafe fn as_uninit_slice_mut<'a>(self) -> Option<&'a mut [MaybeUninit<T>]> {

0 commit comments

Comments
 (0)