Skip to content

Commit

Permalink
Make it so FloatFuncs is only used on no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Mar 17, 2023
1 parent dd9dab4 commit 7cdbf09
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 59 deletions.
35 changes: 19 additions & 16 deletions examples/circle.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
//! Example of circle
#[cfg(feature = "std")]
fn main() {
#[cfg(feature = "std")]
{
use kurbo::{Circle, Shape};
use kurbo::{Circle, Shape};

let circle = Circle::new((400.0, 400.0), 380.0);
println!("<!DOCTYPE html>");
println!("<html>");
println!("<body>");
println!("<svg height=\"800\" width=\"800\">");
let path = circle.to_path(1e-3).to_svg();
println!(" <path d=\"{}\" stroke=\"black\" fill=\"none\" />", path);
let path = circle.to_path(1.0).to_svg();
println!(" <path d=\"{}\" stroke=\"red\" fill=\"none\" />", path);
println!("</svg>");
println!("</body>");
println!("</html>");
}
let circle = Circle::new((400.0, 400.0), 380.0);
println!("<!DOCTYPE html>");
println!("<html>");
println!("<body>");
println!("<svg height=\"800\" width=\"800\">");
let path = circle.to_path(1e-3).to_svg();
println!(" <path d=\"{}\" stroke=\"black\" fill=\"none\" />", path);
let path = circle.to_path(1.0).to_svg();
println!(" <path d=\"{}\" stroke=\"red\" fill=\"none\" />", path);
println!("</svg>");
println!("</body>");
println!("</html>");
}

#[cfg(not(feature = "std"))]
fn main() {
println!("This example requires the standard library");
}
37 changes: 20 additions & 17 deletions examples/ellipse.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
//! Example of ellipse
use kurbo::{Ellipse, Shape};
use std::f64::consts::PI;
#[cfg(feature = "std")]
fn main() {
use kurbo::{Ellipse, Shape};
use std::f64::consts::PI;

let ellipse = Ellipse::new((400.0, 400.0), (200.0, 100.0), 0.25 * PI);
println!("<!DOCTYPE html>");
println!("<html>");
println!("<body>");
println!("<svg height=\"800\" width=\"800\" style=\"background-color: #999\">");
let path = ellipse.to_path(1e-3).to_svg();
println!(" <path d=\"{}\" stroke=\"black\" fill=\"none\" />", path);
let path = ellipse.to_path(1.0).to_svg();
println!(" <path d=\"{}\" stroke=\"red\" fill=\"none\" />", path);
println!("</svg>");
println!("</body>");
println!("</html>");
}

#[cfg(not(feature = "std"))]
fn main() {
#[cfg(feature = "std")]
{
let ellipse = Ellipse::new((400.0, 400.0), (200.0, 100.0), 0.25 * PI);
println!("<!DOCTYPE html>");
println!("<html>");
println!("<body>");
println!("<svg height=\"800\" width=\"800\" style=\"background-color: #999\">");
let path = ellipse.to_path(1e-3).to_svg();
println!(" <path d=\"{}\" stroke=\"black\" fill=\"none\" />", path);
let path = ellipse.to_path(1.0).to_svg();
println!(" <path d=\"{}\" stroke=\"red\" fill=\"none\" />", path);
println!("</svg>");
println!("</body>");
println!("</html>");
}
println!("This example requires the standard library");
}
2 changes: 1 addition & 1 deletion src/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::ops::{Mul, MulAssign};

use crate::{Point, Rect, Vec2};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// A 2D affine transform.
Expand Down
2 changes: 1 addition & 1 deletion src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::{
iter,
};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::{FloatFuncs, FloatFuncsExtra};

/// A single arc segment.
Expand Down
2 changes: 1 addition & 1 deletion src/bezpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
ParamCurveExtrema, ParamCurveNearest, Point, QuadBez, Rect, Shape, TranslateScale, Vec2,
};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// A Bézier path.
Expand Down
2 changes: 1 addition & 1 deletion src/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::{

use crate::{Affine, Arc, ArcAppendIter, Ellipse, PathEl, Point, Rect, Shape, Vec2};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// A circle.
Expand Down
22 changes: 11 additions & 11 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,29 @@ macro_rules! define_float_funcs {
fn $name:ident(self $(,$arg:ident: $arg_ty:ty)*) -> $ret:ty
=> $lname:ident/$lfname:ident;
)+) => {
#[cfg(not(feature = "std"))]
pub(crate) trait FloatFuncs : Sized {
$(fn $name(self $(,$arg: $arg_ty)*) -> $ret;)+
}

#[cfg(not(feature = "std"))]
impl FloatFuncs for f32 {
$(fn $name(self $(,$arg: $arg_ty)*) -> $ret {
#[cfg(feature = "std")]
return self.$name($($arg),*);

#[cfg(not(feature = "std"))]
#[cfg(feature = "libm")]
return libm::$lfname(self $(,$arg as _)*);

#[cfg(all(not(feature = "std"), not(feature = "libm")))]
compile_error!("kurbo requires either the `std` or `libm` feature");
#[cfg(not(feature = "libm"))]
compile_error!("kurbo requires either the `std` or `libm` feature")
})+
}

#[cfg(not(feature = "std"))]
impl FloatFuncs for f64 {
$(fn $name(self $(,$arg: $arg_ty)*) -> $ret {
#[cfg(feature = "std")]
return self.$name($($arg),*);

#[cfg(all(not(feature = "std"), feature = "libm"))]
#[cfg(feature = "libm")]
return libm::$lname(self $(,$arg as _)*);

#[cfg(all(not(feature = "std"), not(feature = "libm")))]
#[cfg(not(feature = "libm"))]
compile_error!("kurbo requires either the `std` or `libm` feature")
})+
}
Expand Down Expand Up @@ -63,10 +60,12 @@ define_float_funcs! {
}

/// Special implementation for signum, because libm doesn't have it.
#[cfg(not(feature = "std"))]
pub(crate) trait FloatFuncsExtra {
fn signum(self) -> Self;
}

#[cfg(not(feature = "std"))]
impl FloatFuncsExtra for f32 {
#[inline]
fn signum(self) -> f32 {
Expand All @@ -78,6 +77,7 @@ impl FloatFuncsExtra for f32 {
}
}

#[cfg(not(feature = "std"))]
impl FloatFuncsExtra for f64 {
#[inline]
fn signum(self) -> f64 {
Expand Down
2 changes: 1 addition & 1 deletion src/cubicbez.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
ParamCurveDeriv, ParamCurveExtrema, ParamCurveNearest, PathEl, Point, QuadBez, Rect, Shape,
};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

const MAX_SPLINE_SPLIT: usize = 100;
Expand Down
2 changes: 1 addition & 1 deletion src/ellipse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use core::{

use crate::{Affine, Arc, ArcAppendIter, Circle, PathEl, Point, Rect, Shape, Size, Vec2};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// An ellipse.
Expand Down
2 changes: 1 addition & 1 deletion src/mindist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use crate::Vec2;
use core::cmp::Ordering;

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

pub(crate) fn min_dist_param(
Expand Down
2 changes: 1 addition & 1 deletion src/param_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use arrayvec::ArrayVec;

use crate::{common, Point, Rect};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// A default value for methods that take an 'accuracy' argument.
Expand Down
2 changes: 1 addition & 1 deletion src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::ops::{Add, AddAssign, Sub, SubAssign};
use crate::common::FloatExt;
use crate::Vec2;

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// A 2D point.
Expand Down
2 changes: 1 addition & 1 deletion src/quadbez.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
Rect, Shape,
};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::{FloatFuncs, FloatFuncsExtra};

/// A single quadratic Bézier segment.
Expand Down
2 changes: 1 addition & 1 deletion src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::ops::{Add, Sub};

use crate::{Ellipse, Insets, PathEl, Point, RoundedRect, RoundedRectRadii, Shape, Size, Vec2};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// A rectangle.
Expand Down
2 changes: 1 addition & 1 deletion src/rounded_rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use core::ops::{Add, Sub};

use crate::{arc::ArcAppendIter, Arc, PathEl, Point, Rect, RoundedRectRadii, Shape, Size, Vec2};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// A rectangle with equally rounded corners.
Expand Down
2 changes: 1 addition & 1 deletion src/rounded_rect_radii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use core::convert::From;

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// Radii for each corner of a rounded rectangle.
Expand Down
2 changes: 1 addition & 1 deletion src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use crate::common::FloatExt;
use crate::{Rect, RoundedRect, RoundedRectRadii, Vec2};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// A 2D size.
Expand Down
2 changes: 1 addition & 1 deletion src/vec2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAss
use crate::common::FloatExt;
use crate::{Point, Size};

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;

/// A 2D vector.
Expand Down

0 comments on commit 7cdbf09

Please sign in to comment.