Skip to content

Commit

Permalink
move external impls to new module
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Nov 1, 2021
1 parent 93ead9b commit 477e26e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ version = "2"
[dependencies.arbitrary]
optional = true
version = "1"
features = ["derive"]

# Public (unstable): Used in `zerocopy` derive
[dependencies.zerocopy]
Expand Down
5 changes: 1 addition & 4 deletions examples/windows_guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ fn guid_to_uuid() {
#[cfg(windows)]
fn uuid_from_cocreateguid() {
use uuid::{Uuid, Variant, Version};
use winapi::{
shared::guiddef,
um::combaseapi::CoCreateGuid,
};
use winapi::{shared::guiddef, um::combaseapi::CoCreateGuid};

let mut guid = guiddef::GUID::default();

Expand Down
43 changes: 43 additions & 0 deletions src/external/arbitrary_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::{std::convert::TryInto, Builder, Uuid};

use arbitrary::{Arbitrary, Unstructured};

impl Arbitrary<'_> for Uuid {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
let b = u
.bytes(16)?
.try_into()
.map_err(|_| arbitrary::Error::NotEnoughData)?;

Ok(Builder::from_random_bytes(b).into_uuid())
}
}

#[cfg(test)]
mod tests {
use super::*;

use crate::{Variant, Version};

#[test]
fn test_arbitrary() {
let mut bytes = Unstructured::new(&[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);

let uuid = Uuid::arbitrary(&mut bytes).unwrap();

assert_eq!(Some(Version::Random), uuid.get_version());
assert_eq!(Variant::RFC4122, uuid.get_variant());
}

#[test]
fn test_arbitrary_empty() {
let mut bytes = Unstructured::new(&[]);

// Ensure we don't panic when building an arbitrary `Uuid`
let uuid = Uuid::arbitrary(&mut bytes);

assert!(uuid.is_err());
}
}
6 changes: 6 additions & 0 deletions src/external/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[cfg(feature = "arbitrary")]
mod arbitrary_support;
#[cfg(feature = "serde")]
mod serde_support;
#[cfg(feature = "slog")]
mod slog_support;
File renamed without changes.
File renamed without changes.
10 changes: 2 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ compile_error!("The `zerocopy-unstable` feature is unstable and may break betwee
#[cfg(feature = "zerocopy-unstable")]
use zerocopy::{AsBytes, FromBytes, Unaligned};

#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;

mod builder;
mod error;
mod parser;
Expand All @@ -210,10 +207,8 @@ mod v5;

#[cfg(feature = "rng")]
mod rng;
#[cfg(feature = "serde")]
mod serde_support;
#[cfg(feature = "slog")]
mod slog_support;

mod external;

#[cfg(feature = "macros")]
#[macro_use]
Expand Down Expand Up @@ -359,7 +354,6 @@ pub enum Variant {
feature = "zerocopy-unstable",
derive(AsBytes, FromBytes, Unaligned)
)]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[repr(transparent)]
pub struct Uuid(Bytes);

Expand Down

0 comments on commit 477e26e

Please sign in to comment.