From 888b1cf5d3502d0fdd837753c428180163f2741b Mon Sep 17 00:00:00 2001 From: Alexander Evgin Date: Sun, 23 Feb 2025 01:59:32 +0400 Subject: [PATCH 1/2] use unsigned integers for Byte/Core/Time units --- src/models/pin.rs | 4 +-- src/models/serialization_helpers.rs | 42 ++++++++++++++--------------- src/models/task.rs | 8 +++--- src/models/worker.rs | 16 +++++------ 4 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/models/pin.rs b/src/models/pin.rs index b19f468..8511998 100644 --- a/src/models/pin.rs +++ b/src/models/pin.rs @@ -205,8 +205,8 @@ impl From for PinSpec { fn from(proto: gevulot::PinSpec) -> Self { PinSpec { cid: None, - bytes: (proto.bytes as i64).into(), - time: (proto.time as i64).into(), + bytes: proto.bytes.into(), + time: proto.time.into(), redundancy: proto.redundancy as i64, fallback_urls: Some(proto.fallback_urls), } diff --git a/src/models/serialization_helpers.rs b/src/models/serialization_helpers.rs index 94e712e..45bc40a 100644 --- a/src/models/serialization_helpers.rs +++ b/src/models/serialization_helpers.rs @@ -12,35 +12,35 @@ use serde::{Deserialize, Serialize}; /// Trait for specifying default multiplication factors for byte units pub trait DefaultFactor { - const FACTOR: i64; + const FACTOR: u64; } /// Default factor of 1 (no multiplication) #[derive(Debug)] pub struct DefaultFactorOne; impl DefaultFactor for DefaultFactorOne { - const FACTOR: i64 = 1; + const FACTOR: u64 = 1; } /// Default factor of 1KB (1024 bytes) #[derive(Debug)] pub struct DefaultFactorOneKilobyte; impl DefaultFactor for DefaultFactorOneKilobyte { - const FACTOR: i64 = 1024; + const FACTOR: u64 = 1024; } /// Default factor of 1MB (1024 * 1024 bytes) #[derive(Debug)] pub struct DefaultFactorOneMegabyte; impl DefaultFactor for DefaultFactorOneMegabyte { - const FACTOR: i64 = 1024 * 1024; + const FACTOR: u64 = 1024 * 1024; } /// Default factor of 1GB (1024 * 1024 * 1024 bytes) #[derive(Debug)] pub struct DefaultFactorOneGigabyte; impl DefaultFactor for DefaultFactorOneGigabyte { - const FACTOR: i64 = 1024 * 1024 * 1024; + const FACTOR: u64 = 1024 * 1024 * 1024; } /// Type for handling byte sizes with configurable default factors @@ -66,7 +66,7 @@ impl DefaultFactor for DefaultFactorOneGigabyte { #[derive(Debug, Serialize, Deserialize, Eq)] #[serde(untagged)] pub enum ByteUnit { - Number(i64), + Number(u64), String(String), #[serde(skip)] Factor(std::marker::PhantomData), @@ -74,14 +74,14 @@ pub enum ByteUnit { impl ByteUnit { /// Convert to number of bytes - pub fn bytes(&self) -> Result { + pub fn bytes(&self) -> Result { match self { ByteUnit::Number(n) => Ok(*n * D::FACTOR), ByteUnit::String(s) => { if s.chars().all(|c| c.is_ascii_digit()) { - return Ok(s.parse::().map_err(|e| e.to_string())? * D::FACTOR); + return Ok(s.parse::().map_err(|e| e.to_string())? * D::FACTOR); } - s.parse::().map(|b| b.0 as i64) + s.parse::().map(|b| b.as_u64()) } ByteUnit::Factor(_) => Ok(D::FACTOR), } @@ -103,8 +103,8 @@ impl FromStr for ByteUnit { } } -impl From for ByteUnit { - fn from(n: i64) -> Self { +impl From for ByteUnit { + fn from(n: u64) -> Self { ByteUnit::Number(n) } } @@ -133,13 +133,13 @@ impl From for ByteUnit { #[derive(Debug, Serialize, Deserialize, Eq)] #[serde(untagged)] pub enum CoreUnit { - Number(i64), + Number(u64), String(String), } impl CoreUnit { /// Convert to millicores (1 core = 1000 millicores) - pub fn millicores(&self) -> Result { + pub fn millicores(&self) -> Result { match self { CoreUnit::Number(n) => Ok(*n * 1000), // Default factor without unit is 1000 CoreUnit::String(s) => { @@ -147,7 +147,7 @@ impl CoreUnit { let numeric: String = s.chars().take_while(|c| c.is_digit(10)).collect(); // Extract and normalize unit part let unit = s[numeric.len()..].to_lowercase().replace(" ", ""); - let base: i64 = numeric + let base: u64 = numeric .parse() .map_err(|e| format!("Invalid number: {}", e))?; @@ -185,8 +185,8 @@ impl FromStr for CoreUnit { } } -impl From for CoreUnit { - fn from(n: i64) -> Self { +impl From for CoreUnit { + fn from(n: u64) -> Self { CoreUnit::Number(n) } } @@ -213,19 +213,19 @@ impl From for CoreUnit { #[derive(Debug, Serialize, Deserialize, Eq)] #[serde(untagged)] pub enum TimeUnit { - Number(i64), + Number(u64), String(String), } impl TimeUnit { /// Convert to seconds - pub fn seconds(&self) -> Result { + pub fn seconds(&self) -> Result { match self { TimeUnit::Number(n) => Ok(*n), TimeUnit::String(s) => { let duration = humantime::parse_duration(s).map_err(|e| format!("Invalid duration: {}", e))?; - Ok(duration.as_secs() as i64) + Ok(duration.as_secs() as u64) } } } @@ -248,8 +248,8 @@ impl FromStr for TimeUnit { } } -impl From for TimeUnit { - fn from(n: i64) -> Self { +impl From for TimeUnit { + fn from(n: u64) -> Self { TimeUnit::Number(n) } } diff --git a/src/models/task.rs b/src/models/task.rs index a307650..5fa52ac 100644 --- a/src/models/task.rs +++ b/src/models/task.rs @@ -203,10 +203,10 @@ impl From for TaskSpec { }) .collect(), resources: TaskResources { - cpus: (proto.cpus as i64).into(), - gpus: (proto.gpus as i64).into(), - memory: (proto.memory as i64).into(), - time: (proto.time as i64).into(), + cpus: proto.cpus.into(), + gpus: proto.gpus.into(), + memory: proto.memory.into(), + time: proto.time.into(), }, store_stdout: proto.store_stdout, store_stderr: proto.store_stderr, diff --git a/src/models/worker.rs b/src/models/worker.rs index 7f22639..9592e7a 100644 --- a/src/models/worker.rs +++ b/src/models/worker.rs @@ -129,10 +129,10 @@ impl From for WorkerSpec { fn from(proto: gevulot::WorkerSpec) -> Self { // Convert protobuf spec to internal spec WorkerSpec { - cpus: (proto.cpus as i64).into(), - gpus: (proto.gpus as i64).into(), - memory: (proto.memory as i64).into(), - disk: (proto.disk as i64).into(), + cpus: proto.cpus.into(), + gpus: proto.gpus.into(), + memory: proto.memory.into(), + disk: proto.disk.into(), } } } @@ -160,10 +160,10 @@ impl From for WorkerStatus { fn from(proto: gevulot::WorkerStatus) -> Self { // Convert protobuf status to internal status WorkerStatus { - cpus_used: (proto.cpus_used as i64).into(), - gpus_used: (proto.gpus_used as i64).into(), - memory_used: (proto.memory_used as i64).into(), - disk_used: (proto.disk_used as i64).into(), + cpus_used: proto.cpus_used.into(), + gpus_used: proto.gpus_used.into(), + memory_used: proto.memory_used.into(), + disk_used: proto.disk_used.into(), exit_announced_at: proto.exit_announced_at as i64, } } From d6aca04f2902459e855749c08ebd8f95166bd20d Mon Sep 17 00:00:00 2001 From: Alexander Evgin Date: Tue, 25 Feb 2025 00:49:01 +0400 Subject: [PATCH 2/2] Expose byte/core/time units in public API --- src/models/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/models/mod.rs b/src/models/mod.rs index c8c507a..9028436 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1,7 +1,10 @@ use serde::{Deserialize, Serialize}; mod serialization_helpers; -use serialization_helpers::*; +pub use serialization_helpers::{ + ByteUnit, CoreUnit, DefaultFactor, DefaultFactorOne, DefaultFactorOneGigabyte, + DefaultFactorOneKilobyte, DefaultFactorOneMegabyte, TimeUnit, +}; mod metadata; pub use metadata::{Label, Metadata};