#[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>
impl<'a> MaybeDebug<'a>
Sourcepub fn has_debug_info(&self) -> bool
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
Sourcepub fn is_fallback(&self) -> bool
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.
Sourcepub fn is_known_slice(&self) -> bool
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.
Sourcepub fn original_slice_len(&self) -> Option<usize>
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.
Sourcepub fn type_name(&self) -> &'static str
pub fn type_name(&self) -> &'static str
Return the underling type name,
as given by core::any::type_name
This function always succeeds.
Sourcepub fn passthrough<T: Debug + 'a>(val: &'a T) -> Self
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.
Sourcepub fn passthrough_str(val: &'a str) -> Self
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
Sourcepub fn passthrough_slice<T: Debug + 'a>(val: &'a [T]) -> Self
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.
Sourcepub fn fallback_slice<T>(val: &'a [T]) -> Self
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.
Sourcepub fn fallback<T: ?Sized>() -> Self
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>
impl<'a> Clone for MaybeDebug<'a>
Source§fn clone(&self) -> MaybeDebug<'a>
fn clone(&self) -> MaybeDebug<'a>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more