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

Make the FloatFuncs trait a sealed public trait, add sin/sinf. #332

Merged
merged 3 commits into from
Feb 9, 2024
Merged
Changes from 1 commit
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
24 changes: 23 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

#![allow(missing_docs)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know why this is here? Does any of this new public API need docs, even just something very simple?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is here to avoid documenting the existing pub const GAUSS_LEGENDRE_COEFFS* values.
I added basically a stub documentation explaining the trait, and pointing to the std implementation for further docs.


#[cfg(not(feature = "std"))]
mod sealed {
/// A [sealed trait](https://predr.ag/blog/definitive-guide-to-sealed-traits-in-rust/)
/// which stops [`super::FloatFuncs`] from being implemented outside kurbo. This could
/// be relaxed in the future if there is are good reasons to allow external impls.
/// The benefit from being sealed is that we can add methods without breaking downstream
/// implementations.
pub trait FloatFuncsSealed {}
}

use arrayvec::ArrayVec;

/// Defines a trait that chooses between libstd or libm implementations of float methods.
Expand All @@ -13,14 +23,23 @@ macro_rules! define_float_funcs {
fn $name:ident(self $(,$arg:ident: $arg_ty:ty)*) -> $ret:ty
=> $lname:ident/$lfname:ident;
)+) => {

/// Since core doesn't depend upon libm, this provides libm implementations
/// of float functions which are typically provided by the std library, when
/// the `std` feature is not enabled.
///
/// For documentation see the respective functions in the std library.
#[cfg(not(feature = "std"))]
pub(crate) trait FloatFuncs : Sized {
pub trait FloatFuncs : Sized + sealed::FloatFuncsSealed {
/// Special implementation for signum, because libm doesn't have it.
fn signum(self) -> Self;

$(fn $name(self $(,$arg: $arg_ty)*) -> $ret;)+
}

#[cfg(not(feature = "std"))]
impl sealed::FloatFuncsSealed for f32{}
ratmice marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(not(feature = "std"))]
impl FloatFuncs for f32 {
#[inline]
Expand All @@ -41,6 +60,8 @@ macro_rules! define_float_funcs {
})+
}

#[cfg(not(feature = "std"))]
impl sealed::FloatFuncsSealed for f64{}
ratmice marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(feature = "std"))]
impl FloatFuncs for f64 {
#[inline]
Expand Down Expand Up @@ -79,6 +100,7 @@ define_float_funcs! {
fn powi(self, n: i32) -> Self => pow/powf;
fn powf(self, n: Self) -> Self => pow/powf;
fn round(self) -> Self => round/roundf;
fn sin(self) -> Self => sin/sinf;
fn sin_cos(self) -> (Self, Self) => sincos/sincosf;
fn sqrt(self) -> Self => sqrt/sqrtf;
fn tan(self) -> Self => tan/tanf;
Expand Down
Loading