From 3109496a1f91357c89cbb57cf86f179e2cb013e7 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 9 Oct 2024 09:50:10 +0000 Subject: [PATCH] expose MAX_BYTES as an associated constant for identifiers Signed-off-by: Jason Volk --- crates/ruma-identifiers-validation/src/lib.rs | 2 +- crates/ruma-macros/src/identifiers.rs | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/ruma-identifiers-validation/src/lib.rs b/crates/ruma-identifiers-validation/src/lib.rs index b83a66aa..50f86d57 100644 --- a/crates/ruma-identifiers-validation/src/lib.rs +++ b/crates/ruma-identifiers-validation/src/lib.rs @@ -19,7 +19,7 @@ pub use error::Error; /// All identifiers must be 255 bytes or less. #[cfg(not(feature = "compat-arbitrary-length-ids"))] -const MAX_BYTES: usize = 255; +pub const MAX_BYTES: usize = 255; /// Checks if an identifier is valid. fn validate_id(id: &str, sigil: u8) -> Result<(), Error> { diff --git a/crates/ruma-macros/src/identifiers.rs b/crates/ruma-macros/src/identifiers.rs index fbeec4d8..cfd01a67 100644 --- a/crates/ruma-macros/src/identifiers.rs +++ b/crates/ruma-macros/src/identifiers.rs @@ -55,6 +55,7 @@ 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 as_str_impl = match &input.fields { Fields::Named(_) | Fields::Unit => { @@ -79,6 +80,10 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result { #[automatically_derived] impl #impl_generics #id_ty { + #[cfg(not(feature = "compat-arbitrary-length-ids"))] + #[doc = #max_bytes_docs] + pub const MAX_BYTES: usize = ruma_identifiers_validation::MAX_BYTES; + pub(super) const fn from_borrowed(s: &str) -> &Self { unsafe { std::mem::transmute(s) } }