https://github.com/timelessnesses/cuid2-timeless
cuid2 implementation from cuid2 python library just to get my rust game up (i am suffering)
https://github.com/timelessnesses/cuid2-timeless
Last synced: 3 months ago
JSON representation
cuid2 implementation from cuid2 python library just to get my rust game up (i am suffering)
- Host: GitHub
- URL: https://github.com/timelessnesses/cuid2-timeless
- Owner: timelessnesses
- License: wtfpl
- Created: 2024-05-18T17:33:21.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-09T06:27:24.000Z (almost 2 years ago)
- Last Synced: 2025-10-30T18:48:03.401Z (7 months ago)
- Language: Rust
- Size: 62.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cuid2-timeless
An unstable, barely tested, probably giving wrong output and might panic out of nowhere CUID2 implementation in Rust. Built for learning more rust and I don't know how to test it.
> [!CAUTION]
> This library is slower than cuid-rust by a whole lot!
> this benchmark was ran with AMD Ryzen 7 7730U 8 Cores and 16 Logical Processors with 16 Gigabytes of RAM
> |benchmark |cuid2-timeless|cuid2 (cuid-rust)|
> |-----------------------|--------------|-----------------|
> |single cuid generation |53.180 µs |2.8162 µs |
> |10,000 cuids generation|553.52 ms |28.900 ms |
>
> This library is for learning purposes. Please don't use this library in production! Use [cuid-rust](https://github.com/mplanchard/cuid-rust) instead!
## Installation
`cargo add cuid2_timeless --features sha3` (or you can use sha2 if you wanted to)
## Usage
Either use the `cuid2_timeless::cuid_wrapper()` and call it again to get ID or get some customization with `cuid2_timeless::Cuid`
```rust
use cuid2_timeless;
let mut cuid = cuid2_timeless::cuid_wrapper();
println!("{}", cuid().unwrap());
```
or some customization
```rust
use cuid2_timeless;
use rand::{self, Rng};
let mut cuid = cuid2_timeless::Cuid::new(
Box::new( // A randomizer function
|| {
rand::thread_rng().gen()
}
),
Box::new( // Your very own counter function
|mut i| {
Box::new(move || {
i += 1;
i
})
}
),
24, // Length
Box::new(cuid2_timeless::utils::create_fingerprint) // And a fingerprint creator function (im too lazy to implement)
);
cuid.generate(None).unwrap();
```
or verify if it actually is `CUID`
```rust
use cuid2_timeless;
println!("{}", cuid2_timeless::is_cuid("f9ovgvsnly7h6khwt4nx08kom".to_string(), None, None));
```
## Features
- `regex` is a feature for either remove or add `regex` crate to the module. Turning this off will remove the `is_cuid` function (enabled by default)
- `random` is a feature for either remove or add `rand` crate to the module. Turning this off will remove `Default` trait from `Cuid` (enabled by default)
- `sha2` is a feature for either remove or add `sha2` crate to the module. Turning this off will remove SHA2 hashing algorithm, but turning this feature on will uses `sha2` for hashing (cannot enabled with `sha3` present, compiling will error if `sha2` and `sha3` is enabled, not enabled by default)
- `sha3` is a feature for either remove or add `sha3` crate to the module. Turning this off will remove SHA3 hashing algorithm, but turning this feature on will uses `sha3` for hashing (cannot enabled with `sha2` present, compiling will error if `sha2` and `sha3` is enabled, enabled by default)