Skip to content

Commit

Permalink
Merge pull request #703 from uuid-rs/feat/convert-to-vec
Browse files Browse the repository at this point in the history
Support converting between Uuid and vec
  • Loading branch information
KodrAus authored Oct 17, 2023
2 parents 50f7027 + 98b2edf commit 1d4bd6e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,22 @@ impl AsRef<[u8]> for Uuid {
}
}

#[cfg(feature = "std")]
impl From<Uuid> for std::vec::Vec<u8> {
fn from(value: Uuid) -> Self {
value.0.to_vec()
}
}

#[cfg(feature = "std")]
impl std::convert::TryFrom<std::vec::Vec<u8>> for Uuid {
type Error = Error;

fn try_from(value: std::vec::Vec<u8>) -> Result<Self, Self::Error> {
Uuid::from_slice(&value)
}
}

#[cfg(feature = "serde")]
pub mod serde {
//! Adapters for alternative `serde` formats.
Expand Down Expand Up @@ -1744,6 +1760,31 @@ mod tests {
assert!(!ur.iter().all(|&b| b == 0));
}

#[test]
#[cfg(feature = "std")]
#[cfg_attr(
all(
target_arch = "wasm32",
target_vendor = "unknown",
target_os = "unknown"
),
wasm_bindgen_test
)]
fn test_convert_vec() {
use crate::std::{convert::TryInto, vec::Vec};

let u = new();
let ub = u.as_ref();

let v: Vec<u8> = u.into();

assert_eq!(&v, ub);

let uv: Uuid = v.try_into().unwrap();

assert_eq!(uv, u);
}

#[test]
#[cfg_attr(
all(
Expand Down

0 comments on commit 1d4bd6e

Please sign in to comment.