Skip to content

Instantly share code, notes, and snippets.

@teschmitt
Last active June 5, 2024 18:58
Show Gist options
  • Save teschmitt/e8526a2823d2e436b8c9aa70d6247751 to your computer and use it in GitHub Desktop.
Save teschmitt/e8526a2823d2e436b8c9aa70d6247751 to your computer and use it in GitHub Desktop.
Rust newtype UUID implementation that can be used with diesel ORM
use diesel::{
backend::Backend,
deserialize::{self, FromSql},
serialize::{self, Output, ToSql},
sql_types::Binary,
AsExpression, FromSqlRow,
};
use std::fmt::{self, Display, Formatter};
use uuid;
#[derive(Debug, Default, Clone, Copy, FromSqlRow, AsExpression, Hash, Eq, PartialEq)]
#[diesel(sql_type = Binary)]
pub struct UUID(pub uuid::Uuid);
impl UUID {
pub fn new_v4() -> Self {
Self(uuid::Uuid::new_v4())
}
}
impl From<UUID> for uuid::Uuid {
fn from(s: UUID) -> Self {
s.0
}
}
impl From<uuid::Uuid> for UUID {
fn from(s: uuid::Uuid) -> Self {
UUID(s)
}
}
impl Display for UUID {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl<B: Backend> FromSql<Binary, B> for UUID
where
Vec<u8>: FromSql<Binary, B>,
{
fn from_sql(bytes: <B as Backend>::RawValue<'_>) -> deserialize::Result<Self> {
let value = <Vec<u8>>::from_sql(bytes)?;
uuid::Uuid::from_slice(&value)
.map(UUID)
.map_err(|e| e.into())
}
}
impl<B: Backend> ToSql<Binary, B> for UUID
where
[u8]: ToSql<Binary, B>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, B>) -> serialize::Result {
self.0.as_bytes().to_sql(out)
}
}
@teschmitt
Copy link
Author

This updates and expands this answer for diesel v2.1.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment