diff options
author | Marcel Müller <m.mueller@ifm.com> | 2022-05-05 11:15:23 +0200 |
---|---|---|
committer | Marcel Müller <m.mueller@ifm.com> | 2022-05-05 15:07:19 +0200 |
commit | 39191b04a092cd59bb8ee4d7a43e9a0e07941bca (patch) | |
tree | c63298cc778232fd9e8fe9d1f49c3444f30efafc /crates | |
parent | dd0582db837cb5d92612a223e3c44b04a695eb4d (diff) |
Add the ability to check if a msg can be received
Signed-off-by: Marcel Müller <m.mueller@ifm.com>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/core/tedge_api/src/address.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/crates/core/tedge_api/src/address.rs b/crates/core/tedge_api/src/address.rs index 93a51450..ef50edb4 100644 --- a/crates/core/tedge_api/src/address.rs +++ b/crates/core/tedge_api/src/address.rs @@ -168,6 +168,16 @@ impl<RB: ReceiverBundle> Address<RB> { reply_recv: receiver, }) } + + /// Whether this Address could potentially receive this message. + /// + /// This does a check whether the [`ReceiverBundle`] contains the type of the message. + pub fn could_receive(&self, msg: &dyn Message) -> bool { + let types = RB::get_ids(); + let msg_type = MessageType::from_message(msg); + + types.iter().any(|ty| ty.satisfy(&msg_type)) + } } #[derive(Debug)] @@ -331,6 +341,11 @@ mod tests { impl Message for Bar {} + #[derive(Debug)] + struct Blub; + + impl Message for Blub {} + make_receiver_bundle!(struct FooBar(Foo, Bar)); #[allow(unreachable_code, dead_code, unused)] @@ -352,4 +367,16 @@ mod tests { assert_not_impl_any!(NotSync: Send, Sync); assert_impl_all!(ReplySenderFor<NotSync>: Send, Sync); assert_impl_all!(ReplyReceiverFor<NotSync>: Send, Sync); + + #[test] + fn check_could_receive() { + let (sender, _receiver) = tokio::sync::mpsc::channel(1); + let addr: Address<FooBar> = Address { + _pd: std::marker::PhantomData, + sender, + }; + assert!(addr.could_receive(&Foo)); + assert!(addr.could_receive(&Bar)); + assert!(!addr.could_receive(&Blub)); + } } |