refactor
This commit is contained in:
parent
1a00e6f5f9
commit
a228e1fe2e
1 changed files with 97 additions and 1 deletions
98
src/lib.rs
98
src/lib.rs
|
@ -63,7 +63,103 @@ impl From<u128> for Icechip<u128> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Shl + Shr + Clone> Icechip<T> {
|
||||
impl Icechip<u64> {
|
||||
pub fn new(version: Version, epoch: Option<u64>) -> Icechip<T> {
|
||||
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<T: Shl + Shr + Clone>(&self) -> Icechip<T> {
|
||||
/*
|
||||
- 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() {
|
||||
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()
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
return self.tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_id(machine_id: u32, version: Version) -> Icechip<T> {
|
||||
todo!();
|
||||
/*match version {
|
||||
Version::ENHANCED => {
|
||||
|
||||
},
|
||||
Version::EXTENDED => {
|
||||
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
impl Icechip<u128> {
|
||||
pub fn new(version: Version, epoch: Option<u64>) -> Icechip<T> {
|
||||
todo!();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue