README

#Moonbit/Core UUID

#Overview

The @uuid package provides an implementation of the Universally Unique IDentifier per RFC 4122.

Currently, @uuid does not assume any random generation; you'll have to bring your own random bytes to construct UUIDs.

#Usage

Construct a version 4 UUID:

///|
test {
let u = @uuid.from_hex("ddf99703-742f-7505-4c54-df36a9c243fe").as_version(V4)
inspect(u.to_string(), content="ddf99703-742f-4505-8c54-df36a9c243fe")
}

You can then use it as a key in mappings as it's immutable and implemented Hash, Eq (and Compare for b-tree).

#
UUID

type UUID derive(Compare, Eq)

UUID that complies with RFC 4122.

UUID objects are immutable, hashable and usable as mapping keys. The Display of UUIDs is like 12345678-1234-1234-1234-123456789abc; UUIDs can be constructed using such strings, or 16-bytes strings with big- endian byte order.

Usage

test {
let u1 = @uuid.from_hex("ddf99703-742f-7505-4c54-df36a9c243fe")
let u2 = @uuid.from_bytes(
Bytes::from_array([
221, 249, 151, 3, 116, 47, 117, 5, 76, 84, 223, 54, 169, 194, 67, 254,
]),
)
assert_eq(u1, u2)
}

Construct an RFC-4122-compliant v4 UUID:

test {
let _u = @uuid.from_hex("9558cfadb48547c70f9adf1f75cdef29").as_version(V4)
}

impl Show for UUID

#
UUID::as_version

fn UUID::as_version(self : UUID, version : Version) -> UUID

Converts this UUID to RFC-4122 with the given version number.

#
UUID::hash

fn UUID::hash(self : UUID) -> Int

#
UUID::to_bytes

fn UUID::to_bytes(self : UUID) -> Bytes

The UUID as a 16-byte string.

#
UUID::to_string

fn UUID::to_string(self : UUID) -> String

#
UUID::variant

fn UUID::variant(self : UUID) -> Variant

Retrieves the variant of the UUID.

#
UUID::version

fn UUID::version(self : UUID) -> Version?

Retrieves the version number of the UUID.

If the variant of the UUID is not RFC4122, returns None.

#
Variant

pub(all) enum Variant {
ReservedNCS
RFC4122(Version)
ReservedMicrosoft
ReservedFuture
} derive(
Debug
)

The UUID variant.

#
Version

pub(all) enum Version {
V1
V2
V3
V4
V5
Unknown(Int)
} derive(
Debug
)

The UUID version number.

This is only used when the variant is RFC4122.

#
from_bytes

fn from_bytes(bytes : Bytes) -> UUID raise

Constructs a UUID with the given big-endian bytes.

The produced UUID is not RFC-4122 compliant; see also as_version().

#
from_hex

fn from_hex(hex : String) -> UUID raise

Constructs a UUID with the given hexadecimal string.

This function ignores dashes (-) in the given string. The remaining letters must be exactly 32 hexadecimals, or an error will be raised.

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io