MaybeDebug

Enum MaybeDebug 

Source
#[non_exhaustive]
pub enum MaybeDebug<'a> { }
Expand description

Optional presense of Debug information (equivalent to Option<&dyn Debug>)

The main difference from the equivalent Option is that it prints a reasonable fallback (the type’s name).

In other words maybe_debug(NotDebug) gives "NotDebug { ... } instead of just printing None.

You can always retrieve the original type name of the value, regardless of whether the Debug implementation is Some or None.

The type name is the same one given by core::any::type_name.

The specific variants of this struct are considered an implementation detail.

Implementations§

Source§

impl<'a> MaybeDebug<'a>

Source

pub fn has_debug_info(&self) -> bool

Check if this type contians an underlying Debug implementation.

Using the Option<&dyn Debug> analogy, this would be equivalent to is_some

Source

pub fn is_fallback(&self) -> bool

Check if this type is using a fallback implementation (based on the type name) (ie !self.has_debug_info())

Note this returns true even for MaybeDebug::fallback_slice, despite the fact that does include length information.

Using the Option<&dyn Debug> analogy, this would be equivalent to is_none.

Source

pub fn is_known_slice(&self) -> bool

Check if this type is known to be a slice.

This function has false negatives, but no false positives.

For example, if MaybeDebug::fallback is used, then this function will return false even if fallback is given a slice.

Source

pub fn original_slice_len(&self) -> Option<usize>

If the original value was a slice, return its length.

Returns None if the original value was not known to be a slice.

Just like MaybeDebug::is_known_slice, this may have false negatives on whether or not something is a slice. However, if it returns Some, then the length is guarenteed to be correct.

Source

pub fn type_name(&self) -> &'static str

Return the underling type name, as given by core::any::type_name

This function always succeeds.

Source

pub fn passthrough<T: Debug + 'a>(val: &'a T) -> Self

Construct a “passthrough” ‘MaybeDebug’, that will simply delegate to the underlying implementation.

On nightly, this is equivalent to maybe_debug. However, on stable, maybe_debug cannot specialize and will unconditionally call MaybeDebug::fallback.

Therefore, this may be useful on stable rust if you need to create a ‘MaybeDebug’ value from a type that is unconditionally known to implement Debug.

Source

pub fn passthrough_str(val: &'a str) -> Self

Construct a “passthrough” MaybeDebug, that will simply delegate to str’s Debug implementation.

Note that a regular passthrough cannot work because str: !Sized

Source

pub fn passthrough_slice<T: Debug + 'a>(val: &'a [T]) -> Self

Construct a “passthrough” MaybeDebug that will simply Debug each individual value in the slice.

On nightly, this is equivalent to maybe_debug. However, on stable, maybe_debug cannot specialize and will unconditionally call MaybeDebug::fallback.

Even worse, it cannot even detect the fact the type is a slice.

NOTE (for stable only): Until RFC #2580 is stabilized, this function will unconditionally delegate to MaybeDebug::fallback_slice on the stable compiler. This is still better than maybe_debug (or a plain fallback), since it also prints the length. Users using nightly can ignore this warning.

See also MaybeDebug::passthrough and MaybeDebug::fallback_slice.

Source

pub fn fallback_slice<T>(val: &'a [T]) -> Self

Unconditionally use the “fallback” implementation for the specified slice.

The current implementation simply prints the name and the slice length.

On stable this actually gives more information than maybe_debug. This is because maybe_debug cannot specialize to slices, so it simply prints the type name without any length information.

On nightly, this is strictly worse than maybe_debug, because it cannot specialize to the case that [T]: Debug.

Source

pub fn fallback<T: ?Sized>() -> Self

A fallback implementation that unconditionally prints the type name.

For example fallback<Option<usize>>() would print "std::option::Option<usize> { ... }"

On stable, maybe_debug unconditionally delegates to this function. On nightly, this is used if T is !Debug (and isn’t a slice).

However on nightly, if T is Debug it is strictly worse than maybe_debug because it cannot take advantage of any Debug implementation for T.

There is really almost no scenario in which you want to use this function. core::any::type_name expresses the intent of printing the type name more clearly and maybe_debug is able to take advantage of specialization on the nightly compiler.

It is included only for completeness. NOTE: It doesn’t actually require a value for T, since it only uses its type name.

See also Self::fallback_slice which also prints length information (which may actually be useful)

Trait Implementations§

Source§

impl<'a> Clone for MaybeDebug<'a>

Source§

fn clone(&self) -> MaybeDebug<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for MaybeDebug<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Copy for MaybeDebug<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for MaybeDebug<'a>

§

impl<'a> !RefUnwindSafe for MaybeDebug<'a>

§

impl<'a> !Send for MaybeDebug<'a>

§

impl<'a> !Sync for MaybeDebug<'a>

§

impl<'a> Unpin for MaybeDebug<'a>

§

impl<'a> !UnwindSafe for MaybeDebug<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.