Skip to content
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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ fast-rng = ["rng", "rand"]
sha1 = ["sha1_smol"]
md5 = ["md-5"]

[dependencies]
diesel = {version = "2.1.0", features = ["sqlite"]}
Copy link
Member

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 on uuid, 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 with diesel besides enable its uuid feature. It looks like their support is implemented here: https://github.com/diesel-rs/diesel/blob/a122e7b88a1bc1b427dde34ed36ce3f36fad772e/diesel/src/pg/types/uuid.rs#L13.


# Public: Used in trait impls on `Uuid`
[dependencies.serde]
default-features = false
Expand Down
53 changes: 49 additions & 4 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,55 @@

//! Adapters for alternative string formats.

use crate::{
std::{borrow::Borrow, fmt, ptr, str},
Uuid, Variant,
};
use alloc::vec::Vec;
use diesel::{deserialize, Queryable};
use diesel::deserialize::FromSql;
use diesel::sql_types::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>{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be conditionally compiled, with a #[cfg(feature = “std”]).

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 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{
Copy link
Member

Choose a reason for hiding this comment

The 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 Uuids is fallible, but we can't express that through this trait.

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]
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ extern crate std;
#[macro_use]
extern crate core as std;

use diesel::FromSqlRow;
#[cfg(all(uuid_unstable, feature = "zerocopy"))]
use zerocopy::{AsBytes, FromBytes, Unaligned};

Expand Down Expand Up @@ -272,6 +273,7 @@ mod macros;
#[doc(hidden)]
#[cfg(feature = "macro-diagnostics")]
pub extern crate uuid_macro_internal;
extern crate alloc;
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Expand Down