diff --git a/src/lib.rs b/src/lib.rs index bc5943c..ed05f5c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,9 @@ use std::time::{SystemTime, UNIX_EPOCH}; -#[derive(Debug)] +#[derive(Clone, Debug)] pub enum Version { ENHANCED, - EXTENDED + EXTENDED, } #[derive(Debug)] @@ -12,14 +12,14 @@ pub struct Icechip { timestamp: T, machine_id: T, sequence_id: T, - epoch: Option + epoch: Option, } -impl fmt::Display for Icechip { +/*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 { @@ -31,13 +31,13 @@ impl Into for Icechip { } impl From for Icechip { - fn from (value: u64) -> 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(), - epoch: None + epoch: None, }; } } @@ -56,203 +56,142 @@ impl From for 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(), + 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(), - epoch: None + epoch: None, }; } } +impl Icechip { + pub fn set_epoch(mut self, epoch: u128) { + self.epoch = Some(epoch); + } +} + impl Icechip { - pub fn new(version: Version, epoch: Option) -> Icechip { + pub fn new(version: Version, epoch: Option) -> Icechip { todo!(); } - // the offset will get subtracted from the unix epoch before being used in the Icechip. - pub fn set_epoch(mut self, epoch: u64) { - self.epoch = Some(epoch); + fn offset_epoch(&self, timestamp: u128) -> u64 { + match self.epoch { + Some(s) => return (timestamp - s) as u64, + None => return timestamp as u64, + } } - pub fn tick(&self) -> Icechip { - /* - - if current timestamp in relation to self does not equal self, return an updated value, else increment the sequence - - if sequence is going to overflow, sleep 1 ms - */ - match SystemTime::now().duration_since(UNIX_EPOCH).as_millis() { + pub fn tick(&self) -> Icechip { + match SystemTime::now().duration_since(UNIX_EPOCH) { Ok(o) => { - match self.version { - Version::ENHANCED => { - let curr_ts = match self.epoch { - Some(s) => (o - s).checked_shl(23).unwrap().checked_shr(23).unwrap(), - None => o.checked_shl(23).unwrap().checked_shr(23).unwrap(), - } - if self.timestamp == curr_ts { - if (self.sequence_id + 1) > 1023 { - std::thread::sleep(std::time::Duration::from_millis(1)); - return self.tick(); - } else { - return Icechip { - version: self.version.clone(), - timestamp: self.timestamp.clone(), - machine_id: self.machine_id.clone(), - sequence_id: self.sequence_id.clone() + 1, - epoch: self.epoch.clone() - } - } - } else { - return Icechip { - version: self.version.clone(), - timestamp: curr_ts.clone(), - machine_id: self.machine_id.clone(), - sequence_id: 0, - epoch: self.epoch.clone() - } - } - }, - Version::EXTENDED => { - let curr_ts = match self.epoch { - Some(s) => (o - s), - None => o, - } - if self.timestamp == curr_ts { - if (self.sequence_id + 1) > u32::MAX { - std::thread::sleep(std::time:Duration::from_millis(1)); - return self.tick(); - } else { - return Icechip { - version: self.version.clone(), - timestamp: self.timestamp.clone(), - machine_id: self.machine_id.clone(), - sequence_id: self.sequence_id.clone() + 1, - epoch: self.epoch.clone() - } - } - } else { - return Icechip { - version: self.version.clone(), - timestamp: o.clone(), - machine_id: self.machine_id.clone(), - sequence_id: 0, - epoch: self.epoch.clone() - } - } - }, + let o = o.as_millis(); + let curr_ts = match self.epoch { + Some(s) => (o - s).checked_shl(23).unwrap().checked_shr(23).unwrap(), + None => o.checked_shl(23).unwrap().checked_shr(23).unwrap(), + }; + if self.timestamp as u128 == curr_ts { + if (self.sequence_id + 1) > 1023 { + std::thread::sleep(std::time::Duration::from_millis(1)); + return self.tick(); + } else { + return Icechip { + version: self.version.clone(), + timestamp: self.timestamp.clone(), + machine_id: self.machine_id.clone(), + sequence_id: self.sequence_id.clone() + 1, + epoch: self.epoch.clone(), + }; + } + } else { + return Icechip { + version: self.version.clone(), + timestamp: self.offset_epoch(curr_ts.clone()), + machine_id: self.machine_id.clone(), + sequence_id: 0, + epoch: self.epoch.clone(), + }; } - }, + } Err(_) => { std::thread::sleep(std::time::Duration::from_millis(1)); return self.tick(); } } } - - pub fn with_id(machine_id: u32, version: Version) -> Icechip { + + pub fn with_id(machine_id: u32, version: Version) -> Icechip { todo!(); /*match version { Version::ENHANCED => { - + }, Version::EXTENDED => { - + } }*/ - } + } } impl Icechip { - pub fn new(version: Version, epoch: Option) -> Icechip { + pub fn new(version: Version, epoch: Option) -> Icechip { todo!(); } - // the offset will get subtracted from the unix epoch before being used in the Icechip. - pub fn set_epoch(mut self, epoch: u64) { - self.epoch = Some(epoch); - } - - pub fn tick(&self) -> Icechip { - /* - - if current timestamp in relation to self does not equal self, return an updated value, else increment the sequence - - if sequence is going to overflow, sleep 1 ms - */ - match SystemTime::now().duration_since(UNIX_EPOCH).as_millis() { + pub fn tick(&self) -> Icechip { + match SystemTime::now().duration_since(UNIX_EPOCH) { Ok(o) => { - match self.version { - Version::ENHANCED => { - let curr_ts = match self.epoch { - Some(s) => (o - s).checked_shl(23).unwrap().checked_shr(23).unwrap(), - None => o.checked_shl(23).unwrap().checked_shr(23).unwrap(), - } - if self.timestamp == curr_ts { - if (self.sequence_id + 1) > 1023 { - std::thread::sleep(std::time::Duration::from_millis(1)); - return self.tick(); - } else { - return Icechip { - version: self.version.clone(), - timestamp: self.timestamp.clone(), - machine_id: self.machine_id.clone(), - sequence_id: self.sequence_id.clone() + 1, - epoch: self.epoch.clone() - } - } - } else { - return Icechip { - version: self.version.clone(), - timestamp: curr_ts.clone(), - machine_id: self.machine_id.clone(), - sequence_id: 0, - epoch: self.epoch.clone() - } - } - }, - Version::EXTENDED => { - let curr_ts = match self.epoch { - Some(s) => (o - s), - None => o, - } - if self.timestamp == curr_ts { - if (self.sequence_id + 1) > u32::MAX { - std::thread::sleep(std::time:Duration::from_millis(1)); - return self.tick(); - } else { - return Icechip { - version: self.version.clone(), - timestamp: self.timestamp.clone(), - machine_id: self.machine_id.clone(), - sequence_id: self.sequence_id.clone() + 1, - epoch: self.epoch.clone() - } - } - } else { - return Icechip { - version: self.version.clone(), - timestamp: o.clone(), - machine_id: self.machine_id.clone(), - sequence_id: 0, - epoch: self.epoch.clone() - } - } - }, + let o = o.as_millis(); + let curr_ts = match self.epoch { + Some(s) => o - s as u128, + None => o, + }; + if self.timestamp == curr_ts { + if (self.sequence_id + 1) > u32::MAX as u128 { + std::thread::sleep(std::time::Duration::from_millis(1)); + return self.tick(); + } else { + return Icechip { + version: self.version.clone(), + timestamp: self.timestamp.clone(), + machine_id: self.machine_id.clone(), + sequence_id: self.sequence_id.clone() + 1, + epoch: self.epoch.clone(), + }; + } + } else { + return Icechip { + version: self.version.clone(), + timestamp: o.clone(), + machine_id: self.machine_id.clone(), + sequence_id: 0, + epoch: self.epoch.clone(), + }; } - }, + } Err(_) => { std::thread::sleep(std::time::Duration::from_millis(1)); return self.tick(); } } } - - pub fn with_id(machine_id: u32, version: Version) -> Icechip { + + pub fn with_id(machine_id: u32, version: Version) -> Icechip { todo!(); /*match version { Version::ENHANCED => { - + }, Version::EXTENDED => { - + } }*/ - } + } } #[cfg(test)]