I'm using the rust uuid crate: https://crates.io/crates/uuid
I need a "v5" id generated with a custom namespace for my program. What is the correct way to define a custom namespace? I looked at the source code of how the NAMESPACE_OID
is defined:
pub const NAMESPACE_DNS: Self = Uuid([
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0,
0x4f, 0xd4, 0x30, 0xc8,
]);
However, copying the code and putting it in my module, with the name and bytes changed of course, does not work. (I have pre-computed the bytes needed for my namespace Uuid)
pub const NAMESPACE_MYOWN: Uuid = Uuid([...]);
I then hope to generate a new Uuid via: Uuid::new_v5(&NAMESPACE_MYOWN, "my string".as_bytes())
But the error occurs occurs during compile time at constant NAMESPACE_MYOWN
initialisation with the error: cannot initialize a tuple struct which contains private fields constructor is not visible here due to private fields
I believe the rust crate has made this constructor using bytes private?
A possible solution of course would be use something like: Uuid::parse_string
every time I need the namespace Uuid, and hardcode the string somewhere instead. But it seems a little wasteful of having to compute the namespace Uuid every time when it's fixed?
The parse_string
also can error out, which means I need to handle errors, etc. A lot of overhead when I can guarantee my namespace Uuid is correct beforehand.
Is there a better way?
You can use Uuid::from_bytes
:
pub const NAMESPACE_DNS: Uuid = Uuid::from_bytes([
0x6b, 0xa7, 0xb8, 0x10,
0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0,
0x4f, 0xd4, 0x30, 0xc8,
]);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments