Struct StringId

Source
pub struct StringId<Id, Lookup>
where Lookup: StrLookup,
{ /* private fields */ }
Expand description

Manage identifier both as human readable string and unique number.

StringId is designed to be used with new-type pattern and use generics to adapt to different usages.

§Generics

  • Id - the type of the id associated with the str
  • Lookup - the type who allow to lookup for str corresponding to id

The Lookup should implement StrLookup.

The constraints of Id type, depends of the type used as Lookup. With provided implementation of StrLookup Id is generated from std with cityhasher::hash, so Id should implement cityhasher::FromCityHash and could only be be u32 or u64.

The size of a StringId variable is the size of the Id type.

StrLookupEmpty is an implementation of StrLookup who don’t keep the str.

§Example


#[strlookup_hashmap(key = u32, store = StringStrStore, size = 128)]
struct CustomIdLookup;

#[derive(Debug, Clone, Copy, StringIdImpl)]
struct CustomId(StringId<u32, CustomIdLookup>);

// Create an id from the str 'resource_1'
let res_id = CustomId::from_str("resource_1").unwrap();

// CustomId can be copied
let res_id_copy = res_id;
assert_eq!(res_id_copy, res_id);

// Two CustomId created from the same `str` are equal.
let res_id_bis = CustomId::from_str("resource_1").unwrap();
assert_eq!(res_id, res_id_bis);

// The StringId variable keep the size of Id. Check :
assert_eq!(
    size_of::<u32>(),
    size_of::<CustomId>()
);

Implementations§

Source§

impl<Id, Lookup> StringId<Id, Lookup>
where Id: Clone + Copy, Lookup: StrLookup<Key = Id>,

Source

pub fn id(&self) -> Id

Source

pub fn get_str(&self) -> &str

Trait Implementations§

Source§

impl<Id, Lookup> Clone for StringId<Id, Lookup>
where Id: Clone + Copy, Lookup: StrLookup<Key = Id>,

Manual implementation to avoid than Lookup should implement Clone

This happen with implementation generated by derive macro Clone.

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<Id, Lookup> Debug for StringId<Id, Lookup>
where Id: Debug + Clone + Copy, Lookup: StrLookup<Key = Id>,

Source§

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

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

impl<Id: Default, Lookup> Default for StringId<Id, Lookup>
where Lookup: StrLookup + Default,

Source§

fn default() -> StringId<Id, Lookup>

Returns the “default value” for a type. Read more
Source§

impl<Id, Lookup> Display for StringId<Id, Lookup>
where Id: Clone + Copy, Lookup: StrLookup<Key = Id>,

Source§

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

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

impl<Id, Lookup> FromStr for StringId<Id, Lookup>
where Id: Clone + Copy, Lookup: StrLookup<Key = Id>,

Source§

type Err = ()

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

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

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

impl<Id, Lookup> Ord for StringId<Id, Lookup>
where Id: Clone + Copy + Eq, Lookup: StrLookup<Key = Id>,

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<Id, Lookup> PartialEq for StringId<Id, Lookup>
where Id: Clone + Copy + PartialEq, Lookup: StrLookup<Key = Id>,

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<Id, Lookup> PartialOrd for StringId<Id, Lookup>
where Id: Clone + Copy + PartialEq, Lookup: StrLookup<Key = Id>,

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<Id, Lookup> Copy for StringId<Id, Lookup>
where Id: Copy, Lookup: StrLookup<Key = Id>,

Manual implementation to avoid than Lookup should implement Copy

This happen with implementation generated by derive macro Copy.

Source§

impl<Id, Lookup> Eq for StringId<Id, Lookup>
where Id: Clone + Copy + PartialEq, Lookup: StrLookup<Key = Id>,

Auto Trait Implementations§

§

impl<Id, Lookup> Freeze for StringId<Id, Lookup>
where Id: Freeze,

§

impl<Id, Lookup> RefUnwindSafe for StringId<Id, Lookup>
where Id: RefUnwindSafe, Lookup: RefUnwindSafe,

§

impl<Id, Lookup> Send for StringId<Id, Lookup>
where Id: Send, Lookup: Send,

§

impl<Id, Lookup> Sync for StringId<Id, Lookup>
where Id: Sync, Lookup: Sync,

§

impl<Id, Lookup> Unpin for StringId<Id, Lookup>
where Id: Unpin, Lookup: Unpin,

§

impl<Id, Lookup> UnwindSafe for StringId<Id, Lookup>
where Id: UnwindSafe, Lookup: 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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.