From 281edbc4e2d2a99a0658fd642028771541f69414 Mon Sep 17 00:00:00 2001 From: Monica Moniot Date: Wed, 16 Aug 2023 15:44:42 -0400 Subject: [PATCH] reformat --- Cargo.lock | 39 ++++++++++++++++++++++++--------------- Cargo.toml | 6 +++--- examples/calculator.rs | 16 ++++------------ examples/hello_world.rs | 2 +- src/single_thread.rs | 16 ++++++++-------- src/sync.rs | 30 +++++++++++------------------- 6 files changed, 51 insertions(+), 58 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8006a3d..cc1b8a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,21 +3,21 @@ version = 3 [[package]] -name = "futex" -version = "0.1.3" +name = "cfg-if" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d370e15a8972dee506ad50638e331183109e85ff99f349f19e04f288dc6cef3" -dependencies = [ - "integer-atomics", - "libc", - "lock-wrappers", -] +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] -name = "integer-atomics" -version = "1.0.2" +name = "getrandom" +version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c33cd4d18b4ade167caace0e92364e8568c1e47c193738397b4b48a3e414139" +checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] [[package]] name = "libc" @@ -26,14 +26,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" [[package]] -name = "lock-wrappers" -version = "0.1.2" +name = "rand_core" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d654f44a90e266c873afdcf93f4506d9d6fb036b211e2be63a09695ecb0a07a" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] [[package]] name = "seq_ex" version = "0.1.0" dependencies = [ - "futex", + "rand_core", ] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml index 0aaba56..8cf1979 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ doc = true [features] default = ["std"] -std = ["futex"] +std = [] -[dependencies] -futex = {version = "0.1.3", optional = true} +[dev-dependencies] +rand_core = { version = "0.6.4", features = ["getrandom"]} diff --git a/examples/calculator.rs b/examples/calculator.rs index 5fce2fa..fab1805 100644 --- a/examples/calculator.rs +++ b/examples/calculator.rs @@ -1,8 +1,4 @@ -use std::{ - sync::{mpsc::Receiver, Mutex}, - thread, - time::Duration, -}; +use std::{sync::mpsc::Receiver, thread, time::Duration}; use seq_ex::sync::{MpscTransport, PacketType, RecvSuccess, ReplyGuard, SeqExSync}; @@ -16,12 +12,8 @@ enum Packet { } fn drop_packet() -> bool { - static RNG: Mutex = Mutex::new(43); - let mut rng = RNG.lock().unwrap(); - *rng ^= *rng << 13; - *rng ^= *rng >> 17; - *rng ^= *rng << 5; - *rng & 1 == 0 + use rand_core::RngCore; + rand_core::OsRng.next_u32() & 1 > 0 } fn process(_: ReplyGuard<'_, &MpscTransport>, recv_packet: Packet, _: Option, value: &mut f32) { @@ -48,7 +40,7 @@ fn receive<'a>( let _ = seq.receive_ack(reply_no); } PacketType::Payload { seq_no, reply_no, payload } => { - for RecvSuccess { guard, packet, send_data } in seq.receive_iter(transport, seq_no, reply_no, payload) { + for RecvSuccess { guard, packet, send_data } in seq.receive_all(transport, seq_no, reply_no, payload) { process(guard, packet, send_data, value); } } diff --git a/examples/hello_world.rs b/examples/hello_world.rs index ef3716d..dc291af 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -47,7 +47,7 @@ fn receive<'a>(recv: &Receiver>, seq: &SeqExSync<&'a MpscTran } } PacketType::Payload { seq_no, reply_no, payload } => { - for RecvSuccess {guard, packet, send_data} in seq.receive_iter(transport, seq_no, reply_no, payload) { + for RecvSuccess { guard, packet, send_data } in seq.receive_all(transport, seq_no, reply_no, payload) { process(guard, packet, send_data) } } diff --git a/src/single_thread.rs b/src/single_thread.rs index 935a987..a96eddb 100644 --- a/src/single_thread.rs +++ b/src/single_thread.rs @@ -1,6 +1,6 @@ -use crate::{Error, SeqEx, SeqNo, TransportLayer}; +use crate::{Error, SeqEx, SeqNo, TransportLayer, DEFAULT_WINDOW_CAP}; -pub struct ReplyGuard<'a, TL: TransportLayer>(&'a mut SeqEx, TL, SeqNo); +pub struct ReplyGuard<'a, TL: TransportLayer, const CAP: usize = DEFAULT_WINDOW_CAP>(&'a mut SeqEx, TL, SeqNo); impl<'a, TL: TransportLayer> ReplyGuard<'a, TL> { /// If you need to reply more than once, say to fragment a large file, then include in your /// first reply some identifier, and then `send` all fragments with the same included identifier. @@ -12,30 +12,30 @@ impl<'a, TL: TransportLayer> ReplyGuard<'a, TL> { core::mem::forget(self); } } -impl<'a, TL: TransportLayer> Drop for ReplyGuard<'a, TL> { +impl<'a, TL: TransportLayer, const CAP: usize> Drop for ReplyGuard<'a, TL, CAP> { fn drop(&mut self) { self.0.ack_raw(self.1.clone(), self.2) } } -pub struct RecvSuccess<'a, TL: TransportLayer, P> { - pub guard: ReplyGuard<'a, TL>, +pub struct RecvSuccess<'a, TL: TransportLayer, P, const CAP: usize = DEFAULT_WINDOW_CAP> { + pub guard: ReplyGuard<'a, TL, CAP>, pub packet: P, pub send_data: Option, } -impl SeqEx { +impl SeqEx { pub fn receive>( &mut self, app: TL, seq_no: SeqNo, reply_no: Option, packet: P, - ) -> Result, Error> { + ) -> Result, Error> { self.receive_raw(app.clone(), seq_no, reply_no, packet) .map(|(reply_no, packet, send_data)| RecvSuccess { guard: ReplyGuard(self, app, reply_no), packet, send_data }) } - pub fn pump(&mut self, app: TL) -> Result, Error> { + pub fn pump(&mut self, app: TL) -> Result, Error> { self.pump_raw() .map(|(reply_no, packet, send_data)| RecvSuccess { guard: ReplyGuard(self, app, reply_no), packet, send_data }) } diff --git a/src/sync.rs b/src/sync.rs index 63e8b37..1aed663 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -8,9 +8,7 @@ use std::{ time::Instant, }; -use crate::{ - Error, SeqEx, SeqNo, TransportLayer, DEFAULT_INITIAL_SEQ_NO, DEFAULT_WINDOW_CAP, DEFAULT_RESEND_INTERVAL_MS, -}; +use crate::{Error, SeqEx, SeqNo, TransportLayer, DEFAULT_INITIAL_SEQ_NO, DEFAULT_RESEND_INTERVAL_MS, DEFAULT_WINDOW_CAP}; pub struct SeqExSync { seq_ex: Mutex>, @@ -46,6 +44,12 @@ pub struct RecvSuccess<'a, TL: TransportLayer, P: Into, const CAP: pub send_data: Option, } +pub struct ReplyIter<'a, TL: TransportLayer, const CAP: usize = DEFAULT_WINDOW_CAP> { + origin: Option<&'a SeqExSync>, + app: TL, + first: Option>, +} + impl SeqExSync { pub fn new(retry_interval: i64, initial_seq_no: SeqNo) -> Self { Self { @@ -74,13 +78,7 @@ impl SeqExSync { self.unblock(ret.is_ok()); ret.map(|(reply_no, packet, send_data)| RecvSuccess { guard: ReplyGuard(self, app, reply_no), packet, send_data }) } - pub fn receive_iter( - &self, - app: TL, - seq_no: SeqNo, - reply_no: Option, - packet: TL::RecvData, - ) -> ReplyIter<'_, TL, CAP> { + pub fn receive_all(&self, app: TL, seq_no: SeqNo, reply_no: Option, packet: TL::RecvData) -> ReplyIter<'_, TL, CAP> { if let Ok(g) = self.receive(app.clone(), seq_no, reply_no, packet) { ReplyIter { origin: Some(self), app, first: Some(g) } } else { @@ -121,20 +119,14 @@ impl SeqExSync { self.seq_ex.lock().unwrap() } } -impl Default for SeqExSync { +impl Default for SeqExSync { fn default() -> Self { Self::new(DEFAULT_RESEND_INTERVAL_MS, DEFAULT_INITIAL_SEQ_NO) } } -pub struct ReplyIter<'a, TL: TransportLayer, const CAP: usize = DEFAULT_WINDOW_CAP> { - origin: Option<&'a SeqExSync>, - app: TL, - first: Option> -} - -impl<'a, TL: TransportLayer> Iterator for ReplyIter<'a, TL> { - type Item = RecvSuccess<'a, TL, TL::RecvData>; +impl<'a, TL: TransportLayer, const CAP: usize> Iterator for ReplyIter<'a, TL, CAP> { + type Item = RecvSuccess<'a, TL, TL::RecvData, CAP>; fn next(&mut self) -> Option { if let Some(g) = self.first.take() { Some(g)