move external impls to new module

This commit is contained in:
KodrAus 2021-11-02 08:00:45 +10:00
parent 93ead9b498
commit 477e26e318
7 changed files with 52 additions and 13 deletions

View File

@ -117,7 +117,6 @@ version = "2"
[dependencies.arbitrary] [dependencies.arbitrary]
optional = true optional = true
version = "1" version = "1"
features = ["derive"]
# Public (unstable): Used in `zerocopy` derive # Public (unstable): Used in `zerocopy` derive
[dependencies.zerocopy] [dependencies.zerocopy]

View File

@ -53,10 +53,7 @@ fn guid_to_uuid() {
#[cfg(windows)] #[cfg(windows)]
fn uuid_from_cocreateguid() { fn uuid_from_cocreateguid() {
use uuid::{Uuid, Variant, Version}; use uuid::{Uuid, Variant, Version};
use winapi::{ use winapi::{shared::guiddef, um::combaseapi::CoCreateGuid};
shared::guiddef,
um::combaseapi::CoCreateGuid,
};
let mut guid = guiddef::GUID::default(); let mut guid = guiddef::GUID::default();

43
src/external/arbitrary_support.rs vendored Normal file
View File

@ -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
src/external/mod.rs vendored Normal file
View File

@ -0,0 +1,6 @@
#[cfg(feature = "arbitrary")]
mod arbitrary_support;
#[cfg(feature = "serde")]
mod serde_support;
#[cfg(feature = "slog")]
mod slog_support;

View File

@ -190,9 +190,6 @@ compile_error!("The `zerocopy-unstable` feature is unstable and may break betwee
#[cfg(feature = "zerocopy-unstable")] #[cfg(feature = "zerocopy-unstable")]
use zerocopy::{AsBytes, FromBytes, Unaligned}; use zerocopy::{AsBytes, FromBytes, Unaligned};
#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;
mod builder; mod builder;
mod error; mod error;
mod parser; mod parser;
@ -210,10 +207,8 @@ mod v5;
#[cfg(feature = "rng")] #[cfg(feature = "rng")]
mod rng; mod rng;
#[cfg(feature = "serde")]
mod serde_support; mod external;
#[cfg(feature = "slog")]
mod slog_support;
#[cfg(feature = "macros")] #[cfg(feature = "macros")]
#[macro_use] #[macro_use]
@ -359,7 +354,6 @@ pub enum Variant {
feature = "zerocopy-unstable", feature = "zerocopy-unstable",
derive(AsBytes, FromBytes, Unaligned) derive(AsBytes, FromBytes, Unaligned)
)] )]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[repr(transparent)] #[repr(transparent)]
pub struct Uuid(Bytes); pub struct Uuid(Bytes);