Struct DynMessage

Source
pub struct DynMessage(pub Box<dyn AnyMessage>);
Expand description

A simple dynamically typed message for the View trait.

This is a thin wrapper around Box<dyn Any>, with added support for debug printing. It is used as the default message type in Xilem Core. The contained messages must also be Send, which makes using this message type in a multithreaded context easier. View is generic over the message type, in case this requirement is too restrictive. Indeed, this functionality is used in Xilem Web.

To convert a DynMessage into its concrete message type, you should use downcast.

This type is a struct rather than (say) a type alias, because type aliases are sometimes resolved by rust-analyzer when autofilling a trait, which can also lead to buggy behaviour (we’ve previously seen Box<dyn Box<dyn Message>> be generated).

If the message contains sensitive data, make sure this isn’t output in its Debug implementation, as that may be called by the Xilem runtime (e.g. due to a bug meaning messages are redirected) or any parent views. (That is, views do not need to design themselves as if the Debug implementation is )

Tuple Fields§

§0: Box<dyn AnyMessage>

Implementations§

Source§

impl DynMessage

Source

pub fn new(x: impl AnyMessage) -> Self

Utility to make a DynMessage from a message value.

Source

pub fn downcast<T: AnyMessage>(self) -> Result<Box<T>, Self>

Access the actual type of this DynMessage.

§Errors

If the message contained within self is not of type T, returns self (so that e.g. a different type can be used).

In most cases, to handle this error, you will want to make an error log, and return this as MessageResult::Stale; this case indicates that a parent view has routed things incorrectly, but it’s reasonable to be robust.

Source

pub fn is<T: AnyMessage>(&self) -> bool

Returns true if the inner type is the same as T.

Trait Implementations§

Source§

impl Debug for DynMessage

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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, 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> AnyMessage for T
where T: Any + Debug + Send,