refactored sequence_id to sequence and switched to wrapping shifts
This commit is contained in:
parent
f13e04ff85
commit
74d9e36b99
3 changed files with 31 additions and 48 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -4,7 +4,7 @@ version = 3
|
|||
|
||||
[[package]]
|
||||
name = "icechip"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"tinyrand",
|
||||
]
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
[package]
|
||||
name = "icechip"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
description = "A mostly true to spec Twitter Snowflake implementation, with multiple modes."
|
||||
repository = "https://git.cbax.dev/icechip"
|
||||
repository = "https://git.cbax.dev/cbax/icechip"
|
||||
readme = "README.md"
|
||||
license = "MIT"
|
||||
publish = true
|
||||
|
|
73
src/lib.rs
73
src/lib.rs
|
@ -13,21 +13,15 @@ pub struct Icechip<T> {
|
|||
version: Version,
|
||||
timestamp: T,
|
||||
machine_id: T,
|
||||
sequence_id: T,
|
||||
sequence: T,
|
||||
epoch: Option<u128>,
|
||||
}
|
||||
|
||||
/*impl fmt::Display for Icechip<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.into::<T>());
|
||||
}
|
||||
}*/
|
||||
|
||||
impl Into<u64> for Icechip<u64> {
|
||||
fn into(self) -> u64 {
|
||||
let mut ret: u64 = self.timestamp.checked_shl(23).unwrap();
|
||||
ret = ret + self.machine_id.checked_shl(12).unwrap();
|
||||
ret = ret + self.sequence_id;
|
||||
let mut ret: u64 = self.timestamp.wrapping_shl(23);
|
||||
ret = ret + self.machine_id.wrapping_shl(12);
|
||||
ret = ret + self.sequence;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
@ -36,9 +30,9 @@ impl From<u64> for Icechip<u64> {
|
|||
fn from(value: u64) -> Icechip<u64> {
|
||||
return Icechip {
|
||||
version: Version::ENHANCED,
|
||||
timestamp: value.checked_shr(23).unwrap(),
|
||||
machine_id: value.checked_shl(41).unwrap().checked_shr(53).unwrap(),
|
||||
sequence_id: value.checked_shl(52).unwrap().checked_shr(52).unwrap(),
|
||||
timestamp: value.wrapping_shr(23),
|
||||
machine_id: value.wrapping_shl(41).wrapping_shr(53),
|
||||
sequence: value.wrapping_shl(52).wrapping_shr(52),
|
||||
epoch: None,
|
||||
};
|
||||
}
|
||||
|
@ -46,9 +40,9 @@ impl From<u64> for Icechip<u64> {
|
|||
|
||||
impl Into<u128> for Icechip<u128> {
|
||||
fn into(self) -> u128 {
|
||||
let mut ret: u128 = self.timestamp.checked_shl(64).unwrap();
|
||||
ret = ret + self.machine_id.checked_shl(32).unwrap();
|
||||
ret = ret + self.sequence_id;
|
||||
let mut ret: u128 = self.timestamp.wrapping_shl(64);
|
||||
ret = ret + self.machine_id.wrapping_shl(32);
|
||||
ret = ret + self.sequence;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
@ -57,15 +51,9 @@ impl From<u128> for Icechip<u128> {
|
|||
fn from(value: u128) -> Icechip<u128> {
|
||||
return Icechip {
|
||||
version: Version::EXTENDED,
|
||||
timestamp: value.checked_shr(64).unwrap(),
|
||||
machine_id: value
|
||||
.checked_shr(32)
|
||||
.unwrap()
|
||||
.checked_shl(64)
|
||||
.unwrap()
|
||||
.checked_shr(64)
|
||||
.unwrap(),
|
||||
sequence_id: value.checked_shl(96).unwrap().checked_shr(96).unwrap(),
|
||||
timestamp: value.wrapping_shr(64),
|
||||
machine_id: value.wrapping_shr(32).wrapping_shl(64).wrapping_shr(64),
|
||||
sequence: value.wrapping_shl(96).wrapping_shr(96),
|
||||
epoch: None,
|
||||
};
|
||||
}
|
||||
|
@ -84,8 +72,8 @@ impl<T: Clone> Icechip<T> {
|
|||
return self.machine_id.clone();
|
||||
}
|
||||
|
||||
pub fn sequence_id(&self) -> T {
|
||||
return self.sequence_id.clone();
|
||||
pub fn sequence(&self) -> T {
|
||||
return self.sequence.clone();
|
||||
}
|
||||
|
||||
pub fn version(&self) -> Version {
|
||||
|
@ -99,13 +87,8 @@ impl Icechip<u64> {
|
|||
return Icechip {
|
||||
version: Version::ENHANCED,
|
||||
timestamp: 0,
|
||||
machine_id: rand
|
||||
.next_u64()
|
||||
.checked_shl(41)
|
||||
.unwrap()
|
||||
.checked_shr(53)
|
||||
.unwrap(),
|
||||
sequence_id: 0,
|
||||
machine_id: rand.next_u64().wrapping_shl(41).wrapping_shr(53),
|
||||
sequence: 0,
|
||||
epoch,
|
||||
}
|
||||
.tick();
|
||||
|
@ -116,11 +99,11 @@ impl Icechip<u64> {
|
|||
Ok(o) => {
|
||||
let o = o.as_millis();
|
||||
let curr_ts = match self.epoch {
|
||||
Some(s) => (o - s).checked_shl(23).unwrap().checked_shr(23).unwrap() as u64,
|
||||
None => o.checked_shl(23).unwrap().checked_shr(23).unwrap() as u64,
|
||||
Some(s) => (o - s).wrapping_shl(23).wrapping_shr(23) as u64,
|
||||
None => o.wrapping_shl(23).wrapping_shr(23) as u64,
|
||||
};
|
||||
if self.timestamp == curr_ts {
|
||||
if (self.sequence_id + 1) > 1023 {
|
||||
if (self.sequence + 1) > 1023 {
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
return self.tick();
|
||||
} else {
|
||||
|
@ -128,7 +111,7 @@ impl Icechip<u64> {
|
|||
version: self.version.clone(),
|
||||
timestamp: self.timestamp.clone(),
|
||||
machine_id: self.machine_id.clone(),
|
||||
sequence_id: self.sequence_id.clone() + 1,
|
||||
sequence: self.sequence.clone() + 1,
|
||||
epoch: self.epoch.clone(),
|
||||
};
|
||||
}
|
||||
|
@ -137,7 +120,7 @@ impl Icechip<u64> {
|
|||
version: self.version.clone(),
|
||||
timestamp: curr_ts.clone(),
|
||||
machine_id: self.machine_id.clone(),
|
||||
sequence_id: 0,
|
||||
sequence: 0,
|
||||
epoch: self.epoch.clone(),
|
||||
};
|
||||
}
|
||||
|
@ -154,7 +137,7 @@ impl Icechip<u64> {
|
|||
version: Version::ENHANCED,
|
||||
timestamp: 0,
|
||||
machine_id,
|
||||
sequence_id: 0,
|
||||
sequence: 0,
|
||||
epoch,
|
||||
}
|
||||
.tick();
|
||||
|
@ -172,7 +155,7 @@ impl Icechip<u128> {
|
|||
version: Version::EXTENDED,
|
||||
timestamp: 0,
|
||||
machine_id: rand.next_u128(),
|
||||
sequence_id: 0,
|
||||
sequence: 0,
|
||||
epoch,
|
||||
}
|
||||
.tick();
|
||||
|
@ -187,7 +170,7 @@ impl Icechip<u128> {
|
|||
None => o,
|
||||
};
|
||||
if self.timestamp == curr_ts {
|
||||
if (self.sequence_id + 1) > u32::MAX as u128 {
|
||||
if (self.sequence + 1) > u32::MAX as u128 {
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
return self.tick();
|
||||
} else {
|
||||
|
@ -195,7 +178,7 @@ impl Icechip<u128> {
|
|||
version: self.version.clone(),
|
||||
timestamp: self.timestamp.clone(),
|
||||
machine_id: self.machine_id.clone(),
|
||||
sequence_id: self.sequence_id.clone() + 1,
|
||||
sequence: self.sequence.clone() + 1,
|
||||
epoch: self.epoch.clone(),
|
||||
};
|
||||
}
|
||||
|
@ -204,7 +187,7 @@ impl Icechip<u128> {
|
|||
version: self.version.clone(),
|
||||
timestamp: o.clone(),
|
||||
machine_id: self.machine_id.clone(),
|
||||
sequence_id: 0,
|
||||
sequence: 0,
|
||||
epoch: self.epoch.clone(),
|
||||
};
|
||||
}
|
||||
|
@ -221,7 +204,7 @@ impl Icechip<u128> {
|
|||
version: Version::EXTENDED,
|
||||
timestamp: 0,
|
||||
machine_id,
|
||||
sequence_id: 0,
|
||||
sequence: 0,
|
||||
epoch,
|
||||
}
|
||||
.tick();
|
||||
|
|
Loading…
Reference in a new issue