Skip to content

Commit d84cb55

Browse files
author
sarah
committed
support no_std mode
1 parent 57a66bd commit d84cb55

9 files changed

Lines changed: 70 additions & 32 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ proptest-support = [ "proptest"]
1515
[dependencies]
1616
num-traits = { version = "0.2", default-features = false }
1717
matrixcompare-core = { path = "matrixcompare-core", version="0.1"}
18-
proptest = { version = "1.0", optional = true }
18+
proptest = { version = "1.0", optional = true, default-features = false }
19+
hashbrown = "0.14.3"
1920

2021
[dev-dependencies]
2122
quickcheck = "0.9"

matrixcompare-core/src/lib.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#![no_std]
2+
extern crate alloc;
3+
use alloc::vec::Vec;
4+
15
/// Defines how the elements of a matrix may be accessed.
26
pub enum Access<'a, T> {
37
Dense(&'a dyn DenseAccess<T>),
@@ -28,8 +32,8 @@ pub trait SparseAccess<T>: Matrix<T> {
2832
}
2933

3034
impl<T, X> Matrix<T> for &X
31-
where
32-
X: Matrix<T>,
35+
where
36+
X: Matrix<T>,
3337
{
3438
fn rows(&self) -> usize {
3539
X::rows(*self)
@@ -45,17 +49,17 @@ impl<T, X> Matrix<T> for &X
4549
}
4650

4751
impl<T, X> DenseAccess<T> for &X
48-
where
49-
X: DenseAccess<T>,
52+
where
53+
X: DenseAccess<T>,
5054
{
5155
fn fetch_single(&self, row: usize, col: usize) -> T {
5256
X::fetch_single(*self, row, col)
5357
}
5458
}
5559

5660
impl<T, X> SparseAccess<T> for &X
57-
where
58-
X: SparseAccess<T>,
61+
where
62+
X: SparseAccess<T>,
5963
{
6064
fn nnz(&self) -> usize {
6165
X::nnz(*self)
@@ -64,4 +68,4 @@ impl<T, X> SparseAccess<T> for &X
6468
fn fetch_triplets(&self) -> Vec<(usize, usize, T)> {
6569
X::fetch_triplets(&self)
6670
}
67-
}
71+
}

src/comparators.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! Comparators used for element-wise comparison of matrix entries.
22
3+
use crate::alloc::string::ToString;
34
use crate::ulp::{Ulp, UlpComparisonResult};
5+
use core::fmt::Write;
46

57
use num_traits::{float::FloatCore, Num};
68

7-
use std::fmt;
8-
use std::fmt::{Display, Formatter};
9+
use alloc::string::String;
10+
use core::fmt;
11+
use core::fmt::{Display, Formatter};
912

1013
/// Trait that describes elementwise comparators for [assert_matrix_eq!](../macro.assert_matrix_eq!.html).
1114
///
@@ -72,7 +75,14 @@ where
7275
}
7376

7477
fn description(&self) -> String {
75-
format!("absolute difference, |x - y| <= {tol}.", tol = self.tol)
78+
let mut out = String::new();
79+
write!(
80+
out,
81+
"absolute difference, |x - y| <= {tol}.",
82+
tol = self.tol
83+
)
84+
.unwrap();
85+
out
7686
}
7787
}
7888

@@ -147,10 +157,14 @@ where
147157
}
148158

149159
fn description(&self) -> String {
150-
format!(
160+
let mut out = String::new();
161+
write!(
162+
out,
151163
"ULP difference less than or equal to {tol}. See documentation for details.",
152164
tol = self.tol
153165
)
166+
.unwrap();
167+
out
154168
}
155169
}
156170

@@ -208,14 +222,18 @@ where
208222
}
209223

210224
fn description(&self) -> String {
211-
format!(
225+
let mut out = String::new();
226+
write!(
227+
out,
212228
"Epsilon-sized absolute comparison, followed by an ULP-based comparison.
213229
Please see the documentation for details.
214230
Epsilon: {eps}
215231
ULP tolerance: {ulp}",
216232
eps = self.abs.tol,
217233
ulp = self.ulp.tol
218234
)
235+
.unwrap();
236+
out
219237
}
220238
}
221239

@@ -227,12 +245,12 @@ mod tests {
227245
UlpElementwiseComparator, UlpError,
228246
};
229247
use crate::ulp::{Ulp, UlpComparisonResult};
248+
use core::f64;
230249
use quickcheck::TestResult;
231-
use std::f64;
232250

233251
/// Returns the next adjacent floating point number (in the direction of positive infinity)
234252
fn next_f64(x: f64) -> f64 {
235-
use std::mem;
253+
use core::mem;
236254
let as_int = unsafe { mem::transmute::<f64, i64>(x) };
237255
unsafe { mem::transmute::<i64, f64>(as_int + 1) }
238256
}

src/comparison_failure.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
use crate::alloc::string::ToString;
2+
use alloc::string::String;
3+
use alloc::vec::Vec;
14
use core::fmt;
2-
use std::fmt::{Display, Formatter};
5+
use core::fmt::Write;
6+
use core::fmt::{Display, Formatter};
37

48
const MAX_MISMATCH_REPORTS: usize = 12;
59

@@ -29,7 +33,8 @@ where
2933

3034
// Write the error into a string first, so that we can add a space between
3135
// the element output and the error output only if there is something to report.
32-
let error = format!("{}", self.error);
36+
let mut error = String::new();
37+
write!(error, "{}", self.error)?;
3338
if !error.is_empty() {
3439
write!(f, " {}", self.error)?;
3540
}
@@ -96,10 +101,13 @@ where
96101
// TODO: Write directly to formatter
97102
let overflow_msg = if mismatches_overflow {
98103
let num_hidden_entries = self.mismatches.len() - MAX_MISMATCH_REPORTS;
99-
format!(
104+
let mut msg = String::new();
105+
write!(
106+
msg,
100107
" ... ({} mismatching elements not shown)\n",
101108
num_hidden_entries
102-
)
109+
)?;
110+
msg
103111
} else {
104112
String::new()
105113
};
@@ -139,6 +147,7 @@ pub enum MatrixComparisonFailure<T, Error> {
139147
DuplicateSparseEntry(Entry),
140148
}
141149

150+
#[cfg(feature = "std")]
142151
impl<T, E> std::error::Error for MatrixComparisonFailure<T, E>
143152
where
144153
T: fmt::Debug + Display,

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ example in the repository.
7171
*/
7272

7373
#![allow(clippy::float_cmp)]
74+
#![no_std]
7475

7576
#[macro_use]
7677
mod matrix_comparison;
@@ -83,6 +84,7 @@ mod comparison_failure;
8384
#[cfg(test)]
8485
#[macro_use]
8586
extern crate quickcheck;
87+
extern crate alloc;
8688

8789
pub mod comparators;
8890
mod macros;

src/macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ macro_rules! base_scalar_eq {
272272
{
273273
use $crate::{compare_scalars};
274274
use $crate::comparators::ExactElementwiseComparator;
275-
use std::borrow::Borrow;
275+
use core::borrow::Borrow;
276276
let comp = ExactElementwiseComparator;
277277
let result = compare_scalars($x.borrow(), $y.borrow(), comp);
278278
if let Err(error) = result {
@@ -287,7 +287,7 @@ Please see the documentation for ways to compare scalars approximately.\n",
287287
{
288288
use $crate::{compare_scalars};
289289
use $crate::comparators::ExactElementwiseComparator;
290-
use std::borrow::Borrow;
290+
use core::borrow::Borrow;
291291
let comp = ExactElementwiseComparator;
292292
let result = compare_scalars($x.borrow(), $y.borrow(), comp);
293293
if let Err(error) = result {
@@ -300,7 +300,7 @@ Please see the documentation for ways to compare scalars approximately.\n",
300300
{
301301
use $crate::{compare_scalars};
302302
use $crate::comparators::AbsoluteElementwiseComparator;
303-
use std::borrow::Borrow;
303+
use core::borrow::Borrow;
304304
let comp = AbsoluteElementwiseComparator { tol: $tol.clone() };
305305
let result = compare_scalars($x.borrow(), $y.borrow(), comp);
306306
if let Err(error) = result {
@@ -313,7 +313,7 @@ Please see the documentation for ways to compare scalars approximately.\n",
313313
{
314314
use $crate::{compare_scalars};
315315
use $crate::comparators::UlpElementwiseComparator;
316-
use std::borrow::Borrow;
316+
use core::borrow::Borrow;
317317
let comp = UlpElementwiseComparator { tol: $tol.clone() };
318318
let result = compare_scalars($x.borrow(), $y.borrow(), comp);
319319
if let Err(error) = result {
@@ -326,7 +326,7 @@ Please see the documentation for ways to compare scalars approximately.\n",
326326
{
327327
use $crate::{compare_scalars};
328328
use $crate::comparators::FloatElementwiseComparator;
329-
use std::borrow::Borrow;
329+
use core::borrow::Borrow;
330330
let comp = FloatElementwiseComparator::default();
331331
let result = compare_scalars($x.borrow(), $y.borrow(), comp);
332332
if let Err(error) = result {
@@ -341,7 +341,7 @@ Please see the documentation for ways to compare scalars approximately.\n",
341341
{
342342
use $crate::{compare_scalars};
343343
use $crate::comparators::FloatElementwiseComparator;
344-
use std::borrow::Borrow;
344+
use core::borrow::Borrow;
345345
let comp = FloatElementwiseComparator::default()$(.$key($val))+;
346346
let result = compare_scalars($x.borrow(), $y.borrow(), comp);
347347
if let Err(error) = result {

src/matrix_comparison.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use crate::{
33
Access, Coordinate, DenseAccess, DimensionMismatch, ElementsMismatch, Matrix,
44
MatrixComparisonFailure, MatrixElementComparisonFailure, SparseAccess,
55
};
6+
use alloc::vec::Vec;
7+
use hashbrown::{HashMap, HashSet};
68
use num_traits::Zero;
7-
use std::collections::{HashMap, HashSet};
89

910
use crate::Entry;
1011

@@ -26,7 +27,8 @@ where
2627
for (i, j, v) in triplets.iter().cloned() {
2728
if i >= rows || j >= cols {
2829
return Err(HashMapBuildError::OutOfBoundsCoord((i, j)));
29-
} else if matrix.insert((i, j), v).is_some() {
30+
}
31+
if matrix.insert((i, j), v).is_some() {
3032
return Err(HashMapBuildError::DuplicateCoord((i, j)));
3133
}
3234
}
@@ -71,7 +73,7 @@ where
7173
let right_keys: HashSet<_> = right_hash.keys().collect();
7274
let zero = T::zero();
7375

74-
for coord in left_keys.union(&right_keys) {
76+
for &coord in left_keys.union(&right_keys) {
7577
let a = left_hash.get(coord).unwrap_or(&zero);
7678
let b = right_hash.get(coord).unwrap_or(&zero);
7779
if let Err(error) = comparator.compare(&a, &b) {

src/scalar_comparison.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::fmt;
1+
use alloc::string::String;
2+
use core::fmt;
23

34
use crate::comparators::ElementwiseComparator;
45

src/ulp.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Tools for ULP-based comparison of floating point numbers.
2-
use std::mem;
2+
use crate::alloc::borrow::ToOwned;
3+
use core::mem;
34

45
/// Represents the result of an ULP-based comparison between two floating point numbers.
56
#[derive(Debug, Copy, Clone, PartialEq)]
@@ -67,9 +68,9 @@ impl_float_ulp!(f64, i64);
6768
mod tests {
6869
use super::Ulp;
6970
use super::UlpComparisonResult;
71+
use core::mem;
72+
use core::{f32, f64};
7073
use quickcheck::TestResult;
71-
use std::mem;
72-
use std::{f32, f64};
7374

7475
#[test]
7576
fn plus_minus_zero_is_exact_match_f32() {

0 commit comments

Comments
 (0)