Skip to content

Instantly share code, notes, and snippets.

@pollend
Created April 18, 2025 03:01
Show Gist options
  • Save pollend/8650a18acfce18e23a1e2bf7d380d84b to your computer and use it in GitHub Desktop.
Save pollend/8650a18acfce18e23a1e2bf7d380d84b to your computer and use it in GitHub Desktop.
use core::str;
use std::{fmt::Display, hash::Hash, u64};
// unique id salt
const UNQ_ID_SALT: u64 = 10393820183;
pub type UidBuf = [u8; 10];
#[derive(Clone)]
pub struct UnqID {
id: u32,
buf: UidBuf
}
impl<'a> Display for UnqID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let res: &str = self.into();
write!(f, "{}", res)
}
}
impl Hash for UnqID {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
state.write_u32(self.id);
}
}
impl<'a> UnqID {
pub const fn id(&self) -> u32 {
return self.id;
}
pub const fn uid_str(&self) -> &str {
unsafe { str::from_utf8_unchecked(&self.buf) }
}
pub const fn new(value: u64) -> UnqID {
let mut hash_id = value as u64;
let mut id: UidBuf = [0; 10];
id[0] = 'u' as u8;
id[1] = '-' as u8;
{
let mut k = 0;
while k < 8 {
id[k + 2] = (['0', '1', '2', '3', '4','5','6','7','8','9','a','b','c','d','e','f'])[(hash_id % 16) as usize] as u8;
hash_id /= 16;
k += 1;
}
}
{
let mut s: usize = 2;
let mut p: usize = 9;
while s < p {
let aux = id[s];
id[s] = id[p];
id[p] = aux;
s += 1;
p -= 1;
}
}
return UnqID {
id: value as u32,
buf: id,
};
}
pub const fn new_str(s: &str) -> UnqID {
let ss = s.as_bytes();
let mut value: u64 = 0x100000001b3;
value = value.overflowing_add(UNQ_ID_SALT).0;
{
let mut i = 0;
while i < ss.len() {
value = value.overflowing_mul(0x100000001b3).0 ^ ss[i] as u64;
i += 1;
}
}
Self::new(value)
}
}
impl<'a> Into<&'a str> for &'a UnqID {
fn into(self) -> &'a str {
unsafe { std::str::from_utf8_unchecked(&self.buf.as_ref()) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment