diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/uuid/examples | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/uuid/examples')
-rw-r--r-- | third_party/rust/uuid/examples/random_uuid.rs | 15 | ||||
-rw-r--r-- | third_party/rust/uuid/examples/sortable_uuid.rs | 15 | ||||
-rw-r--r-- | third_party/rust/uuid/examples/uuid_macro.rs | 19 | ||||
-rw-r--r-- | third_party/rust/uuid/examples/windows_guid.rs | 112 |
4 files changed, 161 insertions, 0 deletions
diff --git a/third_party/rust/uuid/examples/random_uuid.rs b/third_party/rust/uuid/examples/random_uuid.rs new file mode 100644 index 0000000000..897071c468 --- /dev/null +++ b/third_party/rust/uuid/examples/random_uuid.rs @@ -0,0 +1,15 @@ +//! Generating a random UUID. +//! +//! If you enable the `v4` feature you can generate random UUIDs. + +#[test] +#[cfg(feature = "v4")] +fn generate_random_uuid() { + use uuid::Uuid; + + let uuid = Uuid::new_v4(); + + assert_eq!(Some(uuid::Version::Random), uuid.get_version()); +} + +fn main() {} diff --git a/third_party/rust/uuid/examples/sortable_uuid.rs b/third_party/rust/uuid/examples/sortable_uuid.rs new file mode 100644 index 0000000000..dc12812533 --- /dev/null +++ b/third_party/rust/uuid/examples/sortable_uuid.rs @@ -0,0 +1,15 @@ +//! Generating a sortable UUID. +//! +//! If you enable the `v7` feature you can generate sortable UUIDs. + +#[test] +#[cfg(feature = "v7")] +fn generate_sortable_uuid() { + use uuid::Uuid; + + let uuid = Uuid::now_v7(); + + assert_eq!(Some(uuid::Version::SortRand), uuid.get_version()); +} + +fn main() {} diff --git a/third_party/rust/uuid/examples/uuid_macro.rs b/third_party/rust/uuid/examples/uuid_macro.rs new file mode 100644 index 0000000000..c4ff0484b5 --- /dev/null +++ b/third_party/rust/uuid/examples/uuid_macro.rs @@ -0,0 +1,19 @@ +//! Using the `uuid!` macro. +//! +//! `uuid!` will parse encoded UUIDs at compile time instead of at runtime. +//! If you've got a fixed UUID string handy then consider using `uuid!` instead +//! of `Uuid::parse_str` or `str::parse`. +//! +//! If you enable the `macro-diagnostics` feature, you can see much better +//! error messages. + +#[test] +fn parse_uuid_at_compile_time() { + use uuid::uuid; + + let uuid = uuid!("67e55044-10b1-426f-9247-bb680e5fe0c8"); + + assert_eq!(Some(uuid::Version::Random), uuid.get_version()); +} + +fn main() {} diff --git a/third_party/rust/uuid/examples/windows_guid.rs b/third_party/rust/uuid/examples/windows_guid.rs new file mode 100644 index 0000000000..6cbc10ed68 --- /dev/null +++ b/third_party/rust/uuid/examples/windows_guid.rs @@ -0,0 +1,112 @@ +//! Converting between Windows GUIDs and UUIDs. +//! +//! Windows GUIDs are specified as using mixed endianness. +//! What you get will depend on the source of the GUID. +//! Functions like `CoCreateGuid` will generate a valid UUID so +//! the fields will be naturally ordered for `Uuid::from_fields`. +//! Other GUIDs might need to be passed to `Uuid::from_fields_le` +//! to have their ordering swapped. + +#[test] +#[cfg(windows)] +fn guid_to_uuid() { + use uuid::Uuid; + use windows_sys::core; + + let guid_in = core::GUID { + data1: 0x4a35229d, + data2: 0x5527, + data3: 0x4f30, + data4: [0x86, 0x47, 0x9d, 0xc5, 0x4e, 0x1e, 0xe1, 0xe8], + }; + + let uuid = Uuid::from_fields(guid_in.data1, guid_in.data2, guid_in.data3, &guid_in.data4); + + let guid_out = { + let fields = uuid.as_fields(); + + core::GUID { + data1: fields.0, + data2: fields.1, + data3: fields.2, + data4: *fields.3, + } + }; + + assert_eq!( + (guid_in.data1, guid_in.data2, guid_in.data3, guid_in.data4), + ( + guid_out.data1, + guid_out.data2, + guid_out.data3, + guid_out.data4 + ) + ); +} + +#[test] +#[cfg(windows)] +fn guid_to_uuid_le_encoded() { + use uuid::Uuid; + use windows_sys::core; + + // A GUID might not be encoded directly as a UUID + // If its fields are stored in little-endian order they might + // need to be flipped. Whether or not this is necessary depends + // on the source of the GUID + let guid_in = core::GUID { + data1: 0x9d22354a, + data2: 0x2755, + data3: 0x304f, + data4: [0x86, 0x47, 0x9d, 0xc5, 0x4e, 0x1e, 0xe1, 0xe8], + }; + + let uuid = Uuid::from_fields_le(guid_in.data1, guid_in.data2, guid_in.data3, &guid_in.data4); + + let guid_out = { + let fields = uuid.to_fields_le(); + + core::GUID { + data1: fields.0, + data2: fields.1, + data3: fields.2, + data4: *fields.3, + } + }; + + assert_eq!( + (guid_in.data1, guid_in.data2, guid_in.data3, guid_in.data4), + ( + guid_out.data1, + guid_out.data2, + guid_out.data3, + guid_out.data4 + ) + ); +} + +#[test] +#[cfg(windows)] +fn uuid_from_cocreateguid() { + use uuid::{Uuid, Variant, Version}; + use windows_sys::core; + use windows_sys::Win32::System::Com::CoCreateGuid; + + let mut guid = core::GUID { + data1: 0, + data2: 0, + data3: 0, + data4: [0u8; 8], + }; + + unsafe { + CoCreateGuid(&mut guid); + } + + let uuid = Uuid::from_fields(guid.data1, guid.data2, guid.data3, &guid.data4); + + assert_eq!(Variant::RFC4122, uuid.get_variant()); + assert_eq!(Some(Version::Random), uuid.get_version()); +} + +fn main() {} |