getting there

This commit is contained in:
cbax 2024-09-24 20:19:24 -04:00
parent 515b8f2d42
commit f4a9ddf8dc
3 changed files with 104 additions and 7 deletions

7
Cargo.lock generated Normal file
View file

@ -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"

View file

@ -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

View file

@ -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<T> {
version: Version,
timestamp: T,
machine_id: T,
sequence_id: T,
epoch: Option<u64>
}
impl Into<u64> for Icechip<u64> {
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<u64> for Icechip<u64> {
fn from (value: u64) -> Icechip<u64> {
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<u128> for Icechip<u128> {
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<u128> for Icechip<u128> {
fn from(value: u128) -> Icechip<u128> {
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<T> Icechip<T> {
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_offset(mut self, offset: u64) {
self.epoch = Some(offset);
}
pub fn tick(&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
*/
todo!();
}
pub fn with_id(machine_id: u32, version: Version) -> Icechip<T> {
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());
}
}