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>

source

pub const fn new(opt: &'a Option<T>) -> Self

Returns a new FmtOption that formats the provided Option value.

The fmt::opt function can be used as shorthand for this.

source

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§

source§

impl<T: Binary> Binary for FmtOption<'_, T>

source§

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

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

impl<'a, T: Clone> Clone for FmtOption<'a, T>

source§

fn clone(&self) -> FmtOption<'a, T>

Returns a copy 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<T: Debug> Debug for FmtOption<'_, T>

source§

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

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

impl<T: Display> Display for FmtOption<'_, T>

source§

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

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

impl<T: LowerHex> LowerHex for FmtOption<'_, T>

source§

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

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

impl<T: Pointer> Pointer for FmtOption<'_, T>

source§

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

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

impl<T: UpperHex> UpperHex for FmtOption<'_, T>

source§

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

Formats the value using the given formatter. Read more

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> 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§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

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

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

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

§

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>,

§

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.