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

Add unstable zerocopy support #538

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ jobs:
os_tests:
name: "Tests / OS: ${{ matrix.os }} - ${{ matrix.channel }}-${{ matrix.rust_target }}"
runs-on: ${{ matrix.os }}
env:
RUSTFLAGS: "--cfg uuid_unstable"
RUSTDOCFLAGS: "--cfg uuid_unstable"
strategy:
matrix:
exclude:
Expand Down Expand Up @@ -122,6 +125,8 @@ jobs:
nodeps:
name: Build / No deps
runs-on: ubuntu-latest
env:
RUSTFLAGS: "--cfg uuid_unstable"
steps:
- name: Checkout sources
uses: actions/checkout@v2
Expand Down
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ v4 = ["getrandom"]
v5 = ["sha-1"]
js = ["getrandom", "getrandom/js"]

# Unstable features (these also need RUSTFLAGS="--cfg uuid_unstable" to work)
zerocopy-unstable = ["zerocopy"]

# Private
[dependencies.getrandom]
optional = true
Expand Down Expand Up @@ -100,6 +103,11 @@ version = "2"
optional = true
version = "0.3"

# Public (unstable): Used in `zerocopy` derive
[dependencies.zerocopy]
optional = true
version = "0.6"

[dev-dependencies.bincode]
version = "1.0"

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ You can disable default features with:
uuid = { version = "0.8", default-features = false }
```

### Unstable features

Some features are unstable. They may be incomplete or depend on other unstable libraries.
These include:

* `zerocopy-unstable` - adds support for zero-copy deserialization using the `zerocopy` library.

Unstable features may break between minor releases.

To allow unstable features, you'll need to enable the Cargo feature as normal, but also pass an additional
flag through your environment to opt-in to unstable `uuid` features:

```
RUSTFLAGS="--cfg uuid_unstable"
```

## Examples

To parse a UUID given in the simple format and print it as a urn:
Expand Down
26 changes: 25 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@
//! uuid = { version = "0.8", default-features = false }
//! ```
//!
//! ## Unstable features
//!
//! Some features are unstable. They may be incomplete or depend on other unstable libraries.
//! These include:
//!
//! * `zerocopy-unstable` - adds support for zero-copy deserialization using the `zerocopy` library.
//!
//! Unstable features may break between minor releases.
//!
//! To allow unstable features, you'll need to enable the Cargo feature as normal, but also pass an additional
//! flag through your environment to opt-in to unstable `uuid` features:
//!
//! ```text
//! RUSTFLAGS="--cfg uuid_unstable"
//! ```
//!
//! # Building for other targets
//!
//! ## WebAssembly
Expand Down Expand Up @@ -96,7 +112,7 @@
//!
//! If you need to use `v4` in a no-std environment, you'll need to
//! follow [`getrandom`'s docs] on configuring a source of randomness
//! on unsupported targets.
//! on currently unsupported targets.
//!
//! # Examples
//!
Expand Down Expand Up @@ -188,6 +204,13 @@ extern crate std;
#[macro_use]
extern crate core as std;

// Check that unstable features are accompanied by a the `uuid_unstable` cfg
#[cfg(all(not(uuid_unstable), feature = "zerocopy-unstable"))]
compile_error!("The `zerocopy-unstable` feature is unstable and may break between releases. Please also pass `RUSTFLAGS=\"--cfg uuid_unstable\"` to allow it.");

#[cfg(feature = "zerocopy-unstable")]
use zerocopy::{AsBytes, FromBytes, Unaligned};

mod builder;
mod error;
mod parser;
Expand Down Expand Up @@ -248,6 +271,7 @@ pub enum Variant {

/// A Universally Unique Identifier (UUID).
#[derive(Clone, Copy, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[cfg_attr(feature = "zerocopy-unstable", derive(AsBytes, FromBytes, Unaligned))]
#[repr(transparent)]
pub struct Uuid(Bytes);

Expand Down