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
impl DynMessage
Sourcepub fn new(x: impl AnyMessage) -> Self
pub fn new(x: impl AnyMessage) -> Self
Utility to make a DynMessage
from a message value.
Sourcepub fn downcast<T: AnyMessage>(self) -> Result<Box<T>, Self>
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.
Sourcepub fn is<T: AnyMessage>(&self) -> bool
pub fn is<T: AnyMessage>(&self) -> bool
Returns true
if the inner type is the same as T
.