rust_rsm/common/
uuid.rs

1use std::fmt::Display;
2
3use rand;
4use serde::{Deserialize,Serialize};
5
6#[derive(Debug,Clone,Deserialize,Serialize)]
7pub struct uuid_t ([u8;16]);
8impl uuid_t {
9    pub fn new()->Self {
10        let v=rand::random::<u128>().to_be_bytes();
11        return Self (v)
12    }
13    pub fn from_u128(v:u128)->Self {
14        return Self(v.to_be_bytes())
15    }
16    pub fn from_u64(high:u64,low:u64)->Self {
17        let v =(high as u128) << 64 | (low as u128);
18        return Self (v.to_be_bytes())
19    }
20    pub fn as_u128(&self)->u128 {
21        u128::from_ne_bytes(self.0)
22    }
23    
24    fn fromat(&self)->String {
25        let va=&self.0;
26        format!("{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
27        va[0],va[1],va[2],va[3],va[4],va[5],va[6],va[7],va[8],va[9],va[10],va[11],va[12],va[13],va[14],va[15])
28    }
29}
30
31impl Display for uuid_t {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        write!(f,"{}",self.fromat())
34    }
35}
36impl Default for uuid_t {
37    fn default() -> Self {
38        return Self([0;16])
39    }
40    
41}