Struct mycelium_util::fmt::FmtOption
source · pub struct FmtOption<'a, T> { /* private fields */ }
Expand description
A utility to assist with formatting Option
values.
This wraps a reference to an Option
value, and implements formatting
traits by formatting the Option
’s contents if it is Some
. If the
Option
is None
, the formatting trait implementations emit no text by
default, or a string provided using the or_else
method.
A FmtOption
will implement the core::fmt::Display
,
core::fmt::Debug
, core::fmt::Binary
, core::fmt::UpperHex
,
core::fmt::LowerHex
, and core::fmt::Pointer
formatting traits, if
the inner type implements the corresponding trait.
The fmt::opt
method can be used as shorthand to borrow an Option
as a FmtOption
.
§Examples
Formatting a Some
value emits that value’s Debug
and Display
output:
use mycelium_util::fmt;
let value = Some("hello world");
let debug = format!("{:?}", fmt::opt(&value));
assert_eq!(debug, "\"hello world\"");
let display = format!("{}", fmt::opt(&value));
assert_eq!(display, "hello world");
Formatting a None
value generates no text by default:
use mycelium_util::fmt;
let value: Option<&str> = None;
let debug = format!("{:?}", fmt::opt(&value));
assert_eq!(debug, "");
let display = format!("{}", fmt::opt(&value));
assert_eq!(display, "");
The or_else
method can be used to customize the text that
is emitted when the value is None
:
use mycelium_util::fmt;
use core::ptr::NonNull;
let value: Option<NonNull<u8>> = None;
let debug = format!("{:?}", fmt::opt(&value).or_else("null"));
assert_eq!(debug, "null");
Implementations§
source§impl<'a, T> FmtOption<'a, T>
impl<'a, T> FmtOption<'a, T>
sourcepub fn or_else(self, or_else: &'a str) -> Self
pub fn or_else(self, or_else: &'a str) -> Self
Set the text to emit when the value is None
.
§Examples
If the value is None
, the or_else
text is emitted:
use mycelium_util::fmt;
use core::ptr::NonNull;
let value: Option<NonNull<u8>> = None;
let debug = format!("{:?}", fmt::opt(&value).or_else("null"));
assert_eq!(debug, "null");
If the value is Some
, this function does nothing:
let value = Some("hello world");
let debug = format!("{}", fmt::opt(&value).or_else("no string"));
assert_eq!(debug, "hello world");
Trait Implementations§
Auto Trait Implementations§
impl<'a, T> Freeze for FmtOption<'a, T>
impl<'a, T> RefUnwindSafe for FmtOption<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for FmtOption<'a, T>where
T: Sync,
impl<'a, T> Sync for FmtOption<'a, T>where
T: Sync,
impl<'a, T> Unpin for FmtOption<'a, T>
impl<'a, T> UnwindSafe for FmtOption<'a, T>where
T: RefUnwindSafe,
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,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)