working v0.1.0

This commit is contained in:
cbax 2024-09-29 01:44:48 -04:00
parent b588ddc6f5
commit 94c1894d23
3 changed files with 118 additions and 38 deletions

9
Cargo.lock generated
View file

@ -5,3 +5,12 @@ version = 3
[[package]] [[package]]
name = "icechip" name = "icechip"
version = "0.1.0" version = "0.1.0"
dependencies = [
"tinyrand",
]
[[package]]
name = "tinyrand"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87ffaad2263779579369d45f65cad0647c860893d27e4543cdcc1e428d07da2c"

View file

@ -6,3 +6,4 @@ repository = "https://git.cbax.dev/icechip"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
[dependencies] [dependencies]
tinyrand = "0.5.0"

View file

@ -1,3 +1,5 @@
use tinyrand::{Rand, StdRand};
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -6,7 +8,7 @@ pub enum Version {
EXTENDED, EXTENDED,
} }
#[derive(Debug)] #[derive(Clone, Debug)]
pub struct Icechip<T> { pub struct Icechip<T> {
version: Version, version: Version,
timestamp: T, timestamp: T,
@ -69,22 +71,44 @@ impl From<u128> for Icechip<u128> {
} }
} }
impl<T> Icechip<T> { impl<T: Clone> Icechip<T> {
pub fn set_epoch(mut self, epoch: u128) { pub fn set_epoch(mut self, epoch: u128) {
self.epoch = Some(epoch); self.epoch = Some(epoch);
} }
pub fn timestamp(&self) -> T {
return self.timestamp.clone();
}
pub fn machine_id(&self) -> T {
return self.machine_id.clone();
}
pub fn sequence_id(&self) -> T {
return self.sequence_id.clone();
}
pub fn version(&self) -> Version {
return self.version.clone();
}
} }
impl Icechip<u64> { impl Icechip<u64> {
pub fn new(version: Version, epoch: Option<u128>) -> Icechip<u64> { pub fn new(epoch: Option<u128>) -> Icechip<u64> {
todo!(); let mut rand = StdRand::default();
} return Icechip {
version: Version::ENHANCED,
fn offset_epoch(&self, timestamp: u128) -> u64 { timestamp: 0,
match self.epoch { machine_id: rand
Some(s) => return (timestamp - s) as u64, .next_u64()
None => return timestamp as u64, .checked_shl(41)
.unwrap()
.checked_shr(53)
.unwrap(),
sequence_id: 0,
epoch,
} }
.tick();
} }
pub fn tick(&self) -> Icechip<u64> { pub fn tick(&self) -> Icechip<u64> {
@ -92,10 +116,10 @@ impl Icechip<u64> {
Ok(o) => { Ok(o) => {
let o = o.as_millis(); let o = o.as_millis();
let curr_ts = match self.epoch { let curr_ts = match self.epoch {
Some(s) => (o - s).checked_shl(23).unwrap().checked_shr(23).unwrap(), Some(s) => (o - s).checked_shl(23).unwrap().checked_shr(23).unwrap() as u64,
None => o.checked_shl(23).unwrap().checked_shr(23).unwrap(), None => o.checked_shl(23).unwrap().checked_shr(23).unwrap() as u64,
}; };
if self.timestamp as u128 == curr_ts { if self.timestamp == curr_ts {
if (self.sequence_id + 1) > 1023 { if (self.sequence_id + 1) > 1023 {
std::thread::sleep(std::time::Duration::from_millis(1)); std::thread::sleep(std::time::Duration::from_millis(1));
return self.tick(); return self.tick();
@ -111,7 +135,7 @@ impl Icechip<u64> {
} else { } else {
return Icechip { return Icechip {
version: self.version.clone(), version: self.version.clone(),
timestamp: self.offset_epoch(curr_ts.clone()), timestamp: curr_ts.clone(),
machine_id: self.machine_id.clone(), machine_id: self.machine_id.clone(),
sequence_id: 0, sequence_id: 0,
epoch: self.epoch.clone(), epoch: self.epoch.clone(),
@ -125,22 +149,33 @@ impl Icechip<u64> {
} }
} }
pub fn with_id(machine_id: u32, version: Version) -> Icechip<u64> { pub fn with_id(machine_id: u64, epoch: Option<u128>) -> Icechip<u64> {
todo!(); return Icechip {
/*match version { version: Version::ENHANCED,
Version::ENHANCED => { timestamp: 0,
machine_id,
}, sequence_id: 0,
Version::EXTENDED => { epoch,
} }
}*/ .tick();
}
pub fn to_u64(self) -> u64 {
return Into::<u64>::into(self);
} }
} }
impl Icechip<u128> { impl Icechip<u128> {
pub fn new(version: Version, epoch: Option<u128>) -> Icechip<u128> { pub fn new(epoch: Option<u128>) -> Icechip<u128> {
todo!(); let mut rand = StdRand::default();
return Icechip {
version: Version::EXTENDED,
timestamp: 0,
machine_id: rand.next_u128(),
sequence_id: 0,
epoch,
}
.tick();
} }
pub fn tick(&self) -> Icechip<u128> { pub fn tick(&self) -> Icechip<u128> {
@ -181,16 +216,19 @@ impl Icechip<u128> {
} }
} }
pub fn with_id(machine_id: u32, version: Version) -> Icechip<u128> { pub fn with_id(machine_id: u128, epoch: Option<u128>) -> Icechip<u128> {
todo!(); return Icechip {
/*match version { version: Version::EXTENDED,
Version::ENHANCED => { timestamp: 0,
machine_id,
}, sequence_id: 0,
Version::EXTENDED => { epoch,
} }
}*/ .tick();
}
pub fn to_u128(self) -> u128 {
return Into::<u128>::into(self);
} }
} }
@ -200,13 +238,45 @@ mod tests {
#[test] #[test]
fn from_into_u128() { fn from_into_u128() {
let test_chip = Icechip::from(0x0123456789ABCDEFu128); let test_chip = Icechip::from(1234567890u128);
assert_eq!(0x0123456789ABCDEFu128, test_chip.into()); assert_eq!(1234567890u128, test_chip.into());
} }
#[test] #[test]
fn from_into_u64() { fn from_into_u64() {
let test_chip = Icechip::from(0x0123456789ABCDEFu64); let test_chip = Icechip::from(1234567890u64);
assert_eq!(0x0123456789ABCDEFu64, test_chip.into()); assert_eq!(1234567890u64, test_chip.into());
}
#[test]
fn custom_machine_id_u128() {
let test_chip = Icechip::<u128>::with_id(1u128, None);
test_chip.tick();
assert_eq!(test_chip.machine_id(), 1u128);
}
#[test]
fn custom_machine_id_u64() {
let test_chip = Icechip::<u64>::with_id(1u64, None);
test_chip.tick();
assert_eq!(test_chip.machine_id(), 1u64);
}
#[test]
fn tick_u128() {
let test_chip = Icechip::<u128>::new(None);
println!("test: {:#b}", test_chip.clone().to_u128());
let ticked_chip = test_chip.tick();
println!("tick: {:#b}", ticked_chip.clone().to_u128());
assert!(test_chip.clone().to_u128() < ticked_chip.to_u128());
}
#[test]
fn tick_u64() {
let test_chip = Icechip::<u64>::new(None);
println!("test: {:#b}", test_chip.clone().to_u64());
let ticked_chip = test_chip.tick();
println!("tick: {:#b}", ticked_chip.clone().to_u64());
assert!(test_chip.clone().to_u64() < ticked_chip.to_u64());
} }
} }