Skip to content

Commit

Permalink
Add some more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmcm committed Sep 24, 2022
1 parent f0dc359 commit ed16dbf
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions library/core/src/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,13 +1179,39 @@ pub const fn variant_count<T>() -> usize {
intrinsics::variant_count::<T>()
}

/// Provides associated constants for various useful properties of types,
/// to give them a canonical form in our code and make them easier to read.
///
/// This is here only to simplify all the ZST checks we need in the library.
/// It's not on a stabilization track right now.
#[doc(hidden)]
#[unstable(feature = "sized_type_properties", issue = "none")]
pub trait SizedTypeProperties: Sized {
/// `true` if this type requires no storage.
/// `false` if its [size](size_of) is greater than zero.
///
/// # Examples
///
/// ```
/// #![feature(sized_type_properties)]
/// use core::mem::SizedTypeProperties;
///
/// fn do_something_with<T>() {
/// if T::IS_ZST {
/// // ... special approach ...
/// } else {
/// // ... the normal thing ...
/// }
/// }
///
/// struct MyUnit;
/// assert!(MyUnit::IS_ZST);
///
/// // For negative checks, consider using UFCS to emphasize the negation
/// assert!(!<i32>::IS_ZST);
/// // As it can sometimes hide in the type otherwise
/// assert!(!String::IS_ZST);
/// ```
#[doc(hidden)]
#[unstable(feature = "sized_type_properties", issue = "none")]
const IS_ZST: bool = size_of::<Self>() == 0;
Expand Down

0 comments on commit ed16dbf

Please sign in to comment.