pub trait ContextualDisplay<Context> {
    type Error;

    // Required method
    fn contextual_format<F>(
        &self,
        f: &mut F,
        context: &Context
    ) -> Result<(), Self::Error>
       where F: Write;

    // Provided methods
    fn format<F, TContext>(
        &self,
        f: &mut F,
        context: TContext
    ) -> Result<(), Self::Error>
       where F: Write,
             TContext: Into<Context> { ... }
    fn display<TContext, 'a, 'b>(
        &'a self,
        context: TContext
    ) -> ContextDisplayable<'a, Self, Context>
       where TContext: Into<Context> { ... }
    fn to_string<TContext, 'a, 'b>(&'a self, context: TContext) -> String
       where TContext: Into<Context> { ... }
}
Expand description

This trait is used where context is required to correctly display a value.

Typically, this is due to needing to know the current network to display addresses. Other forms of Context are also possible. See ComponentAddress or TransactionReceipt in the radix-engine crate for example implementations.

The Context used should typically just be a wrapper type around references, and so be a small, cheap, ephemeral value on the stack (if it’s not just optimized away entirely). It is therefore recommended that the Context implement Copy, to make it very easy to pass around and re-use.

Required Associated Types§

type Error

Required Methods§

fn contextual_format<F>( &self, f: &mut F, context: &Context ) -> Result<(), Self::Error>
where F: Write,

Formats the value to the given fmt::Write buffer, making use of the provided context. See also format, which is typically easier to use, as it takes an Into<Context> instead of a &Context.

Provided Methods§

fn format<F, TContext>( &self, f: &mut F, context: TContext ) -> Result<(), Self::Error>
where F: Write, TContext: Into<Context>,

Formats the value to the given fmt::Write buffer, making use of the provided context. See also contextual_format, which takes a &Context instead of an Into<Context>.

Alternatively, the display method can be used to create an object that can be used directly in a format! style macro.

fn display<TContext, 'a, 'b>( &'a self, context: TContext ) -> ContextDisplayable<'a, Self, Context>
where TContext: Into<Context>,

Returns an object implementing fmt::Display, which can be used in a format! style macro.

Whilst this is syntactically nicer, beware that the use of format! absorbs any errors during formatting, replacing them with fmt::Error. If you’d like to preserve errors, use the format method instead. This may require manually splitting up your format! style macro. For example:

// Syntactically nice, but the AddressError is swallowed into fmt::Error
write!(f, "ComponentAddress(\"{}\")", address.display(context))?;

// Less nice, but the AddressError is correctly returned
f.write_str("ComponentAddress(\"")?;
address.format(f, context)?;
f.write_str("\")")?;

fn to_string<TContext, 'a, 'b>(&'a self, context: TContext) -> String
where TContext: Into<Context>,

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for Emitter

§

type Error = Error

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for NonFungibleGlobalId

§

type Error = Error

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for BlueprintId

§

type Error = Error

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for ComponentAddress

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for FnIdentifier

§

type Error = Error

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for GlobalAddress

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for InternalAddress

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for NodeId

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for PackageAddress

source§

impl<'a> ContextualDisplay<AddressDisplayContext<'a>> for ResourceAddress

source§

impl<'s, 'a> ContextualDisplay<ValueDisplayParameters<'s, 'a, ScryptoCustomExtension>> for IndexedScryptoValue

source§

impl<'s, 'a, 'b, E> ContextualDisplay<ValueDisplayParameters<'s, 'a, E>> for RawPayload<'b, E>

source§

impl<'s, 'a, 'b, E> ContextualDisplay<ValueDisplayParameters<'s, 'a, E>> for RawValue<'b, E>