Add dyn keyword to dyn traits as per clippy's advice

This commit is contained in:
Francis Lalonde 2019-07-11 11:48:14 -04:00
parent 5b466b6dcc
commit 314764987b
22 changed files with 50 additions and 46 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "dipstick" name = "dipstick"
version = "0.7.9" version = "0.7.10"
authors = ["Francis Lalonde <fralalonde@gmail.com>"] authors = ["Francis Lalonde <fralalonde@gmail.com>"]
description = """A fast, all-purpose metrics library decoupling instrumentation from reporting backends. description = """A fast, all-purpose metrics library decoupling instrumentation from reporting backends.

View File

@ -77,7 +77,7 @@ To use Dipstick in your project, add the following line to your `Cargo.toml`
in the `[dependencies]` section: in the `[dependencies]` section:
```toml ```toml
dipstick = "0.7.9" dipstick = "0.7.10"
``` ```
## External features ## External features

View File

@ -12,7 +12,6 @@ extern crate dipstick;
/// -> Prometheus /// -> Prometheus
/// -> Statsd /// -> Statsd
/// -> stdout /// -> stdout
use dipstick::*; use dipstick::*;
use std::time::Duration; use std::time::Duration;

View File

@ -28,20 +28,20 @@ use std::fmt;
/// A function type to transform aggregated scores into publishable statistics. /// A function type to transform aggregated scores into publishable statistics.
pub type Stat = Option<(InputKind, MetricName, MetricValue)>; pub type Stat = Option<(InputKind, MetricName, MetricValue)>;
pub type StatsFn = Fn(InputKind, MetricName, ScoreType) -> Stat + Send + Sync + 'static; pub type StatsFn = dyn Fn(InputKind, MetricName, ScoreType) -> Stat + Send + Sync + 'static;
fn initial_stats() -> &'static StatsFn { fn initial_stats() -> &'static StatsFn {
&stats_summary &stats_summary
} }
fn initial_drain() -> Arc<OutputDyn + Send + Sync> { fn initial_drain() -> Arc<dyn OutputDyn + Send + Sync> {
Arc::new(output_none()) Arc::new(output_none())
} }
lazy_static! { lazy_static! {
static ref DEFAULT_AGGREGATE_STATS: RwLock<Arc<StatsFn>> = static ref DEFAULT_AGGREGATE_STATS: RwLock<Arc<StatsFn>> =
RwLock::new(Arc::new(initial_stats())); RwLock::new(Arc::new(initial_stats()));
static ref DEFAULT_AGGREGATE_OUTPUT: RwLock<Arc<OutputDyn + Send + Sync>> = static ref DEFAULT_AGGREGATE_OUTPUT: RwLock<Arc<dyn OutputDyn + Send + Sync>> =
RwLock::new(initial_drain()); RwLock::new(initial_drain());
} }
@ -58,7 +58,7 @@ struct InnerAtomicBucket {
metrics: BTreeMap<MetricName, Arc<AtomicScores>>, metrics: BTreeMap<MetricName, Arc<AtomicScores>>,
period_start: TimeHandle, period_start: TimeHandle,
stats: Option<Arc<StatsFn>>, stats: Option<Arc<StatsFn>>,
drain: Option<Arc<OutputDyn + Send + Sync + 'static>>, drain: Option<Arc<dyn OutputDyn + Send + Sync + 'static>>,
publish_metadata: bool, publish_metadata: bool,
} }
@ -101,7 +101,7 @@ impl InnerAtomicBucket {
/// Take a snapshot of aggregated values and reset them. /// Take a snapshot of aggregated values and reset them.
/// Compute stats on captured values using assigned or default stats function. /// Compute stats on captured values using assigned or default stats function.
/// Write stats to assigned or default output. /// Write stats to assigned or default output.
fn flush_to(&mut self, target: &OutputScope) -> error::Result<()> { fn flush_to(&mut self, target: &dyn OutputScope) -> error::Result<()> {
let now = TimeHandle::now(); let now = TimeHandle::now();
let duration_seconds = self.period_start.elapsed_us() as f64 / 1_000_000.0; let duration_seconds = self.period_start.elapsed_us() as f64 / 1_000_000.0;
self.period_start = now; self.period_start = now;
@ -246,7 +246,7 @@ impl AtomicBucket {
} }
/// Immediately flush the bucket's metrics to the specified scope and stats. /// Immediately flush the bucket's metrics to the specified scope and stats.
pub fn flush_to(&self, publish_scope: &OutputScope) -> error::Result<()> { pub fn flush_to(&self, publish_scope: &dyn OutputScope) -> error::Result<()> {
let mut inner = write_lock!(self.inner); let mut inner = write_lock!(self.inner);
inner.flush_to(publish_scope) inner.flush_to(publish_scope)
} }

View File

@ -33,7 +33,7 @@ pub trait CachedInput: Input + Send + Sync + 'static + Sized {
#[derive(Clone)] #[derive(Clone)]
pub struct InputCache { pub struct InputCache {
attributes: Attributes, attributes: Attributes,
target: Arc<InputDyn + Send + Sync + 'static>, target: Arc<dyn InputDyn + Send + Sync + 'static>,
cache: Arc<RwLock<lru::LRUCache<MetricName, InputMetric>>>, cache: Arc<RwLock<lru::LRUCache<MetricName, InputMetric>>>,
} }
@ -74,7 +74,7 @@ impl Input for InputCache {
#[derive(Clone)] #[derive(Clone)]
pub struct InputScopeCache { pub struct InputScopeCache {
attributes: Attributes, attributes: Attributes,
target: Arc<InputScope + Send + Sync + 'static>, target: Arc<dyn InputScope + Send + Sync + 'static>,
cache: Arc<RwLock<lru::LRUCache<MetricName, InputMetric>>>, cache: Arc<RwLock<lru::LRUCache<MetricName, InputMetric>>>,
} }

View File

@ -36,7 +36,7 @@ pub trait CachedOutput: Output + Send + Sync + 'static + Sized {
#[derive(Clone)] #[derive(Clone)]
pub struct OutputCache { pub struct OutputCache {
attributes: Attributes, attributes: Attributes,
target: Arc<OutputDyn + Send + Sync + 'static>, target: Arc<dyn OutputDyn + Send + Sync + 'static>,
cache: Arc<RwLock<lru::LRUCache<MetricName, OutputMetric>>>, cache: Arc<RwLock<lru::LRUCache<MetricName, OutputMetric>>>,
} }
@ -77,7 +77,7 @@ impl Output for OutputCache {
#[derive(Clone)] #[derive(Clone)]
pub struct OutputScopeCache { pub struct OutputScopeCache {
attributes: Attributes, attributes: Attributes,
target: Rc<OutputScope + 'static>, target: Rc<dyn OutputScope + 'static>,
cache: Arc<RwLock<lru::LRUCache<MetricName, OutputMetric>>>, cache: Arc<RwLock<lru::LRUCache<MetricName, OutputMetric>>>,
} }

View File

@ -73,7 +73,7 @@ pub type Shared<T> = Arc<RwLock<T>>;
pub struct Listener { pub struct Listener {
listener_id: usize, listener_id: usize,
listener_fn: Arc<Fn(Instant) -> () + Send + Sync + 'static>, listener_fn: Arc<dyn Fn(Instant) -> () + Send + Sync + 'static>,
} }
/// Attributes common to metric components. /// Attributes common to metric components.
@ -139,7 +139,8 @@ pub struct ObserveWhen<'a, T, F> {
static ID_GENERATOR: AtomicUsize = AtomicUsize::new(0); static ID_GENERATOR: AtomicUsize = AtomicUsize::new(0);
pub struct OnFlushCancel(Arc<Fn()>); /// A handle to cancel a flush observer.
pub struct OnFlushCancel(Arc<dyn Fn()>);
impl Cancel for OnFlushCancel { impl Cancel for OnFlushCancel {
fn cancel(&self) { fn cancel(&self) {

View File

@ -2,4 +2,4 @@ use std::error;
use std::result; use std::result;
/// Just put any error in a box. /// Just put any error in a box.
pub type Result<T> = result::Result<T, Box<error::Error + Send + Sync>>; pub type Result<T> = result::Result<T, Box<dyn error::Error + Send + Sync>>;

View File

@ -30,12 +30,12 @@ pub trait Input: Send + Sync + 'static + InputDyn {
/// A function trait that opens a new metric capture scope. /// A function trait that opens a new metric capture scope.
pub trait InputDyn: Send + Sync + 'static { pub trait InputDyn: Send + Sync + 'static {
/// Open a new scope from this output. /// Open a new scope from this output.
fn input_dyn(&self) -> Arc<InputScope + Send + Sync + 'static>; fn input_dyn(&self) -> Arc<dyn InputScope + Send + Sync + 'static>;
} }
/// Blanket impl of dyn input trait /// Blanket impl of dyn input trait
impl<T: Input + Send + Sync + 'static> InputDyn for T { impl<T: Input + Send + Sync + 'static> InputDyn for T {
fn input_dyn(&self) -> Arc<InputScope + Send + Sync + 'static> { fn input_dyn(&self) -> Arc<dyn InputScope + Send + Sync + 'static> {
Arc::new(self.metrics()) Arc::new(self.metrics())
} }
} }
@ -77,7 +77,7 @@ pub trait InputScope: Flush {
#[derive(Clone)] #[derive(Clone)]
pub struct InputMetric { pub struct InputMetric {
identifier: MetricId, identifier: MetricId,
inner: Arc<Fn(MetricValue, Labels) + Send + Sync>, inner: Arc<dyn Fn(MetricValue, Labels) + Send + Sync>,
} }
impl fmt::Debug for InputMetric { impl fmt::Debug for InputMetric {

View File

@ -68,10 +68,10 @@ impl<T: Output + Send + Sync + 'static> Input for T {
/// Wrap an OutputScope to make it Send + Sync, allowing it to travel the world of threads. /// Wrap an OutputScope to make it Send + Sync, allowing it to travel the world of threads.
/// Obviously, it should only still be used from a single thread at a time or dragons may occur. /// Obviously, it should only still be used from a single thread at a time or dragons may occur.
#[derive(Clone)] #[derive(Clone)]
struct LockedOutputScope(Rc<OutputScope + 'static>); struct LockedOutputScope(Rc<dyn OutputScope + 'static>);
impl ops::Deref for LockedOutputScope { impl ops::Deref for LockedOutputScope {
type Target = OutputScope + 'static; type Target = dyn OutputScope + 'static;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
Rc::as_ref(&self.0) Rc::as_ref(&self.0)
} }

View File

@ -15,7 +15,7 @@ pub trait OutputScope: Flush {
/// Output metrics are not thread safe. /// Output metrics are not thread safe.
#[derive(Clone)] #[derive(Clone)]
pub struct OutputMetric { pub struct OutputMetric {
inner: Rc<Fn(MetricValue, Labels)>, inner: Rc<dyn Fn(MetricValue, Labels)>,
} }
impl OutputMetric { impl OutputMetric {
@ -52,12 +52,12 @@ pub trait Output: Send + Sync + 'static + OutputDyn {
/// A function trait that opens a new metric capture scope. /// A function trait that opens a new metric capture scope.
pub trait OutputDyn { pub trait OutputDyn {
/// Open a new scope from this output. /// Open a new scope from this output.
fn output_dyn(&self) -> Rc<OutputScope + 'static>; fn output_dyn(&self) -> Rc<dyn OutputScope + 'static>;
} }
/// Blanket impl of dyn output trait /// Blanket impl of dyn output trait
impl<T: Output + Send + Sync + 'static> OutputDyn for T { impl<T: Output + Send + Sync + 'static> OutputDyn for T {
fn output_dyn(&self) -> Rc<OutputScope + 'static> { fn output_dyn(&self) -> Rc<dyn OutputScope + 'static> {
Rc::new(self.new_scope()) Rc::new(self.new_scope())
} }
} }

View File

@ -69,7 +69,7 @@ impl Default for Proxy {
struct InnerProxy { struct InnerProxy {
// namespaces can target one, many or no metrics // namespaces can target one, many or no metrics
targets: HashMap<NameParts, Arc<InputScope + Send + Sync>>, targets: HashMap<NameParts, Arc<dyn InputScope + Send + Sync>>,
// last part of the namespace is the metric's name // last part of the namespace is the metric's name
metrics: BTreeMap<NameParts, Weak<ProxyMetric>>, metrics: BTreeMap<NameParts, Weak<ProxyMetric>>,
} }
@ -89,7 +89,11 @@ impl InnerProxy {
} }
} }
fn set_target(&mut self, namespace: &NameParts, target_scope: Arc<InputScope + Send + Sync>) { fn set_target(
&mut self,
namespace: &NameParts,
target_scope: Arc<dyn InputScope + Send + Sync>,
) {
self.targets.insert(namespace.clone(), target_scope.clone()); self.targets.insert(namespace.clone(), target_scope.clone());
for (metric_name, metric) in self.metrics.range_mut(namespace.clone()..) { for (metric_name, metric) in self.metrics.range_mut(namespace.clone()..) {
@ -113,7 +117,7 @@ impl InnerProxy {
fn get_effective_target( fn get_effective_target(
&self, &self,
namespace: &NameParts, namespace: &NameParts,
) -> Option<(Arc<InputScope + Send + Sync>, usize)> { ) -> Option<(Arc<dyn InputScope + Send + Sync>, usize)> {
if let Some(target) = self.targets.get(namespace) { if let Some(target) = self.targets.get(namespace) {
return Some((target.clone(), namespace.len())); return Some((target.clone(), namespace.len()));
} }

View File

@ -65,7 +65,7 @@ struct ScheduledTask {
next_time: Instant, next_time: Instant,
period: Duration, period: Duration,
handle: CancelHandle, handle: CancelHandle,
operation: Arc<Fn(Instant) -> () + Send + Sync + 'static>, operation: Arc<dyn Fn(Instant) -> () + Send + Sync + 'static>,
} }
impl Ord for ScheduledTask { impl Ord for ScheduledTask {

View File

@ -8,10 +8,10 @@ use std::sync::Arc;
lazy_static! { lazy_static! {
/// The reference instance identifying an uninitialized metric config. /// The reference instance identifying an uninitialized metric config.
pub static ref VOID_INPUT: Arc<InputDyn + Send + Sync> = Arc::new(Void::new()); pub static ref VOID_INPUT: Arc<dyn InputDyn + Send + Sync> = Arc::new(Void::new());
/// The reference instance identifying an uninitialized metric scope. /// The reference instance identifying an uninitialized metric scope.
pub static ref NO_METRIC_SCOPE: Arc<InputScope + Send + Sync> = VOID_INPUT.input_dyn(); pub static ref NO_METRIC_SCOPE: Arc<dyn InputScope + Send + Sync> = VOID_INPUT.input_dyn();
} }
/// Discard metrics output. /// Discard metrics output.
@ -50,7 +50,7 @@ impl OutputScope for VoidOutput {
} }
impl Flush for VoidOutput { impl Flush for VoidOutput {
fn flush(&self) -> Result<(), Box<Error + Send + Sync>> { fn flush(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(()) Ok(())
} }
} }

View File

@ -12,7 +12,7 @@ use std::sync::Arc;
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct MultiInput { pub struct MultiInput {
attributes: Attributes, attributes: Attributes,
inputs: Vec<Arc<InputDyn + Send + Sync>>, inputs: Vec<Arc<dyn InputDyn + Send + Sync>>,
} }
impl Input for MultiInput { impl Input for MultiInput {
@ -61,7 +61,7 @@ impl WithAttributes for MultiInput {
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct MultiInputScope { pub struct MultiInputScope {
attributes: Attributes, attributes: Attributes,
scopes: Vec<Arc<InputScope + Send + Sync>>, scopes: Vec<Arc<dyn InputScope + Send + Sync>>,
} }
impl MultiInputScope { impl MultiInputScope {

View File

@ -14,7 +14,7 @@ use std::sync::Arc;
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct MultiOutput { pub struct MultiOutput {
attributes: Attributes, attributes: Attributes,
outputs: Vec<Arc<OutputDyn + Send + Sync + 'static>>, outputs: Vec<Arc<dyn OutputDyn + Send + Sync + 'static>>,
} }
impl Output for MultiOutput { impl Output for MultiOutput {
@ -64,7 +64,7 @@ impl WithAttributes for MultiOutput {
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct MultiOutputScope { pub struct MultiOutputScope {
attributes: Attributes, attributes: Attributes,
scopes: Vec<Rc<OutputScope>>, scopes: Vec<Rc<dyn OutputScope>>,
} }
impl MultiOutputScope { impl MultiOutputScope {

View File

@ -45,7 +45,7 @@ impl LineTemplate {
/// Template execution applies commands in turn, writing to the output. /// Template execution applies commands in turn, writing to the output.
pub fn print<L>( pub fn print<L>(
&self, &self,
output: &mut io::Write, output: &mut dyn io::Write,
value: MetricValue, value: MetricValue,
lookup: L, lookup: L,
) -> Result<(), io::Error> ) -> Result<(), io::Error>

View File

@ -22,7 +22,7 @@ use std::io::Write;
#[derive(Clone)] #[derive(Clone)]
pub struct Log { pub struct Log {
attributes: Attributes, attributes: Attributes,
format: Arc<LineFormat>, format: Arc<dyn LineFormat>,
level: log::Level, level: log::Level,
target: Option<String>, target: Option<String>,
} }

View File

@ -77,7 +77,7 @@ impl OutputScope for StatsMapScope {
} }
impl Flush for StatsMapScope { impl Flush for StatsMapScope {
fn flush(&self) -> Result<(), Box<Error + Send + Sync>> { fn flush(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
self.notify_flush_listeners(); self.notify_flush_listeners();
Ok(()) Ok(())
} }

View File

@ -30,7 +30,7 @@ use parking_lot::RwLock;
/// Buffered metrics text output. /// Buffered metrics text output.
pub struct Stream<W: Write + Send + Sync + 'static> { pub struct Stream<W: Write + Send + Sync + 'static> {
attributes: Attributes, attributes: Attributes,
format: Arc<LineFormat + Send + Sync>, format: Arc<dyn LineFormat + Send + Sync>,
inner: Arc<RwLock<W>>, inner: Arc<RwLock<W>>,
} }

View File

@ -95,7 +95,7 @@ fn new_async_channel(length: usize) -> Arc<crossbeam::Sender<InputQueueCmd>> {
#[derive(Clone)] #[derive(Clone)]
pub struct InputQueue { pub struct InputQueue {
attributes: Attributes, attributes: Attributes,
target: Arc<InputDyn + Send + Sync + 'static>, target: Arc<dyn InputDyn + Send + Sync + 'static>,
#[cfg(not(feature = "crossbeam-channel"))] #[cfg(not(feature = "crossbeam-channel"))]
sender: Arc<mpsc::SyncSender<InputQueueCmd>>, sender: Arc<mpsc::SyncSender<InputQueueCmd>>,
#[cfg(feature = "crossbeam-channel")] #[cfg(feature = "crossbeam-channel")]
@ -144,7 +144,7 @@ pub enum InputQueueCmd {
/// Send metric write /// Send metric write
Write(InputMetric, MetricValue, Labels), Write(InputMetric, MetricValue, Labels),
/// Send metric flush /// Send metric flush
Flush(Arc<InputScope + Send + Sync + 'static>), Flush(Arc<dyn InputScope + Send + Sync + 'static>),
} }
/// A metric scope wrapper that sends writes & flushes over a Rust sync channel. /// A metric scope wrapper that sends writes & flushes over a Rust sync channel.
@ -156,7 +156,7 @@ pub struct InputQueueScope {
sender: Arc<mpsc::SyncSender<InputQueueCmd>>, sender: Arc<mpsc::SyncSender<InputQueueCmd>>,
#[cfg(feature = "crossbeam-channel")] #[cfg(feature = "crossbeam-channel")]
sender: Arc<crossbeam::Sender<InputQueueCmd>>, sender: Arc<crossbeam::Sender<InputQueueCmd>>,
target: Arc<InputScope + Send + Sync + 'static>, target: Arc<dyn InputScope + Send + Sync + 'static>,
} }
impl InputQueueScope { impl InputQueueScope {

View File

@ -98,7 +98,7 @@ fn new_async_channel(length: usize) -> Arc<crossbeam::Sender<OutputQueueCmd>> {
#[derive(Clone)] #[derive(Clone)]
pub struct OutputQueue { pub struct OutputQueue {
attributes: Attributes, attributes: Attributes,
target: Arc<OutputDyn + Send + Sync + 'static>, target: Arc<dyn OutputDyn + Send + Sync + 'static>,
#[cfg(not(feature = "crossbeam-channel"))] #[cfg(not(feature = "crossbeam-channel"))]
q_sender: Arc<mpsc::SyncSender<OutputQueueCmd>>, q_sender: Arc<mpsc::SyncSender<OutputQueueCmd>>,
#[cfg(feature = "crossbeam-channel")] #[cfg(feature = "crossbeam-channel")]
@ -203,7 +203,7 @@ impl Flush for OutputQueueScope {
/// Wrap an OutputScope to make it Send + Sync, allowing it to travel the world of threads. /// Wrap an OutputScope to make it Send + Sync, allowing it to travel the world of threads.
/// Obviously, it should only still be used from a single thread or dragons may occur. /// Obviously, it should only still be used from a single thread or dragons may occur.
#[derive(Clone)] #[derive(Clone)]
pub struct UnsafeScope(Rc<OutputScope + 'static>); pub struct UnsafeScope(Rc<dyn OutputScope + 'static>);
/// This is ok because scope will only ever be used by the dispatcher thread. /// This is ok because scope will only ever be used by the dispatcher thread.
unsafe impl Send for UnsafeScope {} unsafe impl Send for UnsafeScope {}
@ -213,13 +213,13 @@ unsafe impl Sync for UnsafeScope {}
impl UnsafeScope { impl UnsafeScope {
/// Wrap a dynamic RawScope to make it Send + Sync. /// Wrap a dynamic RawScope to make it Send + Sync.
pub fn new(scope: Rc<OutputScope + 'static>) -> Self { pub fn new(scope: Rc<dyn OutputScope + 'static>) -> Self {
UnsafeScope(scope) UnsafeScope(scope)
} }
} }
impl ops::Deref for UnsafeScope { impl ops::Deref for UnsafeScope {
type Target = OutputScope + 'static; type Target = dyn OutputScope + 'static;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
Rc::as_ref(&self.0) Rc::as_ref(&self.0)
} }