Crates.io | anyid |
lib.rs | anyid |
version | 0.1.1 |
source | src |
created_at | 2024-05-02 04:15:13.666185 |
updated_at | 2024-05-20 04:56:30.16463 |
description | Provides ambiguous identifier. |
homepage | |
repository | https://github.com/HalsekiRaika/anyid.git |
max_upload_size | |
id | 1227449 |
size | 13,755 |
This library provides anyid::AnyId
. a struct can be used as an identifier for HashMap keys, etc.,
while keeping the type information ambiguous.
All data often has an identifier to identify it. Often it is tightly constrained by the type. (e.g, using the NewType pattern such as ProductId(uuid::Uuid)
instead of just uuid::Uuid
).
However, when these data are lumped together and treated as just data in an integrated manner, such type restrictions seem a bit cumbersome. Therefore, we can solve this problem by providing a TraitObject that uses a Trait that is a collection of Traits that would be implemented at a minimum when used as an identifier.
Note that this crate is intended to facilitate data handling at the application level and is not intended for ignore type processing such as (De)Serialize. Also, if you use this crate as an identifier for data, it can be very troublesome later on, since it only implements what is needed to handle the data in an organized manner.
anyid::AnyId
internally uses Trait, Identifier
wrapped in Arc<T>
as a trait-object.
pub struct AnyId(Arc<dyn Identifier>);
Trait, Identifier
, is automatically implemented for structures with several Trait implementations.
Copy
'static
lifetime, such as in tokio::spawn
, or when inserting data into a HashMap
, etc., the data is usually duplicated, since it consumes values and can be used over and over again. In most cases, duplicated data is used. In addition, we thought that Copy
would be more appropriate than Clone
because the identifier should be “less resource-intensive to duplicate”.PartialEq
/Eq
Display
/Debug
Sync
/Send
Hash
anyid::AnyId
is provided new just like a general structure,
argument requires an impl Identifier
.
use anyid::AnyId;
fn main() {
let str_id: &str = "abc123";
let id = AnyId::new(str_id);
}
Also, If the default implementation conditions for Identifier are met,
a From
use anyid::AnyId;
fn main() {
let str_id: &str = "abc123";
let id: AnyId = str_id.into();
}
anyid::AnyId
itself does not provide Copy
.
However, it implements Clone
using Arc::clone
as is.
impl Clone for AnyId {
fn clone(&self) -> Self {
Self(Arc::clone(&self.0))
}
}
Therefore, cost of Clone
is less.
Implementation of Eq
/(PartialEq
). is in Identifier
.
fn eq(&self, other: &dyn Identifier) -> bool {
let Some(other) = other.as_any().downcast_ref::<T>() else {
return false;
};
self.eq(other)
}
It is quite simple,
but it is important to note that this process does not use Result<T, E>
.
false is returned if the downcast fails.
This library is released under MIT license.
Bug reports and improvements are welcome ;D.