From 46831b6806e4aa67ce48b6d1ecebdd4481b78bea Mon Sep 17 00:00:00 2001 From: Kai Mast Date: Mon, 8 Jul 2024 02:16:38 -0700 Subject: [PATCH] Add network and serde feature flags (#2) --- Cargo.lock | 24 +----------------------- Cargo.toml | 16 ++++++++++++---- NEWS | 1 + README.md | 6 +++++- src/lib.rs | 1 + src/network/node.rs | 4 ++++ src/time/primitives.rs | 4 +++- 7 files changed, 27 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c314d42..e244d91 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,13 +28,12 @@ dependencies = [ [[package]] name = "asim" -version = "0.1.0" +version = "0.2.0" dependencies = [ "asim-macros", "async-trait", "env_logger", "futures", - "instant", "log 0.4.22", "parking_lot", "rand", @@ -217,15 +216,6 @@ version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" -[[package]] -name = "instant" -version = "0.1.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0242819d153cba4b4b05a5a8f2a7e9bbf97b6055b2a002b395c96b5ff3c0222" -dependencies = [ - "cfg-if", -] - [[package]] name = "lazy_static" version = "1.5.0" @@ -482,18 +472,6 @@ checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" dependencies = [ "backtrace", "pin-project-lite", - "tokio-macros", -] - -[[package]] -name = "tokio-macros" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a" -dependencies = [ - "proc-macro2", - "quote", - "syn", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index eeffea1..8ecac53 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,11 +14,19 @@ asim-macros = { path="macros" } log = "0.4" futures = "0.3" parking_lot = "0.12" -tokio = { version="1", features=["sync", "time", "macros", "rt"], default-features=false } -instant = "0.1" -serde = { version="1", features=["derive"] } +tokio = { version="1", features=["sync"], default-features=false } +serde = { version="1", features=["derive"], optional=true } async-trait = "0.1" -rand = "0.8" +rand = { version="0.8", optional=true } + +[[test]] +name = "speed-test" +path = "tests/speed_test.rs" +required-features = ["network"] [dev-dependencies] env_logger = "0.4" + +[features] +default = ["network", "serde"] +network = ["dep:rand"] diff --git a/NEWS b/NEWS index 64f6c2a..ff82a0b 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,7 @@ - Add asim::spawn() function similar to tokio::spawn() - Add asim::time::sleep() function similar to tokio::time::sleep() - Improve documentation and add examples + - Move serde and network module behind feature gates 0.1: - Initial Release diff --git a/README.md b/README.md index c9ad099..f84e6d1 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,11 @@ The main goal of this crate is to make simulation code look very similar to that This is achieved by providing an API similar to that of the standard library or tokio but with an implementation based on discrete events. This crate provides the simulator itself, a timer that allows an asynchronous task to sleep, and synchronization primitives. -Additionally, it includes basic primitives for processes and links that can be used to jump start your process. +Additionally, it includes basic primitives to simulate networking (guarded behind the "network" feature). + +## Features +* `network`: Enable networking primitives +* `serde`: Support serialization of (some) asim objects using serde ## Usage Take a look at the tests and examples to get an idea how this library is intended to be used. diff --git a/src/lib.rs b/src/lib.rs index abcba21..6d4b0e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ use std::future::Future; pub mod sync; +#[cfg(feature = "network")] pub mod network; pub mod time; diff --git a/src/network/node.rs b/src/network/node.rs index bfc0201..0c10275 100644 --- a/src/network/node.rs +++ b/src/network/node.rs @@ -182,6 +182,10 @@ impl Node { /// Returns the connection to another node with the specified identifier (if it exists) pub fn get_link_to(&self, node_id: &ObjectId) -> Option>> { + if *node_id == self.identifier { + panic!("There cannot be a link to the node itself"); + } + match self.network_links.borrow().get(node_id) { Some(link) => Some(link.clone()), None => { diff --git a/src/time/primitives.rs b/src/time/primitives.rs index 022fc86..84df68b 100644 --- a/src/time/primitives.rs +++ b/src/time/primitives.rs @@ -1,7 +1,9 @@ +#[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; /// Elapsed simulated time in nanoseconds -#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Ord, Eq)] pub struct Time(u64); /// A period of simulated time