common: Add DebugAsRefStr derive macro
This commit is contained in:
parent
44d4a34d9c
commit
625d5a0782
@ -79,6 +79,7 @@ Improvements:
|
|||||||
* Add support for bundled reference relations (MSC3267 / Matrix 1.5)
|
* Add support for bundled reference relations (MSC3267 / Matrix 1.5)
|
||||||
* Add the `formatted` field on `KeyVerificationRequestEventContent` (Matrix 1.5)
|
* Add the `formatted` field on `KeyVerificationRequestEventContent` (Matrix 1.5)
|
||||||
* Add `content` accessors for `Any*StateEvent` enums
|
* Add `content` accessors for `Any*StateEvent` enums
|
||||||
|
* Add the `DebugAsRefStr` derive macro to `ruma_common::serde`
|
||||||
|
|
||||||
# 0.10.5
|
# 0.10.5
|
||||||
|
|
||||||
|
@ -73,6 +73,6 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub use ruma_macros::{
|
pub use ruma_macros::{
|
||||||
AsRefStr, DeserializeFromCowStr, DisplayAsRefStr, FromString, OrdAsRefStr, PartialEqAsRefStr,
|
AsRefStr, DebugAsRefStr, DeserializeFromCowStr, DisplayAsRefStr, FromString, OrdAsRefStr,
|
||||||
PartialOrdAsRefStr, SerializeAsRefStr, StringEnum, _FakeDeriveSerde,
|
PartialEqAsRefStr, PartialOrdAsRefStr, SerializeAsRefStr, StringEnum, _FakeDeriveSerde,
|
||||||
};
|
};
|
||||||
|
@ -39,6 +39,7 @@ use self::{
|
|||||||
identifiers::IdentifierInput,
|
identifiers::IdentifierInput,
|
||||||
serde::{
|
serde::{
|
||||||
as_str_as_ref_str::expand_as_str_as_ref_str,
|
as_str_as_ref_str::expand_as_str_as_ref_str,
|
||||||
|
debug_as_ref_str::expand_debug_as_ref_str,
|
||||||
deserialize_from_cow_str::expand_deserialize_from_cow_str,
|
deserialize_from_cow_str::expand_deserialize_from_cow_str,
|
||||||
display_as_ref_str::expand_display_as_ref_str,
|
display_as_ref_str::expand_display_as_ref_str,
|
||||||
enum_as_ref_str::expand_enum_as_ref_str,
|
enum_as_ref_str::expand_enum_as_ref_str,
|
||||||
@ -291,6 +292,13 @@ pub fn derive_display_as_ref_str(input: TokenStream) -> TokenStream {
|
|||||||
expand_display_as_ref_str(&input.ident).unwrap_or_else(syn::Error::into_compile_error).into()
|
expand_display_as_ref_str(&input.ident).unwrap_or_else(syn::Error::into_compile_error).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Derive the `fmt::Debug` trait using the `AsRef<str>` implementation of the type.
|
||||||
|
#[proc_macro_derive(DebugAsRefStr)]
|
||||||
|
pub fn derive_debug_as_ref_str(input: TokenStream) -> TokenStream {
|
||||||
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
|
expand_debug_as_ref_str(&input.ident).unwrap_or_else(syn::Error::into_compile_error).into()
|
||||||
|
}
|
||||||
|
|
||||||
/// Derive the `Serialize` trait using the `AsRef<str>` implementation of the type.
|
/// Derive the `Serialize` trait using the `AsRef<str>` implementation of the type.
|
||||||
#[proc_macro_derive(SerializeAsRefStr)]
|
#[proc_macro_derive(SerializeAsRefStr)]
|
||||||
pub fn derive_serialize_as_ref_str(input: TokenStream) -> TokenStream {
|
pub fn derive_serialize_as_ref_str(input: TokenStream) -> TokenStream {
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
pub mod as_str_as_ref_str;
|
pub mod as_str_as_ref_str;
|
||||||
pub mod attr;
|
pub mod attr;
|
||||||
pub mod case;
|
pub mod case;
|
||||||
|
pub mod debug_as_ref_str;
|
||||||
pub mod deserialize_from_cow_str;
|
pub mod deserialize_from_cow_str;
|
||||||
pub mod display_as_ref_str;
|
pub mod display_as_ref_str;
|
||||||
pub mod enum_as_ref_str;
|
pub mod enum_as_ref_str;
|
||||||
|
17
crates/ruma-macros/src/serde/debug_as_ref_str.rs
Normal file
17
crates/ruma-macros/src/serde/debug_as_ref_str.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
use proc_macro2::{Ident, TokenStream};
|
||||||
|
use quote::quote;
|
||||||
|
|
||||||
|
pub fn expand_debug_as_ref_str(ident: &Ident) -> syn::Result<TokenStream> {
|
||||||
|
Ok(quote! {
|
||||||
|
#[automatically_derived]
|
||||||
|
#[allow(deprecated)]
|
||||||
|
impl ::std::fmt::Debug for #ident {
|
||||||
|
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
|
||||||
|
<::std::primitive::str as ::std::fmt::Debug>::fmt(
|
||||||
|
::std::convert::AsRef::<::std::primitive::str>::as_ref(self),
|
||||||
|
f,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user