its working?
This commit is contained in:
parent
a228e1fe2e
commit
b588ddc6f5
1 changed files with 96 additions and 157 deletions
237
src/lib.rs
237
src/lib.rs
|
@ -1,9 +1,9 @@
|
||||||
use std::time::{SystemTime, UNIX_EPOCH};
|
use std::time::{SystemTime, UNIX_EPOCH};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Version {
|
pub enum Version {
|
||||||
ENHANCED,
|
ENHANCED,
|
||||||
EXTENDED
|
EXTENDED,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -12,14 +12,14 @@ pub struct Icechip<T> {
|
||||||
timestamp: T,
|
timestamp: T,
|
||||||
machine_id: T,
|
machine_id: T,
|
||||||
sequence_id: T,
|
sequence_id: T,
|
||||||
epoch: Option<u64>
|
epoch: Option<u128>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Icechip<T> {
|
/*impl fmt::Display for Icechip<T> {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{}", self.into::<T>());
|
write!(f, "{}", self.into::<T>());
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
impl Into<u64> for Icechip<u64> {
|
impl Into<u64> for Icechip<u64> {
|
||||||
fn into(self) -> u64 {
|
fn into(self) -> u64 {
|
||||||
|
@ -31,13 +31,13 @@ impl Into<u64> for Icechip<u64> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<u64> for Icechip<u64> {
|
impl From<u64> for Icechip<u64> {
|
||||||
fn from (value: u64) -> Icechip<u64> {
|
fn from(value: u64) -> Icechip<u64> {
|
||||||
return Icechip {
|
return Icechip {
|
||||||
version: Version::ENHANCED,
|
version: Version::ENHANCED,
|
||||||
timestamp: value.checked_shr(23).unwrap(),
|
timestamp: value.checked_shr(23).unwrap(),
|
||||||
machine_id: value.checked_shl(41).unwrap().checked_shr(53).unwrap(),
|
machine_id: value.checked_shl(41).unwrap().checked_shr(53).unwrap(),
|
||||||
sequence_id: value.checked_shl(52).unwrap().checked_shr(52).unwrap(),
|
sequence_id: value.checked_shl(52).unwrap().checked_shr(52).unwrap(),
|
||||||
epoch: None
|
epoch: None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,89 +56,68 @@ impl From<u128> for Icechip<u128> {
|
||||||
return Icechip {
|
return Icechip {
|
||||||
version: Version::EXTENDED,
|
version: Version::EXTENDED,
|
||||||
timestamp: value.checked_shr(64).unwrap(),
|
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(),
|
sequence_id: value.checked_shl(96).unwrap().checked_shr(96).unwrap(),
|
||||||
epoch: None
|
epoch: None,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Icechip<T> {
|
||||||
|
pub fn set_epoch(mut self, epoch: u128) {
|
||||||
|
self.epoch = Some(epoch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Icechip<u64> {
|
impl Icechip<u64> {
|
||||||
pub fn new(version: Version, epoch: Option<u64>) -> Icechip<T> {
|
pub fn new(version: Version, epoch: Option<u128>) -> Icechip<u64> {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
// the offset will get subtracted from the unix epoch before being used in the Icechip.
|
fn offset_epoch(&self, timestamp: u128) -> u64 {
|
||||||
pub fn set_epoch(mut self, epoch: u64) {
|
match self.epoch {
|
||||||
self.epoch = Some(epoch);
|
Some(s) => return (timestamp - s) as u64,
|
||||||
|
None => return timestamp as u64,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tick<T: Shl + Shr + Clone>(&self) -> Icechip<T> {
|
pub fn tick(&self) -> Icechip<u64> {
|
||||||
/*
|
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||||
- 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) => {
|
Ok(o) => {
|
||||||
match self.version {
|
let o = o.as_millis();
|
||||||
Version::ENHANCED => {
|
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(),
|
None => o.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.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();
|
} else {
|
||||||
} else {
|
return Icechip {
|
||||||
return Icechip {
|
version: self.version.clone(),
|
||||||
version: self.version.clone(),
|
timestamp: self.timestamp.clone(),
|
||||||
timestamp: self.timestamp.clone(),
|
machine_id: self.machine_id.clone(),
|
||||||
machine_id: self.machine_id.clone(),
|
sequence_id: self.sequence_id.clone() + 1,
|
||||||
sequence_id: self.sequence_id.clone() + 1,
|
epoch: self.epoch.clone(),
|
||||||
epoch: self.epoch.clone()
|
};
|
||||||
}
|
}
|
||||||
}
|
} 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()
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
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(_) => {
|
Err(_) => {
|
||||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||||
return self.tick();
|
return self.tick();
|
||||||
|
@ -146,7 +125,7 @@ impl Icechip<u64> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_id(machine_id: u32, version: Version) -> Icechip<T> {
|
pub fn with_id(machine_id: u32, version: Version) -> Icechip<u64> {
|
||||||
todo!();
|
todo!();
|
||||||
/*match version {
|
/*match version {
|
||||||
Version::ENHANCED => {
|
Version::ENHANCED => {
|
||||||
|
@ -160,81 +139,41 @@ impl Icechip<u64> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Icechip<u128> {
|
impl Icechip<u128> {
|
||||||
pub fn new(version: Version, epoch: Option<u64>) -> Icechip<T> {
|
pub fn new(version: Version, epoch: Option<u128>) -> Icechip<u128> {
|
||||||
todo!();
|
todo!();
|
||||||
}
|
}
|
||||||
|
|
||||||
// the offset will get subtracted from the unix epoch before being used in the Icechip.
|
pub fn tick(&self) -> Icechip<u128> {
|
||||||
pub fn set_epoch(mut self, epoch: u64) {
|
match SystemTime::now().duration_since(UNIX_EPOCH) {
|
||||||
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) => {
|
Ok(o) => {
|
||||||
match self.version {
|
let o = o.as_millis();
|
||||||
Version::ENHANCED => {
|
let curr_ts = match self.epoch {
|
||||||
let curr_ts = match self.epoch {
|
Some(s) => o - s as u128,
|
||||||
Some(s) => (o - s).checked_shl(23).unwrap().checked_shr(23).unwrap(),
|
None => o,
|
||||||
None => o.checked_shl(23).unwrap().checked_shr(23).unwrap(),
|
};
|
||||||
}
|
if self.timestamp == curr_ts {
|
||||||
if self.timestamp == curr_ts {
|
if (self.sequence_id + 1) > u32::MAX as u128 {
|
||||||
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();
|
} else {
|
||||||
} else {
|
return Icechip {
|
||||||
return Icechip {
|
version: self.version.clone(),
|
||||||
version: self.version.clone(),
|
timestamp: self.timestamp.clone(),
|
||||||
timestamp: self.timestamp.clone(),
|
machine_id: self.machine_id.clone(),
|
||||||
machine_id: self.machine_id.clone(),
|
sequence_id: self.sequence_id.clone() + 1,
|
||||||
sequence_id: self.sequence_id.clone() + 1,
|
epoch: self.epoch.clone(),
|
||||||
epoch: self.epoch.clone()
|
};
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
return Icechip {
|
||||||
return Icechip {
|
version: self.version.clone(),
|
||||||
version: self.version.clone(),
|
timestamp: o.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()
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
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(_) => {
|
Err(_) => {
|
||||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||||
return self.tick();
|
return self.tick();
|
||||||
|
@ -242,7 +181,7 @@ impl Icechip<u128> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_id(machine_id: u32, version: Version) -> Icechip<T> {
|
pub fn with_id(machine_id: u32, version: Version) -> Icechip<u128> {
|
||||||
todo!();
|
todo!();
|
||||||
/*match version {
|
/*match version {
|
||||||
Version::ENHANCED => {
|
Version::ENHANCED => {
|
||||||
|
|
Loading…
Reference in a new issue