scsys_core/id/traits/
identifier.rs1#[cfg(feature = "alloc")]
6use alloc::string::{String, ToString};
7use core::borrow::Borrow;
8
9pub trait Identifier {
11 private!();
12}
13
14pub trait Identity<K>
18where
19 K: Identifier,
20{
21 type Item: Borrow<K>;
22
23 fn get(&self) -> &Self::Item;
24}
25
26#[cfg(feature = "alloc")]
27pub trait IdentifierExt: Identifier
28where
29 Self: Copy + Eq + Ord + ToString + core::hash::Hash,
30{
31}
32
33pub trait HashId: Identifier
34where
35 Self: Eq + core::hash::Hash,
36{
37}
38
39pub trait Identify {
40 type Id: Identifier;
41
42 fn id(&self) -> &Self::Id;
43}
44
45pub trait IdentifyMut: Identify {
46 fn id_mut(&mut self) -> &mut Self::Id;
47}
48
49impl<K, S> Identity<K> for S
53where
54 S: Borrow<K>,
55 K: Identifier,
56{
57 type Item = S;
58
59 fn get(&self) -> &Self::Item {
60 self
61 }
62}
63
64impl<S> Identify for S
65where
66 S: Identifier,
67{
68 type Id = S;
69
70 fn id(&self) -> &Self::Id {
71 self
72 }
73}
74
75impl<Id> HashId for Id where Id: Eq + Identifier + core::hash::Hash {}
76
77#[cfg(feature = "alloc")]
78impl<Id> IdentifierExt for Id where Id: Copy + Eq + Identifier + Ord + ToString + core::hash::Hash {}
79
80macro_rules! identifier {
81 ($($t:ty),*) => {
82 $(
83 identifier!(@impl $t);
84 )*
85 };
86 (@impl $t:ty) => {
87 impl Identifier for $t {
88 seal!();
89 }
90 };
91}
92
93identifier!(
94 f32, f64, i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize
95);
96identifier!(bool, char, &str);
97
98#[cfg(feature = "alloc")]
99identifier!(String);