From 958ca8a4dd06554b210bc1b928eea93d78cbb2f6 Mon Sep 17 00:00:00 2001 From: Toralf Wittner Date: Mon, 17 Sep 2018 18:35:24 +0200 Subject: [PATCH] Use paritytech/int-hasher and add comments. --- Cargo.toml | 1 + src/lib.rs | 1 + src/{notify/mod.rs => notify.rs} | 16 +++++++++--- src/notify/hash.rs | 44 -------------------------------- 4 files changed, 14 insertions(+), 48 deletions(-) rename src/{notify/mod.rs => notify.rs} (78%) delete mode 100644 src/notify/hash.rs diff --git a/Cargo.toml b/Cargo.toml index cbdd95b0..8f4e2b49 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ license = "MIT" [dependencies] bytes = "0.4" futures = "0.1" +int-hasher = { git = "https://github.com/paritytech/int-hasher" } log = "0.4" parking_lot = "0.6" quick-error = "0.1" diff --git a/src/lib.rs b/src/lib.rs index 2fa44bd1..fccbbb1c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,7 @@ extern crate bytes; #[macro_use] extern crate futures; +extern crate int_hasher; #[macro_use] extern crate log; extern crate parking_lot; diff --git a/src/notify/mod.rs b/src/notify.rs similarity index 78% rename from src/notify/mod.rs rename to src/notify.rs index 80b90383..08b16f31 100644 --- a/src/notify/mod.rs +++ b/src/notify.rs @@ -17,11 +17,10 @@ // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -mod hash; use futures::{executor, task}; use parking_lot::Mutex; -use self::hash::HashMap; +use int_hasher::IntMap; use std::sync::atomic::{AtomicUsize, Ordering}; @@ -32,19 +31,27 @@ task_local!{ } +/// A notifier maintains a collection of tasks which should be +/// notified at some point. Useful in conjuction with `futures::executor::Spawn`. pub struct Notifier { - tasks: Mutex> + tasks: Mutex> } impl Notifier { pub fn new() -> Self { - Notifier { tasks: Mutex::new(HashMap::default()) } + Notifier { tasks: Mutex::new(IntMap::default()) } } + /// Insert the current task to the set of tasks to be notified. + /// + /// # Panics + /// + /// If called outside of a tokio task. pub fn insert_current(&self) { self.tasks.lock().insert(TASK_ID.with(|&t| t), task::current()); } + /// Notify all registered tasks. pub fn notify_all(&self) { let mut tasks = self.tasks.lock(); for (_, t) in tasks.drain() { @@ -52,6 +59,7 @@ impl Notifier { } } + /// Return the number of currently registered tasks. pub fn len(&self) -> usize { self.tasks.lock().len() } diff --git a/src/notify/hash.rs b/src/notify/hash.rs deleted file mode 100644 index 07681132..00000000 --- a/src/notify/hash.rs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright 2018 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a copy of -// this software and associated documentation files (the "Software"), to deal in -// the Software without restriction, including without limitation the rights to -// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -// the Software, and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS -// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, -// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -use std::{self, hash::{BuildHasherDefault, Hasher}, marker::PhantomData}; - -pub type HashMap = - std::collections::HashMap>>; - -#[derive(Copy, Clone, Debug, Default)] -pub struct Stateless(u64, PhantomData); - -impl Hasher for Stateless { - fn finish(&self) -> u64 { self.0 } - fn write(&mut self, _: &[u8]) { unimplemented!() } - fn write_u8(&mut self, _: u8) {unimplemented!() } - fn write_u16(&mut self, _: u16) { unimplemented!() } - fn write_u32(&mut self, _: u32) { unimplemented!() } - fn write_u64(&mut self, _: u64) { unimplemented!() } - fn write_u128(&mut self, _: u128) { unimplemented!() } - fn write_usize(&mut self, n: usize) { self.0 = n as u64 } - fn write_i8(&mut self, _: i8) { unimplemented!() } - fn write_i16(&mut self, _: i16) { unimplemented!() } - fn write_i32(&mut self, _: i32) { unimplemented!() } - fn write_i64(&mut self, _: i64) { unimplemented!() } - fn write_i128(&mut self, _: i128) { unimplemented!() } - fn write_isize(&mut self, _: isize) { unimplemented!() } -} -