diff options
author | Marcel Müller <m.mueller@ifm.com> | 2022-05-12 11:48:23 +0200 |
---|---|---|
committer | Marcel Müller <m.mueller@ifm.com> | 2022-05-31 11:24:03 +0200 |
commit | 519ec731579a226aac53cec65c6b20cacfd3f586 (patch) | |
tree | 5aa990ac60e6117d18659132991f6341c8fe1ecb | |
parent | 02bfa3768be59b211daddd9b66203b787fd72153 (diff) |
Stop using TypeId and instead use TypeUuid
This patch completes the switch from the unstable TypeId to a stable
TypeUuid. This ensures that message types can be identified beyond
compiler versions and Rust itself.
Signed-off-by: Marcel Müller <m.mueller@ifm.com>
-rw-r--r-- | crates/core/tedge_api/src/message.rs | 19 | ||||
-rw-r--r-- | crates/core/tedge_api/src/plugin.rs | 30 |
2 files changed, 27 insertions, 22 deletions
diff --git a/crates/core/tedge_api/src/message.rs b/crates/core/tedge_api/src/message.rs index 9df11b51..1e63a368 100644 --- a/crates/core/tedge_api/src/message.rs +++ b/crates/core/tedge_api/src/message.rs @@ -80,11 +80,14 @@ pub struct MessageType { kind: MessageKind, } +#[derive(Clone, PartialEq, Debug)] +struct Uuid([u8; 16]); + #[derive(Debug, Clone, Serialize)] enum MessageKind { Wildcard, #[serde(skip)] - Typed(std::any::TypeId), + Typed(Uuid), } impl MessageType { @@ -109,26 +112,26 @@ impl MessageType { /// Get the [`MessageType`] for a `M`:[`Message`] #[must_use] - pub fn for_message<M: Message>() -> Self { - let id = std::any::TypeId::of::<M>(); + pub fn for_message<M: Message + TypeUuid>() -> Self { + let id = M::UUID; MessageType { name: std::any::type_name::<M>(), - kind: if id == std::any::TypeId::of::<AnyMessage>() { + kind: if id == AnyMessage::UUID { MessageKind::Wildcard } else { - MessageKind::Typed(id) + MessageKind::Typed(Uuid(id)) }, } } pub(crate) fn from_message(msg: &dyn Message) -> Self { - let id = msg.type_id(); + let id = msg.uuid(); MessageType { name: msg.type_name(), - kind: if id == std::any::TypeId::of::<AnyMessage>() { + kind: if id == AnyMessage::UUID { MessageKind::Wildcard } else { - MessageKind::Typed(id) + MessageKind::Typed(Uuid(id)) }, } } diff --git a/crates/core/tedge_api/src/plugin.rs b/crates/core/tedge_api/src/plugin.rs index 6772805a..718dbf67 100644 --- a/crates/core/tedge_api/src/plugin.rs +++ b/crates/core/tedge_api/src/plugin.rs @@ -6,6 +6,7 @@ use futures::future::BoxFuture; use std::any::Any; +use type_uuid::TypeUuid; use downcast_rs::{impl_downcast, DowncastSync}; @@ -420,19 +421,19 @@ impl HandleTypes { /// # type HandledMessages = (Heartbeat,); /// # } /// - /// println!("{:#?}", HeartbeatPlugin::get_handled_types()); + /// println!("{:#x?}", HeartbeatPlugin::get_handled_types()); /// // This will print something akin to: /// // - /// // HandleTypes( - /// // [ - /// // ( - /// // "rust_out::main::_doctest_main_src_plugin_rs_102_0::Heartbeat", - /// // TypeId { - /// // t: 15512189350087767644, - /// // }, - /// // ), - /// // ], - /// // ) + /// // HandleTypes( + /// // [ + /// // MessageType { + /// // name: "rust_out::main::_doctest_main_src_plugin_rs_373_0::Heartbeat", + /// // kind: Typed( + /// // Uuid([ 0x12, 0x76, 0xaa, 0x9c, 0x5e, 0x4, 0x4a, 0xb3, 0xa9, 0x87, 0x61, 0xd8, 0x97, 0x65, 0xab, 0x33, ]) + /// // ), + /// // }, + /// // ], + /// // ) /// ``` pub fn declare_handlers_for<P: PluginDeclaration>() -> HandleTypes where @@ -534,7 +535,7 @@ pub trait DoesHandle<M: MessageBundle> { macro_rules! impl_does_handle_tuple { () => {}; ($cur:ident $($rest:tt)*) => { - impl<$cur: Message, $($rest: Message,)* PLUG: Plugin + Handle<$cur> $(+ Handle<$rest>)*> DoesHandle<($cur, $($rest),*)> for PLUG { + impl<$cur: Message + TypeUuid, $($rest: Message + TypeUuid,)* PLUG: Plugin + Handle<$cur> $(+ Handle<$rest>)*> DoesHandle<($cur, $($rest),*)> for PLUG { fn into_built_plugin(self) -> BuiltPlugin { fn handle_message<'a, $cur: Message, $($rest: Message,)* PLUG: Plugin + Handle<$cur> $(+ Handle<$rest>)*>( plugin: &'a dyn Any, @@ -584,6 +585,8 @@ macro_rules! impl_does_handle_tuple { }; } +impl_does_handle_tuple!(M10 M9 M8 M7 M6 M5 M4 M3 M2 M1); + impl MessageBundle for () { fn get_ids() -> Vec<MessageType> { vec![] @@ -655,7 +658,7 @@ macro_rules! impl_msg_bundle_tuple { ($cur, impl_msg_bundle_tuple!(@rec_tuple $($rest)*)) }; ($cur:ident $($rest:tt)*) => { - impl<$cur: Message, $($rest: Message),*> MessageBundle for ($cur,$($rest),*) { + impl<$cur: Message + TypeUuid, $($rest: Message + TypeUuid),*> MessageBundle for ($cur,$($rest),*) { fn get_ids() -> Vec<MessageType> { vec![ MessageType::for_message::<$cur>(), @@ -669,7 +672,6 @@ macro_rules! impl_msg_bundle_tuple { } impl_msg_bundle_tuple!(M10 M9 M8 M7 M6 M5 M4 M3 M2 M1); -impl_does_handle_tuple!(M10 M9 M8 M7 M6 M5 M4 M3 M2 M1); #[cfg(test)] mod tests { |