Skip to content

Add std feature #2

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ repository = "https://github.com/schell/casuarius"
readme = "README.md"
license = "MIT / Apache-2.0"
keywords = ["constraint", "simplex", "user", "interface", "layout"]
rust-version = "1.63"

[features]
default = ["std"]
std = ["rustc-hash/std"]
# FIXME: should be enabled only if std feature is disabled but cargo can not do that yet
hashbrown = ["dep:hashbrown"]

[dependencies]
ordered-float = "^1.0"
rustc-hash = "^1.1"
ordered-float = "1.0"
rustc-hash = { version = "1.1", default-features = false }
hashbrown = { version = "0.14", default-features = false, optional = true }
64 changes: 32 additions & 32 deletions src/derive_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,49 @@ macro_rules! derive_syntax_for {
}
}

impl std::ops::Add<f64> for $x {
impl core::ops::Add<f64> for $x {
type Output = casuarius::Expression<$x>;
fn add(self, v: f64) -> casuarius::Expression<$x> {
casuarius::Expression::new(vec![casuarius::Term::new(self, 1.0)], v)
}
}

impl std::ops::Add<f32> for $x {
impl core::ops::Add<f32> for $x {
type Output = casuarius::Expression<$x>;
fn add(self, v: f32) -> casuarius::Expression<$x> {
self.add(v as f64)
}
}

impl std::ops::Add<u32> for $x {
impl core::ops::Add<u32> for $x {
type Output = casuarius::Expression<$x>;
fn add(self, v: u32) -> casuarius::Expression<$x> {
self.add(v as f64)
}
}

impl std::ops::Add<$x> for f64 {
impl core::ops::Add<$x> for f64 {
type Output = casuarius::Expression<$x>;
fn add(self, v: $x) -> casuarius::Expression<$x> {
casuarius::Expression::new(vec![casuarius::Term::new(v, 1.0)], self)
}
}

impl std::ops::Add<$x> for f32 {
impl core::ops::Add<$x> for f32 {
type Output = casuarius::Expression<$x>;
fn add(self, v: $x) -> casuarius::Expression<$x> {
(self as f64).add(v)
}
}

impl std::ops::Add<$x> for u32 {
impl core::ops::Add<$x> for u32 {
type Output = casuarius::Expression<$x>;
fn add(self, v: $x) -> casuarius::Expression<$x> {
(self as f64).add(v)
}
}

impl std::ops::Add<$x> for $x {
impl core::ops::Add<$x> for $x {
type Output = casuarius::Expression<$x>;
fn add(self, v: $x) -> casuarius::Expression<$x> {
casuarius::Expression::new(
Expand All @@ -64,92 +64,92 @@ macro_rules! derive_syntax_for {
}
}

impl std::ops::Add<casuarius::Term<$x>> for $x {
impl core::ops::Add<casuarius::Term<$x>> for $x {
type Output = casuarius::Expression<$x>;
fn add(self, t: casuarius::Term<$x>) -> casuarius::Expression<$x> {
casuarius::Expression::new(vec![casuarius::Term::new(self, 1.0), t], 0.0)
}
}

impl std::ops::Add<$x> for casuarius::Term<$x> {
impl core::ops::Add<$x> for casuarius::Term<$x> {
type Output = casuarius::Expression<$x>;
fn add(self, v: $x) -> casuarius::Expression<$x> {
casuarius::Expression::new(vec![self, casuarius::Term::new(v, 1.0)], 0.0)
}
}

impl std::ops::Add<casuarius::Expression<$x>> for $x {
impl core::ops::Add<casuarius::Expression<$x>> for $x {
type Output = casuarius::Expression<$x>;
fn add(self, mut e: casuarius::Expression<$x>) -> casuarius::Expression<$x> {
e.terms.push(casuarius::Term::new(self, 1.0));
e
}
}

impl std::ops::Add<$x> for casuarius::Expression<$x> {
impl core::ops::Add<$x> for casuarius::Expression<$x> {
type Output = casuarius::Expression<$x>;
fn add(mut self, v: $x) -> casuarius::Expression<$x> {
self += v;
self
}
}

impl std::ops::AddAssign<$x> for casuarius::Expression<$x> {
impl core::ops::AddAssign<$x> for casuarius::Expression<$x> {
fn add_assign(&mut self, v: $x) {
self.terms.push(casuarius::Term::new(v, 1.0));
}
}

impl std::ops::Neg for $x {
impl core::ops::Neg for $x {
type Output = casuarius::Term<$x>;
fn neg(self) -> casuarius::Term<$x> {
casuarius::Term::new(self, -1.0)
}
}

impl std::ops::Sub<f64> for $x {
impl core::ops::Sub<f64> for $x {
type Output = casuarius::Expression<$x>;
fn sub(self, v: f64) -> casuarius::Expression<$x> {
casuarius::Expression::new(vec![casuarius::Term::new(self, 1.0)], -v)
}
}

impl std::ops::Sub<f32> for $x {
impl core::ops::Sub<f32> for $x {
type Output = casuarius::Expression<$x>;
fn sub(self, v: f32) -> casuarius::Expression<$x> {
self.sub(v as f64)
}
}

impl std::ops::Sub<u32> for $x {
impl core::ops::Sub<u32> for $x {
type Output = casuarius::Expression<$x>;
fn sub(self, v: u32) -> casuarius::Expression<$x> {
self.sub(v as f64)
}
}

impl std::ops::Sub<$x> for f64 {
impl core::ops::Sub<$x> for f64 {
type Output = casuarius::Expression<$x>;
fn sub(self, v: $x) -> casuarius::Expression<$x> {
casuarius::Expression::new(vec![casuarius::Term::new(v, -1.0)], self)
}
}

impl std::ops::Sub<$x> for f32 {
impl core::ops::Sub<$x> for f32 {
type Output = casuarius::Expression<$x>;
fn sub(self, v: $x) -> casuarius::Expression<$x> {
(self as f64).sub(v)
}
}

impl std::ops::Sub<$x> for u32 {
impl core::ops::Sub<$x> for u32 {
type Output = casuarius::Expression<$x>;
fn sub(self, v: $x) -> casuarius::Expression<$x> {
(self as f64).sub(v)
}
}

impl std::ops::Sub<$x> for $x {
impl core::ops::Sub<$x> for $x {
type Output = casuarius::Expression<$x>;
fn sub(self, v: $x) -> casuarius::Expression<$x> {
casuarius::Expression::new(
Expand All @@ -162,21 +162,21 @@ macro_rules! derive_syntax_for {
}
}

impl std::ops::Sub<casuarius::Term<$x>> for $x {
impl core::ops::Sub<casuarius::Term<$x>> for $x {
type Output = casuarius::Expression<$x>;
fn sub(self, t: casuarius::Term<$x>) -> casuarius::Expression<$x> {
casuarius::Expression::new(vec![casuarius::Term::new(self, 1.0), -t], 0.0)
}
}

impl std::ops::Sub<$x> for casuarius::Term<$x> {
impl core::ops::Sub<$x> for casuarius::Term<$x> {
type Output = casuarius::Expression<$x>;
fn sub(self, v: $x) -> casuarius::Expression<$x> {
casuarius::Expression::new(vec![self, casuarius::Term::new(v, -1.0)], 0.0)
}
}

impl std::ops::Sub<casuarius::Expression<$x>> for $x {
impl core::ops::Sub<casuarius::Expression<$x>> for $x {
type Output = casuarius::Expression<$x>;
fn sub(self, mut e: casuarius::Expression<$x>) -> casuarius::Expression<$x> {
e.negate();
Expand All @@ -185,56 +185,56 @@ macro_rules! derive_syntax_for {
}
}

impl std::ops::Sub<$x> for casuarius::Expression<$x> {
impl core::ops::Sub<$x> for casuarius::Expression<$x> {
type Output = casuarius::Expression<$x>;
fn sub(mut self, v: $x) -> casuarius::Expression<$x> {
self -= v;
self
}
}

impl std::ops::SubAssign<$x> for casuarius::Expression<$x> {
impl core::ops::SubAssign<$x> for casuarius::Expression<$x> {
fn sub_assign(&mut self, v: $x) {
self.terms.push(casuarius::Term::new(v, -1.0));
}
}

impl std::ops::Mul<f64> for $x {
impl core::ops::Mul<f64> for $x {
type Output = casuarius::Term<$x>;
fn mul(self, v: f64) -> casuarius::Term<$x> {
casuarius::Term::new(self, v)
}
}

impl std::ops::Mul<f32> for $x {
impl core::ops::Mul<f32> for $x {
type Output = casuarius::Term<$x>;
fn mul(self, v: f32) -> casuarius::Term<$x> {
self.mul(v as f64)
}
}

impl std::ops::Mul<$x> for f64 {
impl core::ops::Mul<$x> for f64 {
type Output = casuarius::Term<$x>;
fn mul(self, v: $x) -> casuarius::Term<$x> {
casuarius::Term::new(v, self)
}
}

impl std::ops::Mul<$x> for f32 {
impl core::ops::Mul<$x> for f32 {
type Output = casuarius::Term<$x>;
fn mul(self, v: $x) -> casuarius::Term<$x> {
(self as f64).mul(v)
}
}

impl std::ops::Div<f64> for $x {
impl core::ops::Div<f64> for $x {
type Output = casuarius::Term<$x>;
fn div(self, v: f64) -> casuarius::Term<$x> {
casuarius::Term::new(self, 1.0 / v)
}
}

impl std::ops::Div<f32> for $x {
impl core::ops::Div<f32> for $x {
type Output = casuarius::Term<$x>;
fn div(self, v: f32) -> casuarius::Term<$x> {
self.div(v as f64)
Expand Down Expand Up @@ -272,7 +272,7 @@ macro_rules! derive_syntax_for {

#[cfg(test)]
mod tests {
use crate::{self as casuarius, Solver, Constrainable};
use crate::{self as casuarius, Constrainable, Solver};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum VariableX {
Expand Down
27 changes: 27 additions & 0 deletions src/hash_map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub use self::imp::*;

#[cfg(feature = "std")]
mod imp {
pub use rustc_hash::{FxHashMap, FxHashSet};
pub use std::collections::hash_map::Entry;
}

#[cfg(not(feature = "std"))]
mod imp {
use rustc_hash::FxHasher;

pub use hashbrown::hash_map::Entry;

pub type FxHashMap<K, V> = hashbrown::HashMap<K, V, FxBuildHasher>;
pub type FxHashSet<V> = hashbrown::HashSet<V, FxBuildHasher>;

#[derive(Copy, Clone, Default)]
pub struct FxBuildHasher;

impl core::hash::BuildHasher for FxBuildHasher {
type Hasher = FxHasher;
fn build_hasher(&self) -> FxHasher {
rustc_hash::FxHasher::default()
}
}
}
Loading