diff --git a/actix-utils/CHANGES.md b/actix-utils/CHANGES.md index c911a211..57ab7add 100644 --- a/actix-utils/CHANGES.md +++ b/actix-utils/CHANGES.md @@ -1,6 +1,9 @@ # Changes ## Unreleased - 2021-xx-xx + + +## 3.0.0-beta.3 - 2021-04-01 * Moved `mpsc` to own crate `local-channel`. [#301] * Moved `task::LocalWaker` to own crate `local-waker`. [#301] * Remove `timeout` module. [#301] diff --git a/actix-utils/Cargo.toml b/actix-utils/Cargo.toml index 02bc3114..019d6d98 100644 --- a/actix-utils/Cargo.toml +++ b/actix-utils/Cargo.toml @@ -1,12 +1,13 @@ [package] name = "actix-utils" -version = "3.0.0-beta.2" -authors = ["Nikolay Kim "] -description = "Various network related services and utilities for the Actix ecosystem" +version = "3.0.0-beta.3" +authors = [ + "Nikolay Kim ", + "Rob Ede ", +] +description = "Utilities for the Actix ecosystem" keywords = ["network", "framework", "async", "futures"] -homepage = "https://actix.rs" repository = "https://github.com/actix/actix-net.git" -documentation = "https://docs.rs/actix-utils" categories = ["network-programming", "asynchronous"] license = "MIT OR Apache-2.0" edition = "2018" diff --git a/actix-utils/src/counter.rs b/actix-utils/src/counter.rs index c0926b73..7a87fa3d 100644 --- a/actix-utils/src/counter.rs +++ b/actix-utils/src/counter.rs @@ -1,24 +1,18 @@ //! Task-notifying counter. -use core::{cell::Cell, task}; +use core::{cell::Cell, fmt, task}; use std::rc::Rc; use local_waker::LocalWaker; -#[derive(Clone)] /// Simple counter with ability to notify task on reaching specific number /// /// Counter could be cloned, total n-count is shared across all clones. +#[derive(Debug, Clone)] pub struct Counter(Rc); -struct CounterInner { - count: Cell, - capacity: usize, - task: LocalWaker, -} - impl Counter { - /// Create `Counter` instance and set max value. + /// Create `Counter` instance with max value. pub fn new(capacity: usize) -> Self { Counter(Rc::new(CounterInner { capacity, @@ -27,38 +21,26 @@ impl Counter { })) } - /// Get counter guard. + /// Create new counter guard, incrementing the counter. pub fn get(&self) -> CounterGuard { CounterGuard::new(self.0.clone()) } - /// Check if counter is not at capacity. If counter at capacity - /// it registers notification for current task. + /// Notify current task and return true if counter is at capacity. pub fn available(&self, cx: &mut task::Context<'_>) -> bool { self.0.available(cx) } - /// Get total number of acquired counts + /// Get total number of acquired guards. pub fn total(&self) -> usize { self.0.count.get() } } -pub struct CounterGuard(Rc); - -impl CounterGuard { - fn new(inner: Rc) -> Self { - inner.inc(); - CounterGuard(inner) - } -} - -impl Unpin for CounterGuard {} - -impl Drop for CounterGuard { - fn drop(&mut self) { - self.0.dec(); - } +struct CounterInner { + count: Cell, + capacity: usize, + task: LocalWaker, } impl CounterInner { @@ -83,3 +65,32 @@ impl CounterInner { } } } + +impl fmt::Debug for CounterInner { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Counter") + .field("count", &self.count.get()) + .field("capacity", &self.capacity) + .field("task", &self.task) + .finish() + } +} + +/// An RAII structure that keeps the underlying counter incremented until this guard is dropped. +#[derive(Debug)] +pub struct CounterGuard(Rc); + +impl CounterGuard { + fn new(inner: Rc) -> Self { + inner.inc(); + CounterGuard(inner) + } +} + +impl Unpin for CounterGuard {} + +impl Drop for CounterGuard { + fn drop(&mut self) { + self.0.dec(); + } +} diff --git a/actix-utils/src/lib.rs b/actix-utils/src/lib.rs index d0e057ff..f94147ec 100644 --- a/actix-utils/src/lib.rs +++ b/actix-utils/src/lib.rs @@ -1,7 +1,7 @@ -//! Various network related services and utilities for the Actix ecosystem. +//! Various utilities for the Actix ecosystem. #![deny(rust_2018_idioms, nonstandard_style)] -#![allow(clippy::type_complexity)] +#![warn(missing_docs)] #![doc(html_logo_url = "https://actix.rs/img/logo.png")] #![doc(html_favicon_url = "https://actix.rs/favicon.ico")]