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 const constructors #50

Merged
merged 2 commits into from
Oct 8, 2024
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: 3 additions & 2 deletions crates/newtype-uuid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ permits conversions between typed and untyped UUIDs.

## Features

- `default`: Enables default features in the uuid crate.
- `default`: Enables default features in the newtype-uuid crate.
- `std`: Enables the use of the standard library. *Enabled by default.*
- `serde`: Enables serialization and deserialization support via Serde. *Not enabled by
default.*
Expand All @@ -111,7 +111,8 @@ underlying `uuid` crate.
Within the 1.x series, MSRV updates will be accompanied by a minor version bump. The MSRVs for
each minor version are:

* Version **1.0.x**: Rust 1.60
* Version **1.0.x**: Rust 1.60.
* Version **1.1.x**: Rust 1.61. This permits `TypedUuid<T>` to have `const fn` methods.

## Alternatives

Expand Down
84 changes: 82 additions & 2 deletions crates/newtype-uuid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
//!
//! # Features
//!
//! - `default`: Enables default features in the uuid crate.
//! - `default`: Enables default features in the newtype-uuid crate.
//! - `std`: Enables the use of the standard library. *Enabled by default.*
//! - `serde`: Enables serialization and deserialization support via Serde. *Not enabled by
//! default.*
Expand All @@ -104,7 +104,7 @@
//! Within the 1.x series, MSRV updates will be accompanied by a minor version bump. The MSRVs for
//! each minor version are:
//!
//! * Version **1.0.x**: Rust 1.60
//! * Version **1.0.x**: Rust 1.60.
//! * Version **1.1.x**: Rust 1.61. This permits `TypedUuid<T>` to have `const fn` methods.
//!
//! # Alternatives
Expand Down Expand Up @@ -172,6 +172,86 @@ impl<T: TypedUuidKind> TypedUuid<T> {
}
}

/// Creates a UUID from four field values.
#[inline]
#[must_use]
pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: [u8; 8]) -> Self {
Self {
uuid: Uuid::from_fields(d1, d2, d3, &d4),
_phantom: PhantomData,
}
}

/// Creates a UUID from four field values in little-endian order.
///
/// The bytes in the `d1`, `d2` and `d3` fields will be flipped to convert into big-endian
/// order. This is based on the endianness of the UUID, rather than the target environment so
/// bytes will be flipped on both big and little endian machines.
#[inline]
#[must_use]
pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: [u8; 8]) -> Self {
Self {
uuid: Uuid::from_fields_le(d1, d2, d3, &d4),
_phantom: PhantomData,
}
}

/// Creates a UUID from a 128bit value.
#[inline]
#[must_use]
pub const fn from_u128(value: u128) -> Self {
Self {
uuid: Uuid::from_u128(value),
_phantom: PhantomData,
}
}

/// Creates a UUID from a 128bit value in little-endian order.
///
/// The entire value will be flipped to convert into big-endian order. This is based on the
/// endianness of the UUID, rather than the target environment so bytes will be flipped on both
/// big and little endian machines.
#[inline]
#[must_use]
pub const fn from_u128_le(value: u128) -> Self {
Self {
uuid: Uuid::from_u128_le(value),
_phantom: PhantomData,
}
}

/// Creates a UUID from two 64bit values.
#[inline]
#[must_use]
pub const fn from_u64_pair(d1: u64, d2: u64) -> Self {
Self {
uuid: Uuid::from_u64_pair(d1, d2),
_phantom: PhantomData,
}
}

/// Creates a UUID using the supplied bytes.
#[inline]
#[must_use]
pub const fn from_bytes(bytes: uuid::Bytes) -> Self {
Self {
uuid: Uuid::from_bytes(bytes),
_phantom: PhantomData,
}
}

/// Creates a UUID using the supplied bytes in little-endian order.
///
/// The individual fields encoded in the buffer will be flipped.
#[inline]
#[must_use]
pub const fn from_bytes_le(bytes: uuid::Bytes) -> Self {
Self {
uuid: Uuid::from_bytes_le(bytes),
_phantom: PhantomData,
}
}

/// Creates a new, random UUID v4 of this type.
#[inline]
#[cfg(feature = "v4")]
Expand Down
Loading