expose MAX_BYTES as an associated constant for identifiers

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-09 09:50:10 +00:00
parent c434098fb1
commit 3109496a1f
2 changed files with 6 additions and 1 deletions

View File

@ -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> {

View File

@ -55,6 +55,7 @@ pub fn expand_id_zst(input: ItemStruct) -> syn::Result<TokenStream> {
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<TokenStream> {
#[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) }
}