From 814df8faf2096845b5ec4012a3175a9bbd11e7df Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 1 Jan 2025 19:36:52 +0000 Subject: [PATCH] add capacity() and missing len() to Owned identifier interface Signed-off-by: Jason Volk --- crates/ruma-macros/src/identifiers.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/ruma-macros/src/identifiers.rs b/crates/ruma-macros/src/identifiers.rs index 3bd1263f..a53caf0d 100644 --- a/crates/ruma-macros/src/identifiers.rs +++ b/crates/ruma-macros/src/identifiers.rs @@ -60,6 +60,8 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result { let as_str_docs = format!("Creates a string slice from this `{id}`."); let as_bytes_docs = format!("Creates a byte slice from this `{id}`."); let max_bytes_docs = format!("Maximum byte length for any `{id}`."); + let len_docs = format!("Get the string length of {id}."); + let is_empty_docs = format!("Returns true if {id} has zero length."); let as_str_impl = match &input.fields { Fields::Named(_) | Fields::Unit => { @@ -122,10 +124,16 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result { unsafe { Box::from_raw(Box::into_raw(s) as _) } } - #[doc = #as_str_docs] #[inline] - pub fn as_str(&self) -> &str { - #as_str_impl + #[doc = #is_empty_docs] + pub fn is_empty(&self) -> bool { + self.as_str().is_empty() + } + + #[inline] + #[doc = #len_docs] + pub fn len(&self) -> usize { + self.as_str().len() } #[doc = #as_bytes_docs] @@ -133,6 +141,11 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result { pub fn as_bytes(&self) -> &[u8] { self.as_str().as_bytes() } + #[doc = #as_str_docs] + #[inline] + pub fn as_str(&self) -> &str { + #as_str_impl + } } #[automatically_derived] @@ -291,6 +304,7 @@ fn expand_owned_id(input: &ItemStruct, inline_bytes: usize) -> TokenStream { let partial_eq_string = expand_partial_eq_string(owned_ty.clone(), &impl_generics); let doc_header = format!("Owned variant of {id}"); + let capacity_doc = format!("Get the size of the buffer backing {id}"); quote! { #[doc = #doc_header] @@ -307,6 +321,12 @@ fn expand_owned_id(input: &ItemStruct, inline_bytes: usize) -> TokenStream { #phantom_impl } } + + #[inline] + #[doc = #capacity_doc] + pub fn capacity(&self) -> usize { + self.inner.capacity() + } } #[automatically_derived]