Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sqrt/cbrt impls being feature gated inside quote #76

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions crates/diman_unit_system/src/codegen/float_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ impl Codegen {
let float_type = &float_type.name;
let dimension_type = &self.defs.dimension_type;
let quantity_type = &self.defs.quantity_type;

#[cfg(any(feature = "std", feature = "num-traits-libm"))]
let roots = quote! {
pub fn sqrt(&self) -> #quantity_type<#float_type, { D.div_2() }>
{
#quantity_type::<#float_type, { D.div_2() }>(self.0.sqrt())
}

pub fn cbrt(&self) -> #quantity_type<#float_type, { D.div_3() }>
{
#quantity_type::<#float_type, { D.div_3() }>(self.0.cbrt())
}
};
#[cfg(all(not(feature = "std"), not(feature = "num-traits-libm")))]
let roots = quote! {
pub fn sqrt(&self) -> #quantity_type<#float_type, { D.div_2() }>
{
#quantity_type::<#float_type, { D.div_2() }>(self.0.sqrt())
}

pub fn cbrt(&self) -> #quantity_type<#float_type, { D.div_3() }>
{
#quantity_type::<#float_type, { D.div_3() }>(self.0.cbrt())
}
};

quote! {
impl<const D: #dimension_type> #quantity_type<#float_type, D> {
pub fn squared(&self) -> #quantity_type<#float_type, { D.mul(2) }>
Expand All @@ -113,24 +139,15 @@ impl Codegen {
#quantity_type::<#float_type, { D.mul(3) }>(self.0.powi(3))
}


pub fn powi<const I: i32>(&self) -> #quantity_type<#float_type, { D.mul(I) }>
where
#quantity_type::<#float_type, { D.mul(I) }>:
{
#quantity_type::<#float_type, { D.mul(I) }>(self.0.powi(I))
}

#[cfg(any(feature = "std", feature = "num-traits-libm"))]
pub fn sqrt(&self) -> #quantity_type<#float_type, { D.div_2() }>
{
#quantity_type::<#float_type, { D.div_2() }>(self.0.sqrt())
}

#[cfg(any(feature = "std", feature = "num-traits-libm"))]
pub fn cbrt(&self) -> #quantity_type<#float_type, { D.div_3() }>
{
#quantity_type::<#float_type, { D.div_3() }>(self.0.cbrt())
}
#roots

pub fn min<Q: Into<Self>>(self, other: Q) -> Self {
Self(self.0.min(other.into().0))
Expand Down