Skip to content

Commit

Permalink
Add support for uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
djkoloski committed Jun 16, 2021
1 parent 0299138 commit 8fd0e53
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
9 changes: 9 additions & 0 deletions bytecheck/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ bytecheck_derive = { version = "=0.5.2", path = "../bytecheck_derive", default-f
ptr_meta = "0.1"
simdutf8 = { version = "0.1", default-features = false }

# Support for various common crates. These are primarily to get users off the ground and build some
# momentum.

# These are NOT PLANNED to remain in bytecheck for the final release. Much like serde, these
# implementations should be moved into their respective crates over time. Before adding support for
# another crate, please consider getting bytecheck support in the crate instead.

uuid = { version = "0.8", optional = true }

[features]
default = ["const_generics", "std"]
const_generics = []
Expand Down
21 changes: 21 additions & 0 deletions bytecheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@
//! details by default. This feature provides full error information.
//! - `std`: Enables standard library support (enabled by default). If the `std` feature is not
//! enabled, the `alloc` crate is required.
//!
//! ## Crate support
//!
//! Some common crates need to be supported by bytecheck before an official integration has been
//! made. Support is provided by bytecheck for these crates, but in the future crates should depend
//! on bytecheck and provide their own implementations. The crates that already have support
//! provided by bytecheck should work toward integrating the implementations into themselves.
//!
//! Crates supported by bytecheck:
//!
//! - [`uuid`](https://docs.rs/uuid)
#![deny(broken_intra_doc_links)]
#![deny(missing_docs)]
Expand All @@ -101,6 +112,16 @@
#[cfg(not(feature = "std"))]
extern crate alloc;

// Support for various common crates. These are primarily to get users off the ground and build some
// momentum.

// These are NOT PLANNED to remain in bytecheck for the final release. Much like serde, these
// implementations should be moved into their respective crates over time. Before adding support for
// another crate, please consider getting bytecheck support in the crate instead.

#[cfg(feature = "uuid")]
pub mod uuid;

#[cfg(has_atomics)]
use core::sync::atomic::{
AtomicBool, AtomicI16, AtomicI32, AtomicI8, AtomicU16, AtomicU32, AtomicU8,
Expand Down
32 changes: 32 additions & 0 deletions bytecheck/src/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! [`CheckBytes`](crate::CheckBytes) implementations for uuid.
use crate::CheckBytes;
use uuid::{Bytes, Uuid};

impl<C: ?Sized> CheckBytes<C> for Uuid {
type Error = <Bytes as CheckBytes<C>>::Error;

unsafe fn check_bytes<'a>(value: *const Self, context: &mut C) -> Result<&'a Self, Self::Error> {
// Safety: cast is OK because Uuid is repr(transparent)
Bytes::check_bytes(value.cast(), context)?;
Ok(&*value)
}
}

#[cfg(test)]
mod bytecheck_tests {
use crate::CheckBytes;
use uuid::Uuid;

#[test]
fn test_check_bytes() {
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
let u = Uuid::parse_str(uuid_str).unwrap();

// Safety: the pointer is aligned and points to enough bytes to represent a Uuid
unsafe {
Uuid::check_bytes(&u as *const Uuid, &mut ())
.expect("failed to check uuid");
}
}
}

0 comments on commit 8fd0e53

Please sign in to comment.