Expand description
§rosetta-uuid
A wrapper implementation of UUID providing binary diesel bindings for SQLite and PostgreSQL, and redis serialization support.
§Features
This crate provides a Uuid wrapper type that implements various traits based on enabled features:
diesel: Enables Diesel integration.postgres: Enables binaryUuidsupport for PostgreSQL.sqlite: Enables binaryUuidsupport for SQLite (stored as BLOB).
redis: EnablesToRedisArgsandFromRedisValuefor easy Redis storage and retrieval.serde: Enables serialization and deserialization via Serde.
§Platform Support
- Wasm: Verified support for
wasm32-unknown-unknownwithuuidv4 and v7 generation.
§Usage
Add this to your Cargo.toml. Select the features matching your database or storage requirements.
[dependencies]
rosetta-uuid = { version = "0.1", features = ["diesel", "postgres", "redis", "serde"] }§Example
use rosetta_uuid::Uuid;
use core::str::FromStr;
// Create a new random UUID (v4)
let id = Uuid::new_v4();
// Create a new timestamp-based UUID (v7) with the current UTC timestamp
let id_v7 = Uuid::utc_v7();
// Parse from string
let parsed = Uuid::from_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
// Access underlying uuid::Uuid methods via Deref
println!("Version: {:?}", id.get_version());To use the functions made available by this crate as a SQLite registered function, you can use:
#[cfg(feature = "sqlite")]
fn main() {
use diesel::{SqliteConnection, Connection};
#[diesel::declare_sql_function]
extern "SQL" {
/// Generates a UUID v4
fn uuidv4() -> Binary;
/// Generates a UUID v7
fn uuidv7() -> Binary;
}
let mut connection = SqliteConnection::establish(":memory:")
.expect("Failed to create in-memory SQLite database");
uuidv4_utils::register_impl(&connection, rosetta_uuid::Uuid::new_v4)
.expect("Failed to register uuidv4");
uuidv7_utils::register_impl(&connection, rosetta_uuid::Uuid::utc_v7)
.expect("Failed to register uuidv7");
}
#[cfg(not(feature = "sqlite"))]
fn main() {}§Traits
The Uuid type implements:
FromStrDisplay,DebugDeref,DerefMut(touuid::Uuid)AsRef<[u8; 16]>,AsRef<uuid::Uuid>From<uuid::Uuid>,Into<uuid::Uuid>From<[u8; 16]>,Into<[u8; 16]>Default(returns nil UUID)
Structs§
- Uuid
- A wrapper around the
uuidcrate’sUuidtype.