Skip to main content

IdDst

Derive Macro IdDst 

Source
#[derive(IdDst)]
{
    // Attributes available to this derive:
    #[ruma_id]
}
Expand description

Generate methods and trait impl’s for DST identifier type.

This macro generates an Owned* type for the identifier type. The internal representation of the owned type is variable, by default it’ll use a Box<str>, but it can be changed at compile time by setting --cfg=ruma_identifiers_storage=... using RUSTFLAGS or .cargo/config.toml (under [build] -> rustflags = ["..."]). The supported values for this setting are listed in the docs of the owned type.

This macro implements:

  • Conversions to and from string types, AsRef<[u8]> and AsRef<str>, as well as as_str() and as_bytes() methods. The borrowed type can be converted from a borrowed string without allocation.
  • Conversions to and from borrowed and owned type.
  • Deref, AsRef and Borrow to the borrowed type for the owned type.
  • PartialEq implementations for testing equality with string types and owned and borrowed types.

§Attributes

  • #[ruma_id(validate = PATH)]: the path to a function to validate the string during parsing and deserialization. By default, the types implement From string types, when this is set they implement TryFrom.
  • #[ruma_id(smallvec_inline_bytes = USIZE)]: the size of the inline array for the SmallVec inner representation. If this is not provided the default inline size is 32.

§Examples

use ruma_macros::IdDst;

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, IdDst)]
#[ruma_id(validate = ruma_identifiers_validation::user_id::validate, smallvec_inline_bytes = 40)]
pub struct UserId(str);