From f4a9ddf8dc5a976e3179692b3b0a179702050512 Mon Sep 17 00:00:00 2001 From: cbax Date: Tue, 24 Sep 2024 20:19:24 -0400 Subject: [PATCH] getting there --- Cargo.lock | 7 ++++ README.md | 4 +-- src/lib.rs | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..71d70e1 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "icechip" +version = "0.1.0" diff --git a/README.md b/README.md index 5550c4a..236f470 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Adheres to the twitter specification, but also has an improved mode. ## Twitter Specification | u64 or i64 | | | | |:-:|:-:|:-:|:-:| -| bits 0-40 | bits 41-51 | bits 51-62 | 63 | +| bits 0-40 | bits 41-51 | bits 52-62 | 63 | | time since an epoch in ms | machine id | sequence number | sign | ## Enhanced Specification @@ -16,7 +16,7 @@ Utilizes a 64 bit unsigned integer, moves the now spare sign bit and one sequenc | u64 ||| |:-:|:-:|:-:| -| 0-40 | 41-52 | 53-63 | +| 0-40 | 41-53 | 54-63 | | time since an epoch in ms | machine id | sequence number | ## Extended Enhanced Specification diff --git a/src/lib.rs b/src/lib.rs index b93cf3f..056d76e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,89 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +#[derive(Debug)] +pub enum Version { + ENHANCED, + EXTENDED +} + +#[derive(Debug)] +pub struct Icechip { + version: Version, + timestamp: T, + machine_id: T, + sequence_id: T, + epoch: Option +} + +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; + return ret; + } +} + +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(), + epoch: None + }; + } +} + +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; + return ret; + } +} + +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(), + epoch: None + }; + } +} + +impl 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_offset(mut self, offset: u64) { + self.epoch = Some(offset); + } + + 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 + */ + todo!(); + } + + pub fn with_id(machine_id: u32, version: Version) -> Icechip { + todo!(); + /*match version { + Version::ENHANCED => { + + }, + Version::EXTENDED => { + + } + }*/ + } } #[cfg(test)] @@ -7,8 +91,14 @@ mod tests { use super::*; #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + fn from_into_u128() { + let test_chip = Icechip::from(0x0123456789ABCDEFu128); + assert_eq!(0x0123456789ABCDEFu128, test_chip.into()); + } + + #[test] + fn from_into_u64() { + let test_chip = Icechip::from(0x0123456789ABCDEFu64); + assert_eq!(0x0123456789ABCDEFu64, test_chip.into()); } }