Struct EntityId

Source
pub struct EntityId<T: Prefix, I: Identifier> { /* private fields */ }
Expand description

Generic ID type supporting both UUID and ULID

  • Supports UUID (all versions) and ULID.
  • Provides thread-safe caching.
  • Allows custom delimiters.
  • Type-safe through generic parameters.

Implementations§

Source§

impl<T: Prefix, I: Identifier> EntityId<T, I>

Source

pub fn new<S: AsRef<str>>(s: S) -> Result<Self, EntityIdError>

Create an EntityId from an identifier string

Accepts any type that can be referenced as a string slice.

Source

pub fn generate() -> Self

Generate a new EntityId with a random identifier

Source

pub fn from_identifier(id: I) -> Self

Create an EntityId from an existing identifier

Source

pub fn as_str(&self) -> &str

Get the full ID string with prefix (cached)

Returns the complete entity ID string in the format <prefix><delimiter><identifier>. For example: “user_123e4567-e89b-12d3-a456-426614174000”

Source

pub fn id_str(&self) -> &str

Get the raw identifier string without prefix

Returns just the identifier part of the ID without the prefix and delimiter. For example: “123e4567-e89b-12d3-a456-426614174000”

Source

pub fn timestamp_ms(&self) -> Option<u64>

Get the timestamp in milliseconds (if available)

Source

pub fn identifier(&self) -> &I

Get the underlying identifier object

Returns a reference to the underlying identifier object (UuidIdentifier or UlidIdentifier).

Source

pub fn prefix() -> &'static str

Get the prefix for this entity type

Source

pub fn delimiter() -> &'static str

Get the delimiter for this entity type

Source

pub fn builder() -> EntityIdBuilder<T, I>

Create a builder for this entity type

Source

pub fn from_raw_str<S: AsRef<str>>(s: S) -> Result<Self, EntityIdError>

Implement TryFrom<&str> for the raw identifier string (without prefix)

This method parses a raw identifier string (UUID or ULID) without the prefix and creates an EntityId with the appropriate prefix.

§Example
use entid::{Prefix, UuidEntityId};

#[derive(Prefix)]
#[entid(prefix = "user")]
struct User;

// Parse a raw UUID string (without the "user_" prefix)
let uuid_str = "123e4567-e89b-12d3-a456-426614174000";
let user_id = UuidEntityId::<User>::from_raw_str(uuid_str).unwrap();
assert_eq!(user_id.as_str(), "user_123e4567-e89b-12d3-a456-426614174000");
Source

pub fn parse_raw_str<S, E, F>(s: S, error_mapper: F) -> Result<Self, E>
where S: AsRef<str>, F: FnOnce(IdentifierError) -> E,

Parse a raw identifier string (without prefix) into an EntityId, with custom error handling

This method is similar to from_raw_str, but allows mapping the error to a custom type.

§Example
use entid::{Prefix, UuidEntityId};

#[derive(Prefix)]
#[entid(prefix = "user")]
struct User;

// Parse a raw UUID string with custom error handling
let uuid_str = "123e4567-e89b-12d3-a456-426614174000";
let user_id = UuidEntityId::<User>::parse_raw_str(uuid_str, |e| format!("Invalid UUID: {}", e)).unwrap();
Source§

impl<T: Prefix> EntityId<T, UuidIdentifier>

Source

pub fn with_uuid(uuid: Uuid) -> Self

Create a new UUID-based entity ID with a specific UUID

Source

pub fn new_v4() -> Self

Create a new UUID v4 (random) entity ID

Source

pub fn new_v5(namespace: &Uuid, name: &str) -> Self

Create a new UUID v5 (name-based) entity ID

Source§

impl<T: Prefix> EntityId<T, UlidIdentifier>

Source

pub fn with_ulid(ulid: Ulid) -> Self

Create a new ULID-based entity ID with a specific ULID

Source

pub fn with_timestamp(timestamp_ms: u64) -> Self

Create a new ULID entity ID with a specific timestamp

Source

pub fn monotonic_from(previous: Option<&Self>) -> Self

Create a monotonic ULID entity ID based on a previous one

Trait Implementations§

Source§

impl<T: Prefix, I: Identifier> AsRef<str> for EntityId<T, I>

Implement AsRef<str> for easy conversion to string slices

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Prefix, I: Identifier> Borrow<str> for EntityId<T, I>

Implement Borrow<str> to allow using EntityId in collections with string keys

Source§

fn borrow(&self) -> &str

Immutably borrows from an owned value. Read more
Source§

impl<T: Prefix, I: Identifier> Clone for EntityId<T, I>

Implement Clone (Avoids duplicating cached value)

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Prefix, I: Debug + Identifier> Debug for EntityId<T, I>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de, T: Prefix, I: Identifier> Deserialize<'de> for EntityId<T, I>

Deserialization - Reconstruct from stored ID string

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: Prefix, I: Identifier> Display for EntityId<T, I>

Implement Display for easy printing

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Prefix, I: Identifier> From<&EntityId<T, I>> for String

Implement Into<String> for references to convert to string representation

Source§

fn from(entity_id: &EntityId<T, I>) -> Self

Converts to this type from the input type.
Source§

impl<T: Prefix> From<&EntityId<T, UlidIdentifier>> for UlidIdentifier

Implement Into<UlidIdentifier> for references to ULID-based EntityId

Source§

fn from(entity_id: &EntityId<T, UlidIdentifier>) -> Self

Converts to this type from the input type.
Source§

impl<T: Prefix> From<&EntityId<T, UuidIdentifier>> for UuidIdentifier

Implement Into<UuidIdentifier> for references to UUID-based EntityId

Source§

fn from(entity_id: &EntityId<T, UuidIdentifier>) -> Self

Converts to this type from the input type.
Source§

impl<T: Prefix, I: Identifier> From<EntityId<T, I>> for String

Implement Into<String> to convert to string representation

Source§

fn from(entity_id: EntityId<T, I>) -> Self

Converts to this type from the input type.
Source§

impl<T: Prefix> From<EntityId<T, UlidIdentifier>> for UlidIdentifier

Implement Into<UlidIdentifier> for ULID-based EntityId

Source§

fn from(entity_id: EntityId<T, UlidIdentifier>) -> Self

Converts to this type from the input type.
Source§

impl<T: Prefix> From<EntityId<T, UuidIdentifier>> for UuidIdentifier

Implement Into<UuidIdentifier> for UUID-based EntityId

Source§

fn from(entity_id: EntityId<T, UuidIdentifier>) -> Self

Converts to this type from the input type.
Source§

impl<T: Prefix, I: Identifier> From<I> for EntityId<T, I>

Implement From<I> to create an EntityId from an identifier

Source§

fn from(id: I) -> Self

Converts to this type from the input type.
Source§

impl<T: Prefix, I: Identifier> FromStr for EntityId<T, I>

Implement FromStr for parsing from strings

Source§

type Err = EntityIdError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<T: Prefix, I: Identifier> Hash for EntityId<T, I>

Implement Hash based on identifier

This ensures EntityId<T, I> can be used in HashSet or HashMap.

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: Prefix, I: Identifier + Ord> Ord for EntityId<T, I>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: Prefix, I: Identifier> PartialEq for EntityId<T, I>

Implement PartialEq and Eq (Comparison based on identifier)

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Prefix, I: Identifier + Ord> PartialOrd for EntityId<T, I>

Implement PartialOrd and Ord for lexicographical sorting

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Prefix, I: Identifier> Serialize for EntityId<T, I>

Serialization - Store as <prefix><delimiter><id>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Prefix, I: Identifier> TryFrom<&String> for EntityId<T, I>

Implement TryFrom<&String> for converting from string references

Source§

type Error = EntityIdError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: Prefix, I: Identifier> TryFrom<&str> for EntityId<T, I>

Implement TryFrom<&str> for converting from string slices

Source§

type Error = EntityIdError

The type returned in the event of a conversion error.
Source§

fn try_from(s: &str) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: Prefix, I: Identifier> TryFrom<String> for EntityId<T, I>

Implement TryFrom<String> for converting from owned strings

Source§

type Error = EntityIdError

The type returned in the event of a conversion error.
Source§

fn try_from(s: String) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: Prefix, I: Identifier> Eq for EntityId<T, I>

Auto Trait Implementations§

§

impl<T, I> !Freeze for EntityId<T, I>

§

impl<T, I> RefUnwindSafe for EntityId<T, I>

§

impl<T, I> Send for EntityId<T, I>
where I: Send, T: Send,

§

impl<T, I> Sync for EntityId<T, I>
where I: Sync, T: Sync,

§

impl<T, I> Unpin for EntityId<T, I>
where I: Unpin, T: Unpin,

§

impl<T, I> UnwindSafe for EntityId<T, I>
where I: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,