From 5c6ca9465f44dbcb1f8309939572c86eb09c1ba0 Mon Sep 17 00:00:00 2001 From: Louis Thiery Date: Thu, 5 Jun 2025 19:27:34 -0700 Subject: [PATCH] add usize support --- humanbyte-derive/src/lib.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/humanbyte-derive/src/lib.rs b/humanbyte-derive/src/lib.rs index 1e20c276..7284d97c 100644 --- a/humanbyte-derive/src/lib.rs +++ b/humanbyte-derive/src/lib.rs @@ -242,6 +242,34 @@ pub fn humanbyte_ops(input: TokenStream) -> TokenStream { } } + #[cfg(target_pointer_width = "64")] + impl core::ops::Add<#name> for usize { + type Output = #name; + #[inline(always)] + fn add(self, rhs: #name) -> #name { + #name(rhs.0 + (self as u64)) + } + } + + + #[cfg(target_pointer_width = "64")] + impl core::ops::Sub<#name> for usize { + type Output = #name; + #[inline(always)] + fn sub(self, rhs: #name) -> #name { + #name(self as u64 - rhs.0) + } + } + + #[cfg(target_pointer_width = "64")] + impl core::ops::Mul<#name> for usize { + type Output = #name; + #[inline(always)] + fn mul(self, rhs: #name) -> #name { + #name(rhs.0 * (self as u64)) + } + } + impl #name { /// Provides `HumanByteRange` with explicit lower and upper bounds. pub fn range>(start: I, stop: I) -> ::humanbyte::HumanByteRange { @@ -340,6 +368,13 @@ pub fn humanbyte_parse(input: TokenStream) -> TokenStream { pub const fn as_u64(&self) -> u64 { self.0 } + + /// Returns the inner value as usize. + #[cfg(target_pointer_width = "64")] + #[inline(always)] + pub const fn as_usize(&self) -> usize { + self.0 as usize + } } };