aboutsummaryrefslogtreecommitdiff
path: root/azalea-core/src
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-15 23:10:55 -0600
committermat <github@matdoes.dev>2021-12-15 23:10:55 -0600
commit9642558f8f8d983a7087f15d68be8cf07a85f0c2 (patch)
tree5f0a967f005cd5db510a13ab290c8ad6669b25aa /azalea-core/src
parent72aefe871ca4983431b1a0b707b472e73ffea836 (diff)
downloadazalea-drasl-9642558f8f8d983a7087f15d68be8cf07a85f0c2.tar.xz
azalea
Diffstat (limited to 'azalea-core/src')
-rw-r--r--azalea-core/src/lib.rs12
-rw-r--r--azalea-core/src/serializable_uuid.rs54
2 files changed, 66 insertions, 0 deletions
diff --git a/azalea-core/src/lib.rs b/azalea-core/src/lib.rs
new file mode 100644
index 00000000..592988a3
--- /dev/null
+++ b/azalea-core/src/lib.rs
@@ -0,0 +1,12 @@
+//! Random miscellaneous things like UUIDs
+
+mod serializable_uuid;
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn it_works() {
+ let result = 2 + 2;
+ assert_eq!(result, 4);
+ }
+}
diff --git a/azalea-core/src/serializable_uuid.rs b/azalea-core/src/serializable_uuid.rs
new file mode 100644
index 00000000..91b5f2d8
--- /dev/null
+++ b/azalea-core/src/serializable_uuid.rs
@@ -0,0 +1,54 @@
+use uuid::Uuid;
+
+pub trait SerializableUuid {
+ fn to_int_array(&self) -> [u32; 4];
+ fn from_int_array(array: [u32; 4]) -> Self;
+}
+
+// private static int[] leastMostToIntArray(long l, long l2) {
+// return new int[]{(int)(l >> 32), (int)l, (int)(l2 >> 32), (int)l2};
+// }
+
+fn least_most_to_int_array(most: u64, least: u64) -> [u32; 4] {
+ [
+ (most >> 32) as u32,
+ most as u32,
+ (least >> 32) as u32,
+ least as u32,
+ ]
+}
+
+impl SerializableUuid for Uuid {
+ fn to_int_array(&self) -> [u32; 4] {
+ let most_significant_bits = (self.as_u128() >> 64) as u64;
+ let least_significant_bits = (self.as_u128() & 0xffffffffffffffff) as u64;
+
+ least_most_to_int_array(most_significant_bits, least_significant_bits)
+ }
+
+ fn from_int_array(array: [u32; 4]) -> Self {
+ let most = ((array[0] as u64) << 32) | ((array[1] as u64) & 0xFFFFFFFF);
+ let least = ((array[2] as u64) << 32) | ((array[3] as u64) & 0xFFFFFFFF);
+
+ Uuid::from_u128((((most as u128) << 64) | least as u128).into())
+ }
+}
+
+mod tests {
+ use super::*;
+
+ #[test]
+ fn to_int_array() {
+ let u = Uuid::parse_str("6536bfed-8695-48fd-83a1-ecd24cf2a0fd").unwrap();
+ assert_eq!(
+ u.to_int_array(),
+ [0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd]
+ );
+ }
+
+ #[test]
+ fn from_int_array() {
+ let u = Uuid::from_int_array([0x6536bfed, 0x869548fd, 0x83a1ecd2, 0x4cf2a0fd]);
+ assert_eq!(u.to_string(), "6536bfed-8695-48fd-83a1-ecd24cf2a0fd");
+ }
+}