v0.4.0

Uuid structv0.1.0

Source
A universally unique identifier: sixteen bytes in the standard's own order.
pub struct Uuid {
    pub bytes: byte[N];
}

Fields

NameTypeDescription
bytesbyte[N]The sixteen bytes, most significant first.

Associated functions

Nil

pub func Nil() -> Uuid

The nil UUID: sixteen zero bytes. A defined value rather than an absence, and worth having as one — the alternative is every caller inventing a sentinel of their own.

Returns: the all-zero value

Source

Max

pub func Max() -> Uuid

The max UUID: sixteen bytes of ones, which RFC 9562 defines as nil's counterpart.

Returns: the all-ones value

Source

FromBytes

pub func FromBytes(bytes: byte[..]) -> Uuid

A UUID from sixteen bytes in the standard's order. Fewer than sixteen leaves the rest zero; more are ignored. No structure is imposed — the version and variant fields are whatever the bytes say, which is what lets this read a UUID from somewhere else.

NameTypeDescription
bytesbyte[..]sixteen bytes to take as the value, in big-endian order

Returns: those sixteen bytes as a value, with no field interpreted or corrected

Source

Methods

Bytes

pub func Bytes(&self) -> byte[..]

The sixteen bytes, in the standard's order.

Returns: the sixteen bytes, in big-endian order

Source

IsNil

pub func IsNil(&self) -> bool

Whether this is the nil UUID.

Returns: whether every byte is zero

Source

IsMax

pub func IsMax(&self) -> bool

Whether this is the max UUID.

Returns: whether every byte is one

Source

Version

pub func Version(&self) -> uint8

The version, from the high nibble of the seventh byte. Meaningful only for the standard variant; a nil or max UUID reports a number that means nothing, which is why Variant should be consulted first.

Returns: the version nibble, 0 through 15; a value this package did not make may hold anything

Source

Variant

pub func Variant(&self) -> UuidVariant

The variant, from the high bits of the ninth byte. The encoding is variable-length rather than two fixed bits: a leading zero means the oldest layout, 10 the standard one, 110 Microsoft's, and 111 the reserved future.

Returns: the variant the layout declares

Source

Equals

pub func Equals(&self, other: &Uuid) -> bool

Whether two UUIDs are the same.

NameTypeDescription
other&Uuidthe value to compare against

Returns: whether all sixteen bytes match

Source

Compare

pub func Compare(&self, other: &Uuid) -> Ordering

Orders two UUIDs by their bytes, most significant first. For a version 7 UUID this is chronological order, because its timestamp occupies the leading bytes in big-endian form. Reordering here — to match a platform's GUID layout, say — would destroy that, which is why the byte order is fixed rather than negotiable.

NameTypeDescription
other&Uuidthe value to compare against

Returns: how the two order as sixteen big-endian bytes, which is lexicographic order for the text

Source

Hash

pub func Hash(&self) -> uint64

A hash of the sixteen bytes, for use as a table key. FNV-1a, which is not a cryptographic hash and does not need to be: a UUID is already the output of one, or of an entropy source, so what a table wants here is speed and an even spread over buckets.

Returns: the value's hash

Source

Implements Display

Uuid conforms to Display.

WriteDisplay

pub func WriteDisplay(
    &self,
    writer: &var TextWriter,
    spec: FormatSpec
) throws FormatError

Writes the canonical lower-case form, or the URN form under the urn style. The default is the hyphenated form RFC 9562 defines, in lower case, because that is the one representation the standard has and the one every other implementation writes. hyphenated names it explicitly and urn selects the urn:uuid: form. Upper case is not a style, since a canonical form that varies by case is not canonical; FormatUpper remains for a format that demands it. Reports whatever the writer reported, and UnsupportedRequest for any other style or for a precision, which a value of exactly one length has nothing to apply.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole form was accepted

Source

Implements Debug

Uuid conforms to Debug.

WriteDebug

pub func WriteDebug(
    &self,
    writer: &var TextWriter,
    spec: FormatSpec
) throws FormatError

The same text as Display: an identifier has one reading, and quoting it would say nothing extra. The failures WriteDisplay reports.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole form was accepted

Source

SwapGuidOrder

pub func SwapGuidOrder(value: &Uuid) -> Uuid

The same identifier with its first three fields byte-swapped, which is how Windows stores a GUID. The conversion is its own inverse, so one function serves both directions: a UUID becomes the GUID bytes a Windows API expects, and GUID bytes read from one become the UUID they represent. It must be called deliberately — applying it by accident, or failing to apply it when crossing the boundary, both produce a well-formed identifier that refers to something else entirely.

Source

Parameters

NameTypeDescription
value&Uuidthe value to act on

Returns: the same sixteen bytes with the first three fields byte-swapped

UuidVariant

pub enum UuidVariant: int32 {
    NetworkComputingSystem = 0,
    Standard = 1,
    Microsoft = 2,
    Future = 3
}

What layout a UUID's bytes follow.

Source

Cases

NameValueDescription
NetworkComputingSystem0Apollo NCS, from before the standard — the layout the nil UUID also happens to match.
Standard1The layout RFC 9562 defines, which is what essentially every UUID in use is.
Microsoft2Microsoft's early variant: reserved, and rare.
Future3Reserved for the future and never assigned.