From 6ca3dd5ea8afcc902383bb08f131c3c415afaf8d Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Tue, 8 Jan 2019 23:18:05 -0800 Subject: [PATCH] Properly support no_std --- .travis.yml | 4 +++- Cargo.toml | 6 ++++-- src/approxeq.rs | 3 +++ src/lib.rs | 4 ++-- src/num.rs | 23 +++++++++++++++++++++++ src/point.rs | 7 ++++--- src/rotation.rs | 15 ++++++++++++--- src/size.rs | 6 +++--- src/trig.rs | 1 + src/vector.rs | 15 +++++++++++++-- 10 files changed, 68 insertions(+), 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index 91ef909d..e8294c86 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,8 @@ matrix: include: - rust: stable env: FEATURES="" + - rust: stable + env: FEATURES="--no-default-features" TEST=no - rust: stable env: FEATURES="--features mint" - rust: beta @@ -25,4 +27,4 @@ matrix: script: - cargo build $FEATURES - - cargo test --verbose $FEATURES + - if [ $TEST != "no" ]; then cargo test --verbose $FEATURES; fi diff --git a/Cargo.toml b/Cargo.toml index bdbec9f0..20c9abd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "euclid" -version = "0.19.4" +version = "0.19.5" authors = ["The Servo Project Developers"] description = "Geometry primitives" documentation = "https://docs.rs/euclid/" @@ -11,9 +11,11 @@ license = "MIT / Apache-2.0" [features] unstable = [] +std = ["num-traits/std"] +default = ["std"] [dependencies] -num-traits = { version = "0.2" } +num-traits = { version = "0.2", default-features = false } serde = { version = "1.0", features = ["serde_derive"], optional = true } mint = {version = "0.5.1", optional = true} diff --git a/src/approxeq.rs b/src/approxeq.rs index 9fb11cc3..b8b46c74 100644 --- a/src/approxeq.rs +++ b/src/approxeq.rs @@ -7,6 +7,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +#[cfg(not(feature = "std"))] +use num_traits::float::FloatCore; + /// Trait for testing approximate equality pub trait ApproxEq { fn approx_epsilon() -> Eps; diff --git a/src/lib.rs b/src/lib.rs index 2f080ab4..fc4c183a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,7 @@ // except according to those terms. #![cfg_attr(feature = "unstable", feature(fn_must_use))] -#![cfg_attr(not(test), no_std)] +#![cfg_attr(not(any(feature = "std", test)), no_std)] //! A collection of strongly typed math tools for computer graphics with an inclination //! towards 2d graphics and layout. @@ -65,7 +65,7 @@ pub extern crate mint; extern crate num_traits; #[cfg(test)] extern crate rand; -#[cfg(test)] +#[cfg(any(test, feature = "std"))] use std as core; pub use length::Length; diff --git a/src/num.rs b/src/num.rs index 1df86d75..947dcfcf 100644 --- a/src/num.rs +++ b/src/num.rs @@ -10,6 +10,9 @@ use num_traits; +#[cfg(not(feature = "std"))] +use num_traits::float::FloatCore; + pub trait Zero { fn zero() -> Self; } @@ -56,6 +59,26 @@ macro_rules! num_int { } ) } + +#[cfg(not(any(feature = "std", test)))] +macro_rules! num_float { + ($ty:ty) => ( + impl Round for $ty { + #[inline] + fn round(self) -> $ty { <$ty as FloatCore>::round(self) } + } + impl Floor for $ty { + #[inline] + fn floor(self) -> $ty { <$ty as FloatCore>::floor(self) } + } + impl Ceil for $ty { + #[inline] + fn ceil(self) -> $ty { <$ty as FloatCore>::ceil(self) } + } + ) +} + +#[cfg(any(feature = "std", test))] macro_rules! num_float { ($ty:ty) => ( impl Round for $ty { diff --git a/src/point.rs b/src/point.rs index 9168e151..7f59b339 100644 --- a/src/point.rs +++ b/src/point.rs @@ -15,7 +15,8 @@ use size::TypedSize2D; #[cfg(feature = "mint")] use mint; use num::*; -use num_traits::{Float, NumCast}; +use num_traits::NumCast; +use num_traits::float::FloatCore; use vector::{TypedVector2D, TypedVector3D, vec2, vec3}; use core::fmt; use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; @@ -188,7 +189,7 @@ impl, U> Sub> for TypedPoint2D< } } -impl TypedPoint2D { +impl TypedPoint2D { #[inline] pub fn min(self, other: Self) -> Self { point2(self.x.min(other.x), self.y.min(other.y)) @@ -608,7 +609,7 @@ impl, U> Div for TypedPoint3D { } } -impl TypedPoint3D { +impl TypedPoint3D { #[inline] pub fn min(self, other: Self) -> Self { point3( diff --git a/src/rotation.rs b/src/rotation.rs index 54056865..d2a7ba05 100644 --- a/src/rotation.rs +++ b/src/rotation.rs @@ -8,13 +8,19 @@ // except according to those terms. use approxeq::ApproxEq; -use num_traits::{Float, FloatConst, One, Zero}; +#[cfg(feature = "std")] +use num_traits::Float; +use num_traits::{FloatConst, One, Zero}; use core::fmt; use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, SubAssign}; use core::marker::PhantomData; use trig::Trig; -use {TypedPoint2D, TypedPoint3D, TypedVector2D, TypedVector3D, Vector3D, point2, point3, vec3}; -use {TypedTransform2D, TypedTransform3D, UnknownUnit}; +#[cfg(feature = "std")] +use {TypedPoint2D, TypedPoint3D, TypedVector2D, TypedVector3D, point2, point3}; +use {Vector3D, vec3}; +#[cfg(feature = "std")] +use TypedTransform3D; +use {TypedTransform2D, UnknownUnit}; /// An angle in radians #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Hash)] @@ -72,6 +78,7 @@ where } } +#[cfg(feature = "std")] impl Angle where T: Float, @@ -229,6 +236,7 @@ where } } +#[cfg(feature = "std")] impl TypedRotation2D where T: Copy @@ -367,6 +375,7 @@ where } } +#[cfg(feature = "std")] impl TypedRotation3D where T: Float, diff --git a/src/size.rs b/src/size.rs index 068c4bb3..06bbbb68 100644 --- a/src/size.rs +++ b/src/size.rs @@ -14,8 +14,8 @@ use length::Length; use scale::TypedScale; use vector::{TypedVector2D, vec2, BoolVector2D}; use num::*; - -use num_traits::{Float, NumCast, Signed}; +use num_traits::{NumCast, Signed}; +use num_traits::float::FloatCore; use core::fmt; use core::ops::{Add, Div, Mul, Sub}; use core::marker::PhantomData; @@ -329,7 +329,7 @@ impl TypedSize2D { } } -impl TypedSize2D { +impl TypedSize2D { #[inline] pub fn min(self, other: Self) -> Self { size2( diff --git a/src/trig.rs b/src/trig.rs index 9eb9c235..02922761 100644 --- a/src/trig.rs +++ b/src/trig.rs @@ -19,6 +19,7 @@ pub trait Trig { macro_rules! trig { ($ty:ident) => ( + #[cfg(feature = "std")] impl Trig for $ty { #[inline] fn sin(self) -> $ty { self.sin() } diff --git a/src/vector.rs b/src/vector.rs index 5fe7963c..f6a4c3eb 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -18,7 +18,11 @@ use scale::TypedScale; use trig::Trig; use Angle; use num::*; -use num_traits::{Float, NumCast, Signed}; + +#[cfg(feature = "std")] +use num_traits::Float; +use num_traits::{NumCast, Signed}; +use num_traits::float::FloatCore; use core::fmt; use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}; use core::marker::PhantomData; @@ -165,6 +169,7 @@ where } #[inline] + #[cfg(feature = "std")] pub fn normalize(self) -> Self where T: Float, @@ -174,6 +179,7 @@ where /// Return the normalized vector even if the length is larger than the max value of Float. #[inline] + #[cfg(feature = "std")] pub fn robust_normalize(self) -> Self where T: Float, @@ -193,6 +199,7 @@ where } #[inline] + #[cfg(feature = "std")] pub fn length(&self) -> T where T: Float, @@ -252,7 +259,7 @@ impl, U> Neg for TypedVector2D { } } -impl TypedVector2D { +impl TypedVector2D { #[inline] pub fn min(self, other: Self) -> Self { vec2(self.x.min(other.x), self.y.min(other.y)) @@ -615,6 +622,7 @@ impl + Add + Sub + Copy, U> } #[inline] + #[cfg(feature = "std")] pub fn normalize(self) -> Self where T: Float, @@ -624,6 +632,7 @@ impl + Add + Sub + Copy, U> /// Return the normalized vector even if the length is larger than the max value of Float. #[inline] + #[cfg(feature = "std")] pub fn robust_normalize(self) -> Self where T: Float, @@ -643,6 +652,7 @@ impl + Add + Sub + Copy, U> } #[inline] + #[cfg(feature = "std")] pub fn length(&self) -> T where T: Float, @@ -733,6 +743,7 @@ impl, U> DivAssign for TypedVector3D { } } +#[cfg(feature = "std")] impl TypedVector3D { #[inline] pub fn min(self, other: Self) -> Self {