concurrent_event/id.rs
1//! Contains the definition of the handler ID which is used for referencing
2//! event handlers after registration.
3
4use rand::Rng;
5
6/// The number of random bytes contained in a handler ID. This should be
7/// sufficiently high to resist random as well as maliciously crafted
8/// collisions.
9pub const HANDLER_ID_BYTES: usize = 32;
10
11/// An ID for an event handler that consists of random bytes. Uniqueness
12/// depends on sufficient randomness in generation.
13#[derive(PartialEq, Eq, Hash, Copy, Clone)]
14pub struct HandlerId {
15 bytes: [u8; HANDLER_ID_BYTES]
16}
17
18impl HandlerId {
19 pub(crate) fn new() -> HandlerId {
20 let mut rng = rand::thread_rng();
21 HandlerId {
22 bytes: rng.gen()
23 }
24 }
25}