Skip to content

Commit

Permalink
no_std fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lthiery committed Jan 16, 2025
1 parent 7dcf2c7 commit 3fdc7a5
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions bytelike-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn bytelike_ops(input: TokenStream) -> TokenStream {
let name = &input.ident;

let expanded = quote! {
impl std::ops::Add<#name> for #name {
impl core::ops::Add<#name> for #name {
type Output = #name;

#[inline(always)]
Expand All @@ -91,14 +91,14 @@ pub fn bytelike_ops(input: TokenStream) -> TokenStream {
}
}

impl std::ops::AddAssign<#name> for #name {
impl core::ops::AddAssign<#name> for #name {
#[inline(always)]
fn add_assign(&mut self, rhs: #name) {
self.0 += rhs.0
}
}

impl<T> std::ops::Add<T> for #name
impl<T> core::ops::Add<T> for #name
where
T: Into<u64>,
{
Expand All @@ -109,7 +109,7 @@ pub fn bytelike_ops(input: TokenStream) -> TokenStream {
}
}

impl<T> std::ops::AddAssign<T> for #name
impl<T> core::ops::AddAssign<T> for #name
where
T: Into<u64>,
{
Expand All @@ -119,7 +119,7 @@ pub fn bytelike_ops(input: TokenStream) -> TokenStream {
}
}

impl std::ops::Sub<#name> for #name {
impl core::ops::Sub<#name> for #name {
type Output = #name;

#[inline(always)]
Expand All @@ -128,14 +128,14 @@ pub fn bytelike_ops(input: TokenStream) -> TokenStream {
}
}

impl std::ops::SubAssign<#name> for #name {
impl core::ops::SubAssign<#name> for #name {
#[inline(always)]
fn sub_assign(&mut self, rhs: #name) {
self.0 -= rhs.0
}
}

impl<T> std::ops::Sub<T> for #name
impl<T> core::ops::Sub<T> for #name
where
T: Into<u64>,
{
Expand All @@ -147,7 +147,7 @@ pub fn bytelike_ops(input: TokenStream) -> TokenStream {
}
}

impl<T> std::ops::SubAssign<T> for #name
impl<T> core::ops::SubAssign<T> for #name
where
T: Into<u64>,
{
Expand All @@ -157,7 +157,7 @@ pub fn bytelike_ops(input: TokenStream) -> TokenStream {
}
}

impl<T> std::ops::Mul<T> for #name
impl<T> core::ops::Mul<T> for #name
where
T: Into<u64>,
{
Expand All @@ -168,7 +168,7 @@ pub fn bytelike_ops(input: TokenStream) -> TokenStream {
}
}

impl<T> std::ops::MulAssign<T> for #name
impl<T> core::ops::MulAssign<T> for #name
where
T: Into<u64>,
{
Expand All @@ -178,63 +178,63 @@ pub fn bytelike_ops(input: TokenStream) -> TokenStream {
}
}

impl std::ops::Add<#name> for u64 {
impl core::ops::Add<#name> for u64 {
type Output = #name;
#[inline(always)]
fn add(self, rhs: #name) -> #name {
#name(rhs.0 + self)
}
}

impl std::ops::Add<#name> for u32 {
impl core::ops::Add<#name> for u32 {
type Output = #name;
#[inline(always)]
fn add(self, rhs: #name) -> #name {
#name(rhs.0 + (self as u64))
}
}

impl std::ops::Add<#name> for u16 {
impl core::ops::Add<#name> for u16 {
type Output = #name;
#[inline(always)]
fn add(self, rhs: #name) -> #name {
#name(rhs.0 + (self as u64))
}
}

impl std::ops::Add<#name> for u8 {
impl core::ops::Add<#name> for u8 {
type Output = #name;
#[inline(always)]
fn add(self, rhs: #name) -> #name {
#name(rhs.0 + (self as u64))
}
}

impl std::ops::Mul<#name> for u64 {
impl core::ops::Mul<#name> for u64 {
type Output = #name;
#[inline(always)]
fn mul(self, rhs: #name) -> #name {
#name(rhs.0 * self)
}
}

impl std::ops::Mul<#name> for u32 {
impl core::ops::Mul<#name> for u32 {
type Output = #name;
#[inline(always)]
fn mul(self, rhs: #name) -> #name {
#name(rhs.0 * (self as u64))
}
}

impl std::ops::Mul<#name> for u16 {
impl core::ops::Mul<#name> for u16 {
type Output = #name;
#[inline(always)]
fn mul(self, rhs: #name) -> #name {
#name(rhs.0 * (self as u64))
}
}

impl std::ops::Mul<#name> for u8 {
impl core::ops::Mul<#name> for u8 {
type Output = #name;
#[inline(always)]
fn mul(self, rhs: #name) -> #name {
Expand Down Expand Up @@ -291,7 +291,12 @@ pub fn bytelike_fromstr(input: TokenStream) -> TokenStream {
let name = &input.ident;

let expanded = quote! {
impl std::str::FromStr for #name {

impl core::str::FromStr for #name {
#[cfg(feature = "std")]
type Err = std::string::String;

#[cfg(not(feature = "std"))]
type Err = alloc::string::String;

fn from_str(value: &str) -> core::result::Result<Self, Self::Err> {
Expand All @@ -306,14 +311,25 @@ pub fn bytelike_fromstr(input: TokenStream) -> TokenStream {
});
match suffix.parse::<bytelike::Unit>() {
Ok(u) => Ok(Self((v * u64::from(u) as f64) as u64)),
#[cfg(feature = "std")]
Err(error) => Err(std::format!(
"couldn't parse {:?} into a known SI unit, {}",
suffix, error
)),
#[cfg(not(feature = "std"))]
Err(error) => Err(alloc::format!(
"couldn't parse {:?} into a known SI unit, {}",
suffix, error
)),
}
}
Err(error) => Err(alloc::
format!(
#[cfg(feature = "std")]
Err(error) => Err(std::format!(
"couldn't parse {:?} into a ByteSize, {}",
value, error
)),
#[cfg(not(feature = "std"))]
Err(error) => Err(alloc::format!(
"couldn't parse {:?} into a ByteSize, {}",
value, error
)),
Expand Down

0 comments on commit 3fdc7a5

Please sign in to comment.