diff --git a/Cargo.lock b/Cargo.lock index 1994bd9..6193c1e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "icechip" -version = "0.1.0" +version = "0.1.1" dependencies = [ "tinyrand", ] diff --git a/Cargo.toml b/Cargo.toml index d9f29e2..6914916 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/lib.rs b/src/lib.rs index d552b3c..eb417e5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,21 +13,15 @@ pub struct Icechip { version: Version, timestamp: T, machine_id: T, - sequence_id: T, + sequence: T, epoch: Option, } -/*impl fmt::Display for Icechip { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.into::()); - } -}*/ - impl Into for Icechip { 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 for Icechip { fn from(value: u64) -> Icechip { 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 for Icechip { impl Into for Icechip { 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 for Icechip { fn from(value: u128) -> Icechip { 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 Icechip { 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 { 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 { 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 { 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 { 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 { version: Version::ENHANCED, timestamp: 0, machine_id, - sequence_id: 0, + sequence: 0, epoch, } .tick(); @@ -172,7 +155,7 @@ impl Icechip { version: Version::EXTENDED, timestamp: 0, machine_id: rand.next_u128(), - sequence_id: 0, + sequence: 0, epoch, } .tick(); @@ -187,7 +170,7 @@ impl Icechip { 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 { 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 { 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 { version: Version::EXTENDED, timestamp: 0, machine_id, - sequence_id: 0, + sequence: 0, epoch, } .tick();