From 8f8937b29e81f1aa457092f531676e802b6e053c Mon Sep 17 00:00:00 2001 From: Jonathan de Jong Date: Tue, 12 Jul 2022 10:44:02 +0200 Subject: [PATCH] identifiers: Add Into Box & Arc, and PartialEq Arc (#1235) * Add Into Box & Arc, and PartialEq Arc * change into to from * Apply suggestions from code review Co-authored-by: Jonas Platte Co-authored-by: Jonas Platte --- crates/ruma-macros/src/identifiers.rs | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/crates/ruma-macros/src/identifiers.rs b/crates/ruma-macros/src/identifiers.rs index 33d8bf10..dafda878 100644 --- a/crates/ruma-macros/src/identifiers.rs +++ b/crates/ruma-macros/src/identifiers.rs @@ -313,6 +313,24 @@ fn expand_owned_id(input: &ItemStruct) -> TokenStream { } } + impl #impl_generics From<#owned_ty> for Box<#id_ty> { + fn from(a: #owned_ty) -> Box<#id_ty> { + #[cfg(not(any(ruma_identifiers_storage = "Arc")))] + { a.inner } + #[cfg(ruma_identifiers_storage = "Arc")] + { a.inner.as_ref().into() } + } + } + + impl #impl_generics From<#owned_ty> for std::sync::Arc<#id_ty> { + fn from(a: #owned_ty) -> std::sync::Arc<#id_ty> { + #[cfg(not(any(ruma_identifiers_storage = "Arc")))] + { a.inner.into() } + #[cfg(ruma_identifiers_storage = "Arc")] + { a.inner } + } + } + impl #impl_generics std::fmt::Display for #owned_ty { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.as_str()) @@ -400,6 +418,18 @@ fn expand_owned_id(input: &ItemStruct) -> TokenStream { AsRef::<#id_ty>::as_ref(self) == AsRef::<#id_ty>::as_ref(other) } } + + impl #impl_generics PartialEq> for #owned_ty { + fn eq(&self, other: &std::sync::Arc<#id_ty>) -> bool { + AsRef::<#id_ty>::as_ref(self) == AsRef::<#id_ty>::as_ref(other) + } + } + + impl #impl_generics PartialEq<#owned_ty> for std::sync::Arc<#id_ty> { + fn eq(&self, other: &#owned_ty) -> bool { + AsRef::<#id_ty>::as_ref(self) == AsRef::<#id_ty>::as_ref(other) + } + } } }