Skip to content

Commit 39f8697

Browse files
committed
Remove dependency on rand for no_std compatibility
1 parent 6e07fd7 commit 39f8697

File tree

10 files changed

+75
-116
lines changed

10 files changed

+75
-116
lines changed

Cargo.toml

+13-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "graphlib"
33
version = "0.6.3"
44
authors = ["Octavian Oncescu <[email protected]>"]
5-
edition = "2018"
5+
edition = "2021"
66
repository = "https://github.com/purpleprotocol/graphlib"
77
keywords = ["graph", "data-structures", "mutable", "graph-algorithm", "no-std"]
88
categories = ["data-structures", "no-std"]
@@ -14,22 +14,28 @@ readme = "README.md"
1414
travis-ci = { repository = "purpleprotocol/graphlib", branch = "master" }
1515

1616
[dependencies]
17-
rand = { version = "0.7.3", default-features = false }
18-
rand_core = { version = "0.5.1", default-features = false }
19-
rand_isaac = { version = "0.2.0", default-features = false }
20-
hex = { version = "0.4.0", default-features = false }
17+
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
2118
hashbrown = { version = "0.6.3", default-features = false, features = ["inline-more", "ahash"] }
22-
dot = { version = "0.1.4", optional = true }
19+
dot = { version = "0.1.4", default-features = false, optional = true }
2320

2421
[dev-dependencies]
2522
criterion = "0.3.0"
2623

24+
[lib]
25+
name = "graphlib"
26+
path = "src/lib.rs"
27+
crate-type = [
28+
"lib",
29+
]
30+
2731
[[bench]]
2832
name = "benchmark"
2933
harness = false
3034

3135
[features]
32-
default = ["hashbrown/nightly"]
36+
# std needs to be a default feature or else you get the following error:
37+
# ink! only supports compilation as `std` or `no_std` + `wasm32-unknown`"
38+
default = ["hashbrown/nightly", "std"]
3339
# use `cargo bench --features sbench` only if you want benchmarks with 10 million
3440
# iterations (may fail on some systems)
3541
sbench = []

README.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ Graphlib is a simple and powerful Rust graph library.
77

88
This library attempts to provide a generic api for building, mutating and iterating over graphs that is similar to that of other data-structures found in Rust i.e. `Vec`, `HashMap`, `VecDeque`, etc.
99

10+
## Modifications in this fork from the original
11+
1. Fix compilation error when dot feature is enabled.
12+
2. Replace no_std feature with a std feature (and reverse the default behavior).
13+
3. Remove the dependency on rand. This library causes problems in constrained environments that do not have access to an RNG.
14+
1015
### Using Graphlib
1116
```rust
1217
use graphlib::Graph;
@@ -34,13 +39,6 @@ assert_eq!(graph.vertex_count(), 1);
3439
assert_eq!(graph.edge_count(), 0);
3540
```
3641

37-
### Using without `std`
38-
In `Cargo.toml`:
39-
```toml
40-
[dependencies]
41-
graphlib = { version = "*", features = ["no_std"] }
42-
```
43-
4442
### Contributing
4543
We welcome anyone wishing to contribute to Graphlib! Check out the [issues section][issues] of the repository before starting out.
4644

rust-toolchain.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[toolchain]
2+
channel = "nightly-2022-09-08"
3+
components = [ "rustfmt" ]
4+
targets = [ "wasm32-unknown-unknown" ]

src/dot.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
11
use crate::{Graph, GraphErr, VertexId};
22

33
#[cfg(not(feature = "std"))]
4-
use core::io::Write;
5-
6-
#[cfg(feature = "std")]
7-
use std::io::Write;
8-
9-
#[cfg(not(feature = "std"))]
10-
use core::borrow::Cow;
4+
use alloc::borrow::Cow;
115

126
#[cfg(feature = "std")]
137
use std::borrow::Cow;
148

15-
#[cfg(not(feature = "std"))]
16-
use core::fmt::Debug;
17-
18-
#[cfg(feature = "std")]
19-
use std::fmt::Debug;
20-
219
type Nd = VertexId;
2210
type Ed<'a> = (&'a VertexId, &'a VertexId);
2311

@@ -43,7 +31,7 @@ impl<'a, T> dot::Labeller<'a, Nd, Ed<'a>> for DotGraph<'a, T> {
4331
}
4432

4533
fn node_id(&'a self, n: &Nd) -> dot::Id<'a> {
46-
let hex = format!("N{}", hex::encode(n.bytes()));
34+
let hex = format!("N{}", n.val());
4735
dot::Id::new(hex).unwrap()
4836
}
4937

src/edge.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ impl Edge {
4040

4141
/// Returns true if the given vertex ids are the
4242
/// inbound and outbound vertices of the edge.
43+
#[allow(dead_code)]
4344
pub(crate) fn matches(&self, a: &VertexId, b: &VertexId) -> bool {
4445
a == &self.outbound && b == &self.inbound
4546
}
4647

4748
/// Returns true if either the inbound or outbound
4849
/// vertex is matching the given `VertexId`.
50+
#[allow(dead_code)]
4951
pub(crate) fn matches_any(&self, id: &VertexId) -> bool {
5052
id == &self.inbound || id == &self.outbound
5153
}

src/graph.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ use alloc::vec;
2424
#[cfg(not(feature = "std"))]
2525
use alloc::vec::Vec;
2626

27-
#[cfg(feature = "dot")]
28-
use super::SEED;
2927

3028
#[cfg(feature = "dot")]
3129
const DEFAULT_LABEL: &str = "";
@@ -80,6 +78,8 @@ pub struct Graph<T> {
8078
/// Mapping between vertex ids and outbound edges
8179
outbound_table: HashMap<VertexId, Vec<VertexId>>,
8280

81+
next_vertex_id: u32,
82+
8383
#[cfg(feature = "dot")]
8484
/// Mapping between vertices and labels
8585
vertex_labels: HashMap<VertexId, String>,
@@ -109,6 +109,7 @@ impl<T> Graph<T> {
109109
tips: HashSet::new(),
110110
inbound_table: HashMap::new(),
111111
outbound_table: HashMap::new(),
112+
next_vertex_id: 1,
112113

113114
#[cfg(feature = "dot")]
114115
vertex_labels: HashMap::new(),
@@ -139,6 +140,7 @@ impl<T> Graph<T> {
139140
tips: HashSet::with_capacity(capacity),
140141
inbound_table: HashMap::with_capacity(capacity),
141142
outbound_table: HashMap::with_capacity(capacity),
143+
next_vertex_id: 1,
142144

143145
#[cfg(feature = "dot")]
144146
vertex_labels: HashMap::with_capacity(capacity),
@@ -264,7 +266,10 @@ impl<T> Graph<T> {
264266
/// assert_eq!(graph.fetch(&id).unwrap(), &1);
265267
/// ```
266268
pub fn add_vertex(&mut self, item: T) -> VertexId {
267-
let id = VertexId::random();
269+
let id = VertexId::new(self.next_vertex_id);
270+
// This library is clearly not thread-safe, so we do not bother
271+
// using an atomic int
272+
self.next_vertex_id += 1;
268273

269274
self.vertices.insert(id, (item, id));
270275
self.roots.insert(id);
@@ -282,7 +287,7 @@ impl<T> Graph<T> {
282287
/// let mut graph: Graph<usize> = Graph::new();
283288
///
284289
/// // Id of vertex that is not place in the graph
285-
/// let id = VertexId::random();
290+
/// let id = VertexId::new(100);
286291
///
287292
/// let v1 = graph.add_vertex(1);
288293
/// let v2 = graph.add_vertex(2);
@@ -316,7 +321,7 @@ impl<T> Graph<T> {
316321
/// let mut graph: Graph<usize> = Graph::new();
317322
///
318323
/// // Id of vertex that is not place in the graph
319-
/// let id = VertexId::random();
324+
/// let id = VertexId::new(100);
320325
///
321326
/// let v1 = graph.add_vertex(1);
322327
/// let v2 = graph.add_vertex(2);
@@ -347,7 +352,7 @@ impl<T> Graph<T> {
347352
/// let mut graph: Graph<usize> = Graph::new();
348353
///
349354
/// // Id of vertex that is not place in the graph
350-
/// let id = VertexId::random();
355+
/// let id = VertexId::new(100);
351356
///
352357
/// let v1 = graph.add_vertex(1);
353358
/// let v2 = graph.add_vertex(2);
@@ -385,7 +390,7 @@ impl<T> Graph<T> {
385390
/// let mut graph: Graph<usize> = Graph::new();
386391
///
387392
/// // Id of vertex that is not place in the graph
388-
/// let id = VertexId::random();
393+
/// let id = VertexId::new(100);
389394
///
390395
/// let v1 = graph.add_vertex(1);
391396
/// let v2 = graph.add_vertex(2);
@@ -420,7 +425,7 @@ impl<T> Graph<T> {
420425
/// let mut graph: Graph<usize> = Graph::new();
421426
///
422427
/// // Id of vertex that is not place in the graph
423-
/// let id = VertexId::random();
428+
/// let id = VertexId::new(100);
424429
///
425430
/// let v1 = graph.add_vertex(1);
426431
/// let v2 = graph.add_vertex(2);
@@ -1371,7 +1376,7 @@ impl<T> Graph<T> {
13711376
/// use graphlib::{Graph, VertexId};
13721377
///
13731378
/// let mut graph: Graph<usize> = Graph::new();
1374-
/// let random_id = VertexId::random();
1379+
/// let random_id = VertexId::new(100);
13751380
///
13761381
/// let v1 = graph.add_vertex(0);
13771382
/// let v2 = graph.add_vertex(1);
@@ -1403,7 +1408,7 @@ impl<T> Graph<T> {
14031408
/// use graphlib::{Graph, VertexId};
14041409
///
14051410
/// let mut graph: Graph<usize> = Graph::new();
1406-
/// let random_id = VertexId::random();
1411+
/// let random_id = VertexId::new(100);
14071412
///
14081413
/// let v1 = graph.add_vertex(0);
14091414
/// let v2 = graph.add_vertex(1);
@@ -1471,7 +1476,7 @@ impl<T> Graph<T> {
14711476
/// use graphlib::{Graph, VertexId};
14721477
///
14731478
/// let mut graph: Graph<usize> = Graph::new();
1474-
/// let random_id = VertexId::random();
1479+
/// let random_id = VertexId::new(100);
14751480
/// let mut vertex_id: usize = 1;
14761481
///
14771482
/// let v1 = graph.add_vertex(0);
@@ -1526,7 +1531,7 @@ impl<T> Graph<T> {
15261531
/// use graphlib::{Graph, VertexId};
15271532
///
15281533
/// let mut graph: Graph<usize> = Graph::new();
1529-
/// let random_id = VertexId::random();
1534+
/// let random_id = VertexId::new(100);
15301535
/// let mut vertex_id: usize = 1;
15311536
///
15321537
/// let v1 = graph.add_vertex(0);
@@ -1843,9 +1848,6 @@ mod tests {
18431848
fn test_add_edge_cycle_check() {
18441849
let mut graph: Graph<usize> = Graph::new();
18451850

1846-
// Id of vertex that is not place in the graph
1847-
let id = VertexId::random();
1848-
18491851
let v1 = graph.add_vertex(1);
18501852
let v2 = graph.add_vertex(2);
18511853

src/iterators/dijkstra.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ mod tests {
184184

185185
#[test]
186186
fn test_new_with_empty_graph() {
187-
let random_vertex = VertexId::random();
187+
let random_vertex = VertexId::new(100);
188188

189189
let graph: Graph<usize> = Graph::new();
190190
let result = Dijkstra::new(&graph, &random_vertex);
@@ -194,7 +194,7 @@ mod tests {
194194

195195
#[test]
196196
fn test_new_with_invalid_source() {
197-
let random_vertex = VertexId::random();
197+
let random_vertex = VertexId::new(100);
198198

199199
let mut graph: Graph<usize> = Graph::new();
200200
let v1 = graph.add_vertex(1);
@@ -222,7 +222,7 @@ mod tests {
222222

223223
#[test]
224224
fn test_set_source_with_invalid_vertex() {
225-
let random_vertex = VertexId::random();
225+
let random_vertex = VertexId::new(100);
226226

227227
let mut graph: Graph<usize> = Graph::new();
228228
let v1 = graph.add_vertex(1);
@@ -237,7 +237,7 @@ mod tests {
237237

238238
#[test]
239239
fn test_get_path_to_with_invalid_vertex() {
240-
let random_vertex = VertexId::random();
240+
let random_vertex = VertexId::new(100);
241241

242242
let mut graph: Graph<usize> = Graph::new();
243243
let v1 = graph.add_vertex(1);
@@ -252,7 +252,7 @@ mod tests {
252252

253253
#[test]
254254
fn test_get_distance_with_invalid_vertex() {
255-
let random_vertex = VertexId::random();
255+
let random_vertex = VertexId::new(100);
256256

257257
let mut graph: Graph<usize> = Graph::new();
258258
let v1 = graph.add_vertex(1);

src/iterators/owning_iterator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ mod tests {
7373
#[test]
7474
fn it_yields_correct_vertex_ids() {
7575
let ids: VecDeque<VertexId> =
76-
vec![VertexId::random(), VertexId::random(), VertexId::random()]
76+
vec![VertexId::new(1), VertexId::new(2), VertexId::new(3)]
7777
.iter()
7878
.cloned()
7979
.collect();

0 commit comments

Comments
 (0)