commit | 3ca66ef49829d82b6e070d7120e4f4273e6a5bce | [log] [tgz] |
---|---|---|
author | Yi Kong <yikong@google.com> | Wed Mar 17 17:53:21 2021 +0800 |
committer | Yi Kong <yikong@google.com> | Wed Mar 17 10:10:49 2021 +0000 |
tree | a5c9bd83e5a12bebdcce91c4e2283738ed658a8c | |
parent | 35775f0646d64af81968444e2198c08ae8f3745c [diff] |
Import 'uuid' crate version 0.8.2 Test: presubmit Change-Id: I17e47a7d1e4725b94b514a48a306a6b87e178837
Generate and parse UUIDs.
Provides support for Universally Unique Identifiers (UUIDs). A UUID is a unique 128-bit number, stored as 16 octets. UUIDs are used to assign unique identifiers to entities without requiring a central allocating authority.
They are particularly useful in distributed systems, though they can be used in disparate areas, such as databases and network protocols. Typically a UUID is displayed in a readable string form as a sequence of hexadecimal digits, separated into groups by hyphens.
The uniqueness property is not strictly guaranteed, however for all practical purposes, it can be assumed that an unintentional collision would be extremely unlikely.
By default, this crate depends on nothing but std
and cannot generate Uuid
s. You need to enable the following Cargo features to enable various pieces of functionality:
v1
- adds the Uuid::new_v1
function and the ability to create a V1 using an implementation of uuid::v1::ClockSequence
(usually uuid::v1::Context
) and a timestamp from time::timespec
.v3
- adds the Uuid::new_v3
function and the ability to create a V3 UUID based on the MD5 hash of some data.v4
- adds the Uuid::new_v4
function and the ability to randomly generate a Uuid
.v5
- adds the Uuid::new_v5
function and the ability to create a V5 UUID based on the SHA1 hash of some data.serde
- adds the ability to serialize and deserialize a Uuid
using the serde
crate.You need to enable one of the following Cargo features together with v3
, v4
or v5
feature if you're targeting wasm32-unknown-unknown
target:
stdweb
- enables support for OsRng
on wasm32-unknown-unknown
via stdweb
combined with cargo-web
wasm-bindgen
- wasm-bindgen
enables support for OsRng
on wasm32-unknown-unknown
via wasm-bindgen
By default, uuid
can be depended on with:
[dependencies] uuid = "0.8"
To activate various features, use syntax like:
[dependencies] uuid = { version = "0.8", features = ["serde", "v4"] }
You can disable default features with:
[dependencies] uuid = { version = "0.8", default-features = false }
To parse a UUID given in the simple format and print it as a urn:
use uuid::Uuid; fn main() -> Result<(), uuid::Error> { let my_uuid = Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8")?; println!("{}", my_uuid.to_urn()); Ok(()) }
To create a new random (V4) UUID and print it out in hexadecimal form:
// Note that this requires the `v4` feature enabled in the uuid crate. use uuid::Uuid; fn main() { let my_uuid = Uuid::new_v4(); println!("{}", my_uuid); Ok(()) }
Examples of string representations:
936DA01F9ABD4d9d80C702AF85C822A8
550e8400-e29b-41d4-a716-446655440000
urn:uuid:F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.