]> git.lizzy.rs Git - rust.git/commitdiff
More doc-comments for pointer metadata APIs
authorSimon Sapin <simon.sapin@exyr.org>
Mon, 18 Jan 2021 18:45:32 +0000 (19:45 +0100)
committerSimon Sapin <simon.sapin@exyr.org>
Mon, 15 Feb 2021 13:27:34 +0000 (14:27 +0100)
library/core/src/ptr/metadata.rs
library/core/src/ptr/non_null.rs

index 0661ccc020014864d335e1fe011f72ac5049cffd..f89e891cb8657a84bf3081d0994e2d3d8e754953 100644 (file)
@@ -3,7 +3,52 @@
 use crate::fmt;
 use crate::hash::{Hash, Hasher};
 
-/// FIXME docs
+/// Provides the pointer metadata type of any pointed-to type.
+///
+/// # Pointer metadata
+///
+/// Raw pointer types and reference types in Rust can be thought of as made of two parts:
+/// a data pointer that contains the memory address of the value, and some metadata.
+///
+/// For statically-sized types (that implement the `Sized` traits)
+/// as well as for `extern` types,
+/// pointers are said to be “thin”: metadata is zero-sized and its type is `()`.
+///
+/// Pointers to [dynamically-sized types][dst] are said to be “wide” or “fat”,
+/// they have non-zero-sized metadata:
+///
+/// * For structs whose last field is a DST, metadata is the metadata for the last field
+/// * For the `str` type, metadata is the length in bytes as `usize`
+/// * For slice types like `[T]`, metadata is the length in items as `usize`
+/// * For trait objects like `dyn SomeTrait`, metadata is [`DynMetadata<Self>`][DynMetadata]
+///   (e.g. `DynMetadata<dyn SomeTrait>`)
+///
+/// In the future, the Rust language may gain new kinds of types
+/// that have different pointer metadata.
+///
+/// [dst]: https://doc.rust-lang.org/nomicon/exotic-sizes.html#dynamically-sized-types-dsts
+///
+///
+/// # The `Pointee` trait
+///
+/// The point of this trait is its `Metadata` associated type,
+/// which is `()` or `usize` or `DynMetadata<_>` as described above.
+/// It is automatically implemented for every type.
+/// It can be assumed to be implemented in a generic context, even without a corresponding bound.
+///
+///
+/// # Usage
+///
+/// Raw pointers can be decomposed into the data address and metadata components
+/// with their [`to_raw_parts`] method.
+///
+/// Alternatively, metadata alone can be extracted with the [`metadata`] function.
+/// A reference can be passed to [`metadata`] and implicitly coerced.
+///
+/// A (possibly-wide) pointer can be put back together from its address and metadata
+/// with [`from_raw_parts`] or [`from_raw_parts_mut`].
+///
+/// [`to_raw_parts`]: <*const _>::to_raw_parts
 #[lang = "pointee_trait"]
 pub trait Pointee {
     /// The type for metadata in pointers and references to `Self`.
@@ -14,7 +59,11 @@ pub trait Pointee {
     type Metadata: Copy + Send + Sync + Ord + Hash + Unpin;
 }
 
-/// Pointers to types implementing this trait alias are “thin”
+/// Pointers to types implementing this trait alias are “thin”.
+///
+/// This includes statically-`Sized` types and `extern` types.
+///
+/// # Example
 ///
 /// ```rust
 /// #![feature(ptr_metadata)]
@@ -28,6 +77,17 @@ pub trait Pointee {
 pub trait Thin = Pointee<Metadata = ()>;
 
 /// Extract the metadata component of a pointer.
+///
+/// Values of type `*mut T`, `&T`, or `&mut T` can be passed directly to this function
+/// as they implicitly coerce to `*const T`.
+///
+/// # Example
+///
+/// ```
+/// #![feature(ptr_metadata)]
+///
+/// assert_eq!(std::ptr::metadata("foo"), 3_usize);
+/// ```
 #[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
 #[inline]
 pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
@@ -37,7 +97,13 @@ pub const fn metadata<T: ?Sized>(ptr: *const T) -> <T as Pointee>::Metadata {
     unsafe { PtrRepr { const_ptr: ptr }.components.metadata }
 }
 
-/// Forms a raw pointer from a data address and metadata.
+/// Forms a (possibly-wide) raw pointer from a data address and metadata.
+///
+/// This function is safe but the returned pointer is not necessarily safe to dereference.
+/// For slices, see the documentation of [`slice::from_raw_parts`] for safety requirements.
+/// For trait objects, the metadata must come from a pointer to the same underlying ereased type.
+///
+/// [`slice::from_raw_parts`]: crate::slice::from_raw_parts
 #[unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
 #[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
 #[inline]
@@ -91,7 +157,23 @@ fn clone(&self) -> Self {
     }
 }
 
-/// The metadata for a `dyn SomeTrait` trait object type.
+/// The metadata for a `Dyn = dyn SomeTrait` trait object type.
+///
+/// It is a pointer to a vtable (virtual call table)
+/// that represents all the necessary information
+/// to manipulate the concrete type stored inside a trait object.
+/// The vtable notably it contains:
+///
+/// * type size
+/// * type alignment
+/// * a pointer to the type’s `drop_in_place` impl (may be a no-op for plain-old-data)
+/// * pointers to all the methods for the type’s implementation of the trait
+///
+/// Note that the first three are special because they’re necessary to allocate, drop,
+/// and deallocate any trait object.
+///
+/// It is possible to name this struct with a type parameter that is not a `dyn` trait object
+/// (for example `DynMetadata<u64>`) but not to obtain a meaningful value of that struct.
 #[lang = "dyn_metadata"]
 pub struct DynMetadata<Dyn: ?Sized> {
     vtable_ptr: &'static VTable,
index 00cb1e1b271450ea19e32abb03a924c62bd3ddbc..3de5b097f5e34a56298aca6057e6a180474e9400 100644 (file)
@@ -179,6 +179,8 @@ pub fn new(ptr: *mut T) -> Option<Self> {
     /// `NonNull` pointer is returned, as opposed to a raw `*const` pointer.
     ///
     /// See the documentation of [`std::ptr::from_raw_parts`] for more details.
+    ///
+    /// [`std::ptr::from_raw_parts`]: crate::ptr::from_raw_parts
     #[cfg(not(bootstrap))]
     #[unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]
     #[rustc_const_unstable(feature = "ptr_metadata", issue = /* FIXME */ "none")]