pub struct Discriminant<T>(<T as DiscriminantKind>::Discriminant);Expand description
Opaque type representing the discriminant of an enum.
See the discriminant function in this module for more information.
Tuple Fields§
§0: <T as DiscriminantKind>::DiscriminantImplementations§
Source§impl<T> Discriminant<T>
impl<T> Discriminant<T>
Sourcepub const fn ord(&self) -> DiscriminantOrd<T>
🔬This is a nightly-only experimental API. (discriminant_ord #165438)
pub const fn ord(&self) -> DiscriminantOrd<T>
discriminant_ord #165438)Returns an ordering key for this Discriminant that has an unstable
implementation with regards to semantic versioning. This is not to be
confused with an [“unstable sort”].
Because all types have a publicly accessible Discriminant, regardless
of whether the order of said type is a part of its stability guarantees,
the exact ordering of the returned DiscriminantOrd cannot be relied on
for stability across crate boundaries. This is similar to the stability
of the size of a type as returned by size_of.
Discriminants from enums will be ordered based on the source declaration
and assigned values of their variants, consistent with derived implementations
of PartialOrd and Ord. All other kinds of discriminants have an
unspecified ordering, but will typically be trivially ordered as equivalent.
Users of this function should instead rely on the Ord implementation
of T wherever possible.
§Examples
#![feature(discriminant_ord)]
use std::cmp::Ordering;
use std::mem::discriminant;
enum Foo { A(i32), B(i32) }
// Equivalent variants will compare equal regardless of contained data
assert_matches!(
discriminant(&Foo::A(1)).ord().cmp(&discriminant(&Foo::A(2)).ord()),
Ordering::Equal,
);
// Non-equivalent variants will compare not-equal
assert_matches!(
discriminant(&Foo::A(1)).ord().cmp(&discriminant(&Foo::B(1)).ord()),
Ordering::Less | Ordering::Greater,
);Upstream libraries and the compiler are free to change the exact ordering of discriminants unless they are explicitly documented as stable. The below may break at any time:
// Foo::A is declared before Foo::B, but that ordering is not stable
assert_matches!(
discriminant(&Foo::A(0)).ord().cmp(&discriminant(&Foo::B(0)).ord()),
Ordering::Less,
);Trait Implementations§
1.21.0 · Source§impl<T> Clone for Discriminant<T>
impl<T> Clone for Discriminant<T>
impl<T> Copy for Discriminant<T>
1.21.0 · Source§impl<T> Debug for Discriminant<T>
impl<T> Debug for Discriminant<T>
1.21.0 · Source§impl<T> Eq for Discriminant<T>
impl<T> Eq for Discriminant<T>
1.0.0 (const: unstable) · Source§#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
#[doc(hidden)]fn assert_receiver_is_total_eq(&self)
implementation detail of #[derive(Eq)]
Source§#[doc(hidden)]fn assert_fields_are_eq(&self)
#[doc(hidden)]fn assert_fields_are_eq(&self)
derive_eq_internals)1.21.0 · Source§impl<T> Hash for Discriminant<T>
impl<T> Hash for Discriminant<T>
1.21.0 · Source§impl<T> PartialEq for Discriminant<T>
impl<T> PartialEq for Discriminant<T>
impl<T> TrivialClone for Discriminant<T>
Auto Trait Implementations§
impl<T> Forget for Discriminant<T>
impl<T> Freeze for Discriminant<T>
impl<T> Move for Discriminant<T>
impl<T> RefUnwindSafe for Discriminant<T>
impl<T> Send for Discriminant<T>
impl<T> Sync for Discriminant<T>
impl<T> Unpin for Discriminant<T>
impl<T> UnsafeUnpin for Discriminant<T>
impl<T> UnwindSafe for Discriminant<T>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> Printable for T
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize
#[doc(hidden)]const SIZE: usize
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize
#[doc(hidden)]const ALIGN: usize
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment
#[doc(hidden)]const ALIGNMENT: Alignment
sized_type_properties)Source§#[doc(hidden)]const IS_ZST: bool
#[doc(hidden)]const IS_ZST: bool
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout
#[doc(hidden)]const LAYOUT: Layout
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize
#[doc(hidden)]const MAX_SLICE_LEN: usize
sized_type_properties)[Self]. Read more