Base64Id

Derive Macro Base64Id 

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

Create your own base64id tuple struct

§Usage

In it’s most simple form, #[derive(Base64Id)] may be used as follows:

#[derive(Base64Id)]
struct MyCustomId(T);

Where T is any of the following concrete types: i64, i32, i16, u64, u32, u16

For example:

#[derive(Base64Id)]
struct MyCustomId(i64);

§Derive Macro Trait Implementations

Once #[derive(Base64Id)] is applied to a tuple struct as described above, the following trait implementations are added:

§Display

Display is added to encode the inner integer as a base64url string of appropriate length.

§FromStr

FromStr is added to decode any string from base64url to the inner integer type, returning an Error on failure.

§TryFrom

TryFrom<[char; n]> is added where n is the length of a given base64url string. This allows for converting a char array of length n into your tuple struct. The value of n is:

  • 11 for 64 bit integers
  • 6 for 32 bit integers
  • 3 for 16 bit integers
§PartialEq, Eq

These are standard impl’s and have no special behaviour.

§PartialOrd, Ord

These traits are implemented with special behaviour where the inner type is a signed integer.

For unsigned integers the ordering behaviour is standard. For signed integers, the value is converted to big endian bytes, these bytes are then converted into an unsigned integer and order comparsion is done on this.

In other words, order comparions are based on the unsigned integer / binary representation of the integer.

§From

Four From trait impl’s are added. Given the following example struct:

#[derive(Base64Id)]
struct MyCustomId(i64);

The following From traits would be added:

impl From<MyCustomId> for i64;
impl From<i64> for MyCustomId;

impl From<MyCustomId> for u64;
impl From<u64> for MyCustomId;

The first two trait impl’s allow converting to and from the structs internal integer type, useful for simply extracting the internal integer out of the struct.

The last two allow converting integers of the opposite sign to and from the struct. This conversion preserves the binary representation of the integer. In practice this means signed and unsigned positive integers will have the same decimal value when converting between them. However, signed and unsigned negative integers will have different decimal values however.

§Serde Trait Implementations

§Serialize, Deserialize

You can also add optional Serde Serialize and Deserialize trait implementations to the struct. To do this you must include Serde as a dependency in your Cargo.toml file. Serde is not a dependency of this crate.

Serde traits can be applied using the following derive macro helper attribute:

#[derive(Base64Id)]
#[base64id(Serialize, Deserialize)]
struct MyCustomId(i64);

You can add neither, either or both traits as needed.

§MIN / MAX Constants

In addition to the above trait implementations, MIN and MAX constants are added. These values are based on the inner integers unsigned / binary representation.