From 4f59020819c4a9019ad9e6c380156cd9cfcfcb14 Mon Sep 17 00:00:00 2001 From: hat Date: Sun, 5 Nov 2023 16:23:23 +0100 Subject: [PATCH] Crate remade to be no_std with alloc. By default std still required for one Error trait, but can be turned off using features. --- Cargo.toml | 8 ++++++- benches/benchmark_utilities.rs | 6 ++--- delaunay_compare/benches/bench_utilities.rs | 4 ++-- delaunay_compare/src/spade_crate.rs | 2 +- examples/svg_renderer/main.rs | 2 +- examples/svg_renderer/quicksketch/color.rs | 4 ++-- examples/svg_renderer/quicksketch/mod.rs | 2 +- fuzz/fuzz_targets/bulk_load_fuzz.rs | 4 ++-- fuzz/fuzz_targets/bulk_load_int_fuzz.rs | 4 ++-- src/cdt.rs | 11 ++++++--- src/delaunay_core/bulk_load.rs | 8 +++++-- src/delaunay_core/bulk_load_fuzz_tests.rs | 2 ++ src/delaunay_core/dcel.rs | 2 ++ src/delaunay_core/dcel_operations.rs | 4 ++++ src/delaunay_core/handles/handle_defs.rs | 6 ++--- src/delaunay_core/handles/handle_impls.rs | 24 +++++++++---------- .../handles/iterators/circular_iterator.rs | 2 +- .../handles/iterators/fixed_iterators.rs | 8 +++---- .../handles/iterators/hull_iterator.rs | 3 +++ src/delaunay_core/handles/public_handles.rs | 2 +- src/delaunay_core/hint_generator.rs | 6 ++++- src/delaunay_core/math.rs | 11 ++++----- src/delaunay_core/triangulation_ext.rs | 12 ++++++---- src/flood_fill_iterator.rs | 6 ++++- src/intersection_iterator.rs | 6 +++-- src/lib.rs | 6 +++++ src/point.rs | 4 ++-- src/test_utilities.rs | 2 ++ src/triangulation.rs | 2 ++ 29 files changed, 106 insertions(+), 57 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 01621d8..d9e3687 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,16 +12,22 @@ autobenches = false [lib] bench = false +[features] +default = [ "std" ] +std = [] + [dependencies] smallvec = "1" robust = "1.1.0" num-traits = "0.2" +hashbrown = "0.14.2" [dependencies.serde] package = "serde" optional = true version = "1" -features = ["derive"] +default-features = false +features = [ "derive", "alloc" ] [workspace] members = ["delaunay_compare"] diff --git a/benches/benchmark_utilities.rs b/benches/benchmark_utilities.rs index 1d2f257..23029a0 100644 --- a/benches/benchmark_utilities.rs +++ b/benches/benchmark_utilities.rs @@ -1,4 +1,4 @@ -use std::fmt::{self, Display, Formatter}; +use core::fmt::{self, Display, Formatter}; use criterion::{measurement::WallTime, BenchmarkGroup, BenchmarkId, Throughput}; use rand::{distributions::uniform::SampleUniform, Rng, SeedableRng}; @@ -29,7 +29,7 @@ where { let range = rand::distributions::Uniform::new_inclusive(-range, range); let mut rng = rand::rngs::StdRng::from_seed(seed); - std::iter::from_fn(move || Some(Point2::new(rng.sample(range), rng.sample(range)))) + core::iter::from_fn(move || Some(Point2::new(rng.sample(range), rng.sample(range)))) } pub fn uniform_f64() -> impl Iterator> { @@ -54,7 +54,7 @@ where Some(Point2::new(last_x, last_y)) }; - std::iter::from_fn(step_fn) + core::iter::from_fn(step_fn) } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] diff --git a/delaunay_compare/benches/bench_utilities.rs b/delaunay_compare/benches/bench_utilities.rs index 5365768..f260c96 100644 --- a/delaunay_compare/benches/bench_utilities.rs +++ b/delaunay_compare/benches/bench_utilities.rs @@ -14,7 +14,7 @@ where { let range = rand::distributions::Uniform::new_inclusive(-range, range); let mut rng = StdRng::from_seed(seed); - std::iter::from_fn(move || Some([rng.sample(range), rng.sample(range)])) + core::iter::from_fn(move || Some([rng.sample(range), rng.sample(range)])) } pub fn uniform_f64() -> impl Iterator { @@ -41,5 +41,5 @@ where Some([last_x, last_y]) }; - std::iter::from_fn(step_fn) + core::iter::from_fn(step_fn) } diff --git a/delaunay_compare/src/spade_crate.rs b/delaunay_compare/src/spade_crate.rs index d23167d..a7e85b8 100644 --- a/delaunay_compare/src/spade_crate.rs +++ b/delaunay_compare/src/spade_crate.rs @@ -5,7 +5,7 @@ type SpadePoint = spade::Point2; #[derive(Default)] pub struct SpadeCrateWithHintGenerator { vertices: Vec, - _hint_generator_type: std::marker::PhantomData, + _hint_generator_type: core::marker::PhantomData, } pub type SpadeCrate = SpadeCrateWithHintGenerator; diff --git a/examples/svg_renderer/main.rs b/examples/svg_renderer/main.rs index 14c9fe8..04a0103 100644 --- a/examples/svg_renderer/main.rs +++ b/examples/svg_renderer/main.rs @@ -2,7 +2,7 @@ pub mod quicksketch; mod scenario; mod scenario_list; -type Result = std::result::Result<(), Box>; +type Result = core::result::Result<(), Box>; /// Used for rendering SVGs for documentation. These are inlined (via #[doc = include_str!(...)]) /// into the doc comment of a few items. That makes sure they will be visible even for offline users. diff --git a/examples/svg_renderer/quicksketch/color.rs b/examples/svg_renderer/quicksketch/color.rs index f865595..21048cf 100644 --- a/examples/svg_renderer/quicksketch/color.rs +++ b/examples/svg_renderer/quicksketch/color.rs @@ -1,4 +1,4 @@ -use std::fmt::Display; +use core::fmt::Display; #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Copy)] pub struct SketchColor { @@ -8,7 +8,7 @@ pub struct SketchColor { } impl Display for SketchColor { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { write!(f, "rgb({} {} {})", self.red, self.green, self.blue) } } diff --git a/examples/svg_renderer/quicksketch/mod.rs b/examples/svg_renderer/quicksketch/mod.rs index ee173bd..1909ca4 100644 --- a/examples/svg_renderer/quicksketch/mod.rs +++ b/examples/svg_renderer/quicksketch/mod.rs @@ -178,7 +178,7 @@ impl Style { stroke_dash_array, ]) .flatten() - .chain(std::iter::once(fill)) + .chain(core::iter::once(fill)) .collect::>() .join("; ") } diff --git a/fuzz/fuzz_targets/bulk_load_fuzz.rs b/fuzz/fuzz_targets/bulk_load_fuzz.rs index 3f193cc..0d5dbc8 100644 --- a/fuzz/fuzz_targets/bulk_load_fuzz.rs +++ b/fuzz/fuzz_targets/bulk_load_fuzz.rs @@ -27,8 +27,8 @@ pub struct FuzzPoint { y: f64, } -impl std::fmt::Debug for FuzzPoint { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl core::fmt::Debug for FuzzPoint { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.write_fmt(format_args!("Point2::new({:?}, {:?})", self.x, self.y)) } } diff --git a/fuzz/fuzz_targets/bulk_load_int_fuzz.rs b/fuzz/fuzz_targets/bulk_load_int_fuzz.rs index 0a56f1a..f3c2cd8 100644 --- a/fuzz/fuzz_targets/bulk_load_int_fuzz.rs +++ b/fuzz/fuzz_targets/bulk_load_int_fuzz.rs @@ -26,8 +26,8 @@ pub struct IntFuzzPoint { y: i32, } -impl std::fmt::Debug for IntFuzzPoint { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl core::fmt::Debug for IntFuzzPoint { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.write_fmt(format_args!("Point2::new({:?}.0, {:?}.0)", self.x, self.y)) } } diff --git a/src/cdt.rs b/src/cdt.rs index 15edad6..dddf7d8 100644 --- a/src/cdt.rs +++ b/src/cdt.rs @@ -7,6 +7,8 @@ use crate::{ #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +use alloc::{vec, vec::Vec}; + /// Undirected edge type of a [ConstrainedDelaunayTriangulation] (CDT). /// /// CDTs need to store if an undirected edge is a constrained edge. To do so, CDTs don't use @@ -362,7 +364,7 @@ where VertexOutDirection::EdgeIntersection(edge) => edge, }; - let mut border_loop = std::collections::VecDeque::new(); + let mut border_loop = alloc::collections::VecDeque::new(); border_loop.push_back(first_edge.rev().next().fix()); border_loop.push_front(first_edge.rev().prev().fix()); @@ -552,6 +554,8 @@ mod test { type Cdt = ConstrainedDelaunayTriangulation>; type Delaunay = DelaunayTriangulation>; + use alloc::{vec, vec::Vec}; + #[test] fn test_add_single_simple_constraint() -> Result<(), InsertionError> { let mut cdt = Cdt::new(); @@ -657,7 +661,7 @@ mod test { fn test_add_border_constraint() -> Result<(), InsertionError> { let points = random_points_with_seed(1000, SEED); let mut cdt = Cdt::new(); - let mut max_y = -::std::f64::MAX; + let mut max_y = -::core::f64::MAX; for point in points { max_y = max_y.max(point.y); cdt.insert(point)?; @@ -695,7 +699,8 @@ mod test { for p in delaunay_points { d.insert(p)?; } - let mut used_vertices = ::std::collections::HashSet::new(); + let mut used_vertices = ::hashbrown::HashSet::new(); + let mut inserted_constraints = Vec::new(); for v in d.vertices() { // Insert only edges that do not touch at the end points if diff --git a/src/delaunay_core/bulk_load.rs b/src/delaunay_core/bulk_load.rs index 9232ffd..ad60be9 100644 --- a/src/delaunay_core/bulk_load.rs +++ b/src/delaunay_core/bulk_load.rs @@ -1,9 +1,11 @@ -use std::cmp::{Ordering, Reverse}; +use core::cmp::{Ordering, Reverse}; use crate::{HasPosition, InsertionError, Point2, Triangulation, TriangulationExt}; use super::{dcel_operations, FixedDirectedEdgeHandle, FixedUndirectedEdgeHandle}; +use alloc::vec::Vec; + /// An `f64` wrapper implementing `Ord` and `Eq`. /// /// This is only used as part of bulk loading. @@ -469,7 +471,7 @@ impl Hull { const INVALID: usize = usize::MAX; self.buckets - .extend(std::iter::repeat(INVALID).take(target_size)); + .extend(core::iter::repeat(INVALID).take(target_size)); let (first_index, current_node) = self .data @@ -753,6 +755,8 @@ mod test { use super::Hull; + use alloc::vec::Vec; + #[test] fn test_bulk_load_with_small_number_of_vertices() -> Result<(), InsertionError> { for size in 0..10 { diff --git a/src/delaunay_core/bulk_load_fuzz_tests.rs b/src/delaunay_core/bulk_load_fuzz_tests.rs index 6843f1a..4ad0efd 100644 --- a/src/delaunay_core/bulk_load_fuzz_tests.rs +++ b/src/delaunay_core/bulk_load_fuzz_tests.rs @@ -1,5 +1,7 @@ use crate::{DelaunayTriangulation, Point2, Triangulation, TriangulationExt}; +use alloc::{vec, vec::Vec}; + fn fuzz_test(vertices: Vec>) { let mut clone = vertices.clone(); clone.sort_by(|l, r| l.partial_cmp(r).unwrap()); diff --git a/src/delaunay_core/dcel.rs b/src/delaunay_core/dcel.rs index bbec1a1..6170300 100644 --- a/src/delaunay_core/dcel.rs +++ b/src/delaunay_core/dcel.rs @@ -5,6 +5,8 @@ use super::handles::*; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +use alloc::vec::Vec; + #[derive(Default, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy, Hash)] pub struct EdgeData { directed_data: [DE; 2], diff --git a/src/delaunay_core/dcel_operations.rs b/src/delaunay_core/dcel_operations.rs index b323b7f..9fb57ba 100644 --- a/src/delaunay_core/dcel_operations.rs +++ b/src/delaunay_core/dcel_operations.rs @@ -5,6 +5,8 @@ use super::handles::*; use smallvec::SmallVec; +use alloc::{vec, vec::Vec}; + pub const OUTER_FACE_HANDLE: FixedFaceHandle = new_fixed_face_handle(0); #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] @@ -1308,6 +1310,8 @@ mod test { use super::{Dcel, FixedDirectedEdgeHandle, FixedFaceHandle, FixedVertexHandle}; + use alloc::{vec, vec::Vec}; + fn default_triangle() -> Dcel { use super::{EdgeEntry, FaceEntry, HalfEdgeEntry, VertexEntry}; diff --git a/src/delaunay_core/handles/handle_defs.rs b/src/delaunay_core/handles/handle_defs.rs index 2e88880..46dbcda 100644 --- a/src/delaunay_core/handles/handle_defs.rs +++ b/src/delaunay_core/handles/handle_defs.rs @@ -1,4 +1,4 @@ -use std::convert::TryInto; +use core::convert::TryInto; use super::super::Dcel; use super::public_handles::{InnerOuterMarker, PossiblyOuterTag}; @@ -27,8 +27,8 @@ pub struct FixedHandleImpl { inner_outer: InnerOuter, } -impl std::fmt::Debug for FixedHandleImpl { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl core::fmt::Debug for FixedHandleImpl { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_struct("FixedHandle") .field("index", &self.index) .finish() diff --git a/src/delaunay_core/handles/handle_impls.rs b/src/delaunay_core/handles/handle_impls.rs index ac96735..7067ed3 100644 --- a/src/delaunay_core/handles/handle_impls.rs +++ b/src/delaunay_core/handles/handle_impls.rs @@ -6,20 +6,20 @@ use super::iterators::NextBackFn; use super::public_handles::*; use crate::{HasPosition, LineSideInfo, Point2}; +use core::cmp::Ordering; +use core::fmt::Debug; +use core::hash::{Hash, Hasher}; use num_traits::{Float, One}; -use std::cmp::Ordering; -use std::fmt::Debug; -use std::hash::{Hash, Hasher}; // Debug implementations -impl<'a, V, DE, UE, F> std::fmt::Debug for VertexHandle<'a, V, DE, UE, F> { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { +impl<'a, V, DE, UE, F> core::fmt::Debug for VertexHandle<'a, V, DE, UE, F> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!(f, "VertexHandle({:?})", self.handle.index()) } } impl<'a, V, DE, UE, F> Debug for DirectedEdgeHandle<'a, V, DE, UE, F> { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { write!( f, "DirectedEdgeHandle - id: {:?} ({:?} -> {:?})", @@ -30,8 +30,8 @@ impl<'a, V, DE, UE, F> Debug for DirectedEdgeHandle<'a, V, DE, UE, F> { } } -impl<'a, V, DE, UE, F> std::fmt::Debug for UndirectedEdgeHandle<'a, V, DE, UE, F> { - fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result { +impl<'a, V, DE, UE, F> core::fmt::Debug for UndirectedEdgeHandle<'a, V, DE, UE, F> { + fn fmt(&self, f: &mut core::fmt::Formatter) -> ::core::fmt::Result { let [v0, v1] = self.vertices(); write!( f, @@ -43,8 +43,8 @@ impl<'a, V, DE, UE, F> std::fmt::Debug for UndirectedEdgeHandle<'a, V, DE, UE, F } } -impl<'a, V, DE, UE, F> std::fmt::Debug for FaceHandle<'a, PossiblyOuterTag, V, DE, UE, F> { - fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result { +impl<'a, V, DE, UE, F> core::fmt::Debug for FaceHandle<'a, PossiblyOuterTag, V, DE, UE, F> { + fn fmt(&self, f: &mut core::fmt::Formatter) -> ::core::fmt::Result { if let Some(inner) = self.as_inner() { inner.fmt(f) } else { @@ -53,8 +53,8 @@ impl<'a, V, DE, UE, F> std::fmt::Debug for FaceHandle<'a, PossiblyOuterTag, V, D } } -impl<'a, V, DE, UE, F> std::fmt::Debug for FaceHandle<'a, InnerTag, V, DE, UE, F> { - fn fmt(&self, f: &mut std::fmt::Formatter) -> ::std::fmt::Result { +impl<'a, V, DE, UE, F> core::fmt::Debug for FaceHandle<'a, InnerTag, V, DE, UE, F> { + fn fmt(&self, f: &mut core::fmt::Formatter) -> ::core::fmt::Result { let [v0, v1, v2] = self.vertices(); write!( f, diff --git a/src/delaunay_core/handles/iterators/circular_iterator.rs b/src/delaunay_core/handles/iterators/circular_iterator.rs index e43351c..286db52 100644 --- a/src/delaunay_core/handles/iterators/circular_iterator.rs +++ b/src/delaunay_core/handles/iterators/circular_iterator.rs @@ -1,4 +1,4 @@ -use std::marker::PhantomData; +use core::marker::PhantomData; use super::super::DirectedEdgeHandle; diff --git a/src/delaunay_core/handles/iterators/fixed_iterators.rs b/src/delaunay_core/handles/iterators/fixed_iterators.rs index c143cc2..006075b 100644 --- a/src/delaunay_core/handles/iterators/fixed_iterators.rs +++ b/src/delaunay_core/handles/iterators/fixed_iterators.rs @@ -1,4 +1,4 @@ -use std::marker::PhantomData; +use core::marker::PhantomData; use super::super::handle_defs::{DelaunayElementType, DynamicHandleImpl, FixedHandleImpl}; use super::super::InnerOuterMarker; @@ -6,7 +6,7 @@ use super::super::InnerOuterMarker; use crate::delaunay_core::Dcel; pub struct FixedHandleIterator { - range: std::ops::Range, + range: core::ops::Range, ty: PhantomData, inner_outer: PhantomData, } @@ -59,7 +59,7 @@ impl DoubleEndedIterator pub struct DynamicHandleIterator<'a, V, DE, UE, F, Type, InnerOuter> { fixed_iterator: FixedHandleIterator, dcel: &'a Dcel, - inner_outer: std::marker::PhantomData, + inner_outer: core::marker::PhantomData, } impl<'a, V, DE, UE, F, Type, InnerOuter> DynamicHandleIterator<'a, V, DE, UE, F, Type, InnerOuter> @@ -71,7 +71,7 @@ where DynamicHandleIterator { fixed_iterator: FixedHandleIterator::new(Type::num_elements(dcel)), dcel, - inner_outer: std::marker::PhantomData, + inner_outer: core::marker::PhantomData, } } } diff --git a/src/delaunay_core/handles/iterators/hull_iterator.rs b/src/delaunay_core/handles/iterators/hull_iterator.rs index 4699d09..35fa69f 100644 --- a/src/delaunay_core/handles/iterators/hull_iterator.rs +++ b/src/delaunay_core/handles/iterators/hull_iterator.rs @@ -58,6 +58,9 @@ impl<'a, V, DE, UE, F> DoubleEndedIterator for HullIterator<'a, V, DE, UE, F> { #[cfg(test)] mod test { + + use alloc::vec::Vec; + use crate::test_utilities::{random_points_with_seed, SEED}; use crate::{DelaunayTriangulation, InsertionError, Point2, Triangulation}; diff --git a/src/delaunay_core/handles/public_handles.rs b/src/delaunay_core/handles/public_handles.rs index ec9271b..fab3566 100644 --- a/src/delaunay_core/handles/public_handles.rs +++ b/src/delaunay_core/handles/public_handles.rs @@ -19,7 +19,7 @@ pub const OUTER_FACE: FixedFaceHandle = dcel_operations::OUTER /// /// There should be no need to implement this. pub trait InnerOuterMarker: - Clone + Copy + PartialEq + Eq + PartialOrd + Ord + std::fmt::Debug + Default + std::hash::Hash + Clone + Copy + PartialEq + Eq + PartialOrd + Ord + core::fmt::Debug + Default + core::hash::Hash { fn debug_string() -> &'static str; } diff --git a/src/delaunay_core/hint_generator.rs b/src/delaunay_core/hint_generator.rs index 5304781..354c6e8 100644 --- a/src/delaunay_core/hint_generator.rs +++ b/src/delaunay_core/hint_generator.rs @@ -1,4 +1,4 @@ -use std::sync::atomic::{AtomicUsize, Ordering}; +use core::sync::atomic::{AtomicUsize, Ordering}; use crate::{ DelaunayTriangulation, HasPosition, Point2, SpadeNum, Triangulation, TriangulationExt, @@ -9,6 +9,8 @@ use super::FixedVertexHandle; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +use alloc::vec::Vec; + /// A structure used to speed up common operations on delaunay triangulations like insertion and geometry queries by providing /// hints on where to start searching for elements. /// @@ -288,6 +290,8 @@ mod test { Triangulation, TriangulationExt, }; + use alloc::vec::Vec; + const BRANCH_FACTOR: u32 = 3; type HierarchyTriangulation = DelaunayTriangulation< diff --git a/src/delaunay_core/math.rs b/src/delaunay_core/math.rs index 004293f..bd8a08d 100644 --- a/src/delaunay_core/math.rs +++ b/src/delaunay_core/math.rs @@ -1,5 +1,3 @@ -use std::{error::Error, fmt::Display}; - use crate::{HasPosition, LineSideInfo, Point2, SpadeNum}; use num_traits::Float; @@ -35,13 +33,14 @@ pub enum InsertionError { NAN, } -impl Display for InsertionError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ::fmt(self, f) +impl core::fmt::Display for InsertionError { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + ::fmt(self, f) } } -impl Error for InsertionError {} +#[cfg(feature = "std")] +impl std::error::Error for InsertionError {} /// The smallest allowed coordinate value greater than zero that can be inserted into Delaunay /// triangulations. This value is equal to 2-142. diff --git a/src/delaunay_core/triangulation_ext.rs b/src/delaunay_core/triangulation_ext.rs index 13f047b..5d06c2e 100644 --- a/src/delaunay_core/triangulation_ext.rs +++ b/src/delaunay_core/triangulation_ext.rs @@ -9,6 +9,8 @@ use crate::HintGenerator; use crate::Point2; use crate::{HasPosition, InsertionError, PositionInTriangulation, Triangulation}; +use alloc::{collections::VecDeque, vec::Vec}; + impl TriangulationExt for T where T: Triangulation + ?Sized {} pub enum PositionWhenAllVerticesOnLine { @@ -261,7 +263,7 @@ pub trait TriangulationExt: Triangulation { position: Point2<::Scalar>, forward_predicate: ForwardPredicate, backward_predicate: BackwardPredicate, - ) -> std::collections::VecDeque + ) -> VecDeque where ForwardPredicate: Fn( DirectedEdgeHandle, @@ -270,7 +272,7 @@ pub trait TriangulationExt: Triangulation { DirectedEdgeHandle, ) -> bool, { - let mut result = std::collections::VecDeque::with_capacity(8); + let mut result = VecDeque::with_capacity(8); let mut current_edge_forward = self.directed_edge(start_edge); debug_assert!(current_edge_forward.side_query(position).is_on_left_side()); @@ -635,7 +637,7 @@ pub trait TriangulationExt: Triangulation { vertex_to_remove, ); // Not exactly elegant. IsolateVertexResult should maybe be split into two parts - let mut new_edges = std::mem::take(&mut isolation_result.new_edges); + let mut new_edges = core::mem::take(&mut isolation_result.new_edges); self.legalize_edges_after_removal(&mut new_edges, |edge| { !isolation_result.is_new_edge(edge) }); @@ -860,6 +862,8 @@ mod test { use rand::distributions::{Distribution, Uniform}; use rand::{seq::SliceRandom, Rng, SeedableRng}; + use alloc::{vec, vec::Vec}; + #[test] fn test_empty() { let d = DelaunayTriangulation::>::default(); @@ -976,7 +980,7 @@ mod test { fn test_insert_outside_convex_hull() -> Result<(), InsertionError> { const NUM: usize = 100; let mut rng = rand::rngs::StdRng::from_seed(*SEED); - let range = Uniform::new(0., 2.0 * ::std::f64::consts::PI); + let range = Uniform::new(0., 2.0 * ::core::f64::consts::PI); let mut d = DelaunayTriangulation::<_>::default(); diff --git a/src/flood_fill_iterator.rs b/src/flood_fill_iterator.rs index 297c8c4..12a2965 100644 --- a/src/flood_fill_iterator.rs +++ b/src/flood_fill_iterator.rs @@ -1,4 +1,5 @@ -use std::collections::{HashSet, VecDeque}; +use alloc::{collections::VecDeque, vec::Vec}; +use hashbrown::HashSet; use num_traits::{one, zero, Float}; use smallvec::smallvec; @@ -554,6 +555,9 @@ where #[cfg(test)] mod test { + + use alloc::{vec, vec::Vec}; + use crate::{ flood_fill_iterator::{CircleMetric, DistanceMetric, RectangleMetric}, test_utilities::random_points_with_seed, diff --git a/src/intersection_iterator.rs b/src/intersection_iterator.rs index 321423b..28f052a 100644 --- a/src/intersection_iterator.rs +++ b/src/intersection_iterator.rs @@ -24,11 +24,11 @@ where EdgeOverlap(DirectedEdgeHandle<'a, V, DE, UE, F>), } -impl<'a, V, DE, UE, F> ::std::fmt::Debug for Intersection<'a, V, DE, UE, F> +impl<'a, V, DE, UE, F> ::core::fmt::Debug for Intersection<'a, V, DE, UE, F> where V: HasPosition, { - fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use self::Intersection::*; match self { EdgeIntersection(handle) => write!(f, "EdgeIntersection({:?})", handle), @@ -414,6 +414,8 @@ mod test { use super::*; use crate::{InsertionError, Point2, Triangulation as _}; + use alloc::{vec, vec::Vec}; + type Triangulation = crate::DelaunayTriangulation>; fn reverse<'a, V, DE, UE, F>( diff --git a/src/lib.rs b/src/lib.rs index 968c4f1..3985213 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,11 +9,17 @@ //! * Supports vertex removal //! * Serde support with the `serde` feature. +#![no_std] #![forbid(unsafe_code)] #![warn(clippy::all)] #![deny(rustdoc::broken_intra_doc_links)] #![cfg_attr(not(fuzzing), warn(missing_docs))] +#[cfg(feature = "std")] +extern crate std; + +extern crate alloc; + mod cdt; mod delaunay_core; mod delaunay_triangulation; diff --git a/src/point.rs b/src/point.rs index 1a1a430..f4283f2 100644 --- a/src/point.rs +++ b/src/point.rs @@ -10,12 +10,12 @@ use serde::{Deserialize, Serialize}; /// /// This type should usually be either `f32` or `f64`. pub trait SpadeNum: - Num + PartialOrd + Into + From + Copy + Signed + std::fmt::Debug + Num + PartialOrd + Into + From + Copy + Signed + core::fmt::Debug { } impl SpadeNum for T where - T: Num + PartialOrd + Into + From + Copy + Signed + std::fmt::Debug + T: Num + PartialOrd + Into + From + Copy + Signed + core::fmt::Debug { } diff --git a/src/test_utilities.rs b/src/test_utilities.rs index b729d4f..e662d55 100644 --- a/src/test_utilities.rs +++ b/src/test_utilities.rs @@ -6,6 +6,8 @@ use rand::SeedableRng; pub const SEED: &[u8; 32] = b"wPYxAkIiHcEmSBAxQFoXFrpYToCe1B71"; pub const SEED2: &[u8; 32] = b"14LzG37Y9EHTcmLW8vBDqWwtYsCeVVyF"; +use alloc::vec::Vec; + pub fn random_points_in_range(range: f64, size: usize, seed: &[u8; 32]) -> Vec> { let mut rng = rand::rngs::StdRng::from_seed(*seed); let range = Uniform::new(-range, range); diff --git a/src/triangulation.rs b/src/triangulation.rs index b4d1f4a..d2ab29c 100644 --- a/src/triangulation.rs +++ b/src/triangulation.rs @@ -12,6 +12,8 @@ use crate::HintGenerator; use crate::{delaunay_core::Dcel, handles::*}; use crate::{HasPosition, InsertionError, Point2, TriangulationExt}; +use alloc::vec::Vec; + /// Describes a position in a triangulation. /// /// The position is set in relation to the triangulation's vertices, edges and faces.