-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added conversion for Vec<u8> #699
Changes from all commits
ee1bbf2
1a8e8d4
36f7d67
af5824b
b68cb30
5012493
a7e4264
a599f24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,10 +11,57 @@ | |
|
||
//! Adapters for alternative string formats. | ||
|
||
use crate::{ | ||
std::{borrow::Borrow, fmt, ptr, str}, | ||
Uuid, Variant, | ||
}; | ||
use alloc::vec::Vec; | ||
use diesel::{deserialize, Expression, Queryable}; | ||
use diesel::deserialize::FromSql; | ||
use diesel::sql_types::{Binary, Blob}; | ||
use diesel::sqlite::Sqlite; | ||
use crate::{Bytes, std::{borrow::Borrow, fmt, ptr, str}, Uuid, Variant}; | ||
use core::convert::TryInto; | ||
use diesel::backend::{Backend}; | ||
|
||
impl From<Uuid> for Vec<u8>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need to be conditionally compiled, with a |
||
fn from(value: Uuid) -> Self { | ||
value.as_bytes().to_vec() | ||
} | ||
} | ||
|
||
impl Queryable<Blob, Sqlite> for Uuid { | ||
type Row = Uuid; | ||
|
||
fn build(row: Self::Row) -> deserialize::Result<Self> { | ||
Ok(row) | ||
} | ||
} | ||
|
||
impl Expression for Uuid{ | ||
type SqlType = Binary; | ||
} | ||
impl FromSql<Blob, Sqlite> for Uuid{ | ||
fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> deserialize::Result<Self> { | ||
|
||
let arr: [u8;16] = Vec::from_sql(bytes) | ||
.map_err(|_| "error calling from_sql on vec")? | ||
.try_into() | ||
//this error is unfallible to unwrap is safe | ||
.unwrap(); | ||
|
||
Ok(Uuid::from_bytes(arr)) | ||
} | ||
} | ||
|
||
impl From<Vec<u8>> for Uuid{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should add this side of the conversion. Converting slices into |
||
fn from(value: Vec<u8>) -> Self { | ||
let mut arr: [u8; 16] = [0;16]; | ||
|
||
value.into_iter() | ||
.take(16) | ||
.enumerate() | ||
.for_each(|(i, v)| arr[i] = v); | ||
|
||
Uuid(arr) | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for Uuid { | ||
#[inline] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,6 +224,7 @@ extern crate std; | |
#[macro_use] | ||
extern crate core as std; | ||
|
||
use diesel::{FromSqlRow, Queryable, Selectable}; | ||
#[cfg(all(uuid_unstable, feature = "zerocopy"))] | ||
use zerocopy::{AsBytes, FromBytes, Unaligned}; | ||
|
||
|
@@ -272,6 +273,7 @@ mod macros; | |
#[doc(hidden)] | ||
#[cfg(feature = "macro-diagnostics")] | ||
pub extern crate uuid_macro_internal; | ||
extern crate alloc; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will also need to be conditionally compiled. |
||
|
||
#[doc(hidden)] | ||
pub mod __macro_support { | ||
|
@@ -437,7 +439,10 @@ pub enum Variant { | |
/// # ABI | ||
/// | ||
/// The `Uuid` type is always guaranteed to be have the same ABI as [`Bytes`]. | ||
/// | ||
|
||
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)] | ||
#[derive(diesel::QueryId)] | ||
#[cfg_attr( | ||
all(uuid_unstable, feature = "zerocopy"), | ||
derive(AsBytes, FromBytes, Unaligned) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, digging back into this it looks like
diesel
actually already depends onuuid
, so I don't think we can add it as a dependency here. You might not actually need to do anything in this library to use it withdiesel
besides enable itsuuid
feature. It looks like their support is implemented here: https://github.com/diesel-rs/diesel/blob/a122e7b88a1bc1b427dde34ed36ce3f36fad772e/diesel/src/pg/types/uuid.rs#L13.