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]
name = "dipstick"
version = "0.7.9"
version = "0.7.10"
authors = ["Francis Lalonde <fralalonde@gmail.com>"]
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:
```toml
dipstick = "0.7.9"
dipstick = "0.7.10"
```
## External features

View File

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

View File

@ -28,20 +28,20 @@ use std::fmt;
/// A function type to transform aggregated scores into publishable statistics.
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 {
&stats_summary
}
fn initial_drain() -> Arc<OutputDyn + Send + Sync> {
fn initial_drain() -> Arc<dyn OutputDyn + Send + Sync> {
Arc::new(output_none())
}
lazy_static! {
static ref DEFAULT_AGGREGATE_STATS: RwLock<Arc<StatsFn>> =
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());
}
@ -58,7 +58,7 @@ struct InnerAtomicBucket {
metrics: BTreeMap<MetricName, Arc<AtomicScores>>,
period_start: TimeHandle,
stats: Option<Arc<StatsFn>>,
drain: Option<Arc<OutputDyn + Send + Sync + 'static>>,
drain: Option<Arc<dyn OutputDyn + Send + Sync + 'static>>,
publish_metadata: bool,
}
@ -101,7 +101,7 @@ impl InnerAtomicBucket {
/// Take a snapshot of aggregated values and reset them.
/// Compute stats on captured values using assigned or default stats function.
/// 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 duration_seconds = self.period_start.elapsed_us() as f64 / 1_000_000.0;
self.period_start = now;
@ -246,7 +246,7 @@ impl AtomicBucket {
}
/// 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);
inner.flush_to(publish_scope)
}

View File

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

View File

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

View File

@ -73,7 +73,7 @@ pub type Shared<T> = Arc<RwLock<T>>;
pub struct Listener {
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.
@ -139,7 +139,8 @@ pub struct ObserveWhen<'a, T, F> {
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 {
fn cancel(&self) {

View File

@ -2,4 +2,4 @@ use std::error;
use std::result;
/// 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.
pub trait InputDyn: Send + Sync + 'static {
/// 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
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())
}
}
@ -77,7 +77,7 @@ pub trait InputScope: Flush {
#[derive(Clone)]
pub struct InputMetric {
identifier: MetricId,
inner: Arc<Fn(MetricValue, Labels) + Send + Sync>,
inner: Arc<dyn Fn(MetricValue, Labels) + Send + Sync>,
}
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.
/// Obviously, it should only still be used from a single thread at a time or dragons may occur.
#[derive(Clone)]
struct LockedOutputScope(Rc<OutputScope + 'static>);
struct LockedOutputScope(Rc<dyn OutputScope + 'static>);
impl ops::Deref for LockedOutputScope {
type Target = OutputScope + 'static;
type Target = dyn OutputScope + 'static;
fn deref(&self) -> &Self::Target {
Rc::as_ref(&self.0)
}

View File

@ -15,7 +15,7 @@ pub trait OutputScope: Flush {
/// Output metrics are not thread safe.
#[derive(Clone)]
pub struct OutputMetric {
inner: Rc<Fn(MetricValue, Labels)>,
inner: Rc<dyn Fn(MetricValue, Labels)>,
}
impl OutputMetric {
@ -52,12 +52,12 @@ pub trait Output: Send + Sync + 'static + OutputDyn {
/// A function trait that opens a new metric capture scope.
pub trait OutputDyn {
/// 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
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())
}
}

View File

@ -69,7 +69,7 @@ impl Default for Proxy {
struct InnerProxy {
// 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
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());
for (metric_name, metric) in self.metrics.range_mut(namespace.clone()..) {
@ -113,7 +117,7 @@ impl InnerProxy {
fn get_effective_target(
&self,
namespace: &NameParts,
) -> Option<(Arc<InputScope + Send + Sync>, usize)> {
) -> Option<(Arc<dyn InputScope + Send + Sync>, usize)> {
if let Some(target) = self.targets.get(namespace) {
return Some((target.clone(), namespace.len()));
}

View File

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

View File

@ -8,10 +8,10 @@ use std::sync::Arc;
lazy_static! {
/// 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.
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.
@ -50,7 +50,7 @@ impl OutputScope for VoidOutput {
}
impl Flush for VoidOutput {
fn flush(&self) -> Result<(), Box<Error + Send + Sync>> {
fn flush(&self) -> Result<(), Box<dyn Error + Send + Sync>> {
Ok(())
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -77,7 +77,7 @@ impl OutputScope 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();
Ok(())
}

View File

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

View File

@ -95,7 +95,7 @@ fn new_async_channel(length: usize) -> Arc<crossbeam::Sender<InputQueueCmd>> {
#[derive(Clone)]
pub struct InputQueue {
attributes: Attributes,
target: Arc<InputDyn + Send + Sync + 'static>,
target: Arc<dyn InputDyn + Send + Sync + 'static>,
#[cfg(not(feature = "crossbeam-channel"))]
sender: Arc<mpsc::SyncSender<InputQueueCmd>>,
#[cfg(feature = "crossbeam-channel")]
@ -144,7 +144,7 @@ pub enum InputQueueCmd {
/// Send metric write
Write(InputMetric, MetricValue, Labels),
/// 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.
@ -156,7 +156,7 @@ pub struct InputQueueScope {
sender: Arc<mpsc::SyncSender<InputQueueCmd>>,
#[cfg(feature = "crossbeam-channel")]
sender: Arc<crossbeam::Sender<InputQueueCmd>>,
target: Arc<InputScope + Send + Sync + 'static>,
target: Arc<dyn InputScope + Send + Sync + 'static>,
}
impl InputQueueScope {

View File

@ -98,7 +98,7 @@ fn new_async_channel(length: usize) -> Arc<crossbeam::Sender<OutputQueueCmd>> {
#[derive(Clone)]
pub struct OutputQueue {
attributes: Attributes,
target: Arc<OutputDyn + Send + Sync + 'static>,
target: Arc<dyn OutputDyn + Send + Sync + 'static>,
#[cfg(not(feature = "crossbeam-channel"))]
q_sender: Arc<mpsc::SyncSender<OutputQueueCmd>>,
#[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.
/// Obviously, it should only still be used from a single thread or dragons may occur.
#[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.
unsafe impl Send for UnsafeScope {}
@ -213,13 +213,13 @@ unsafe impl Sync for UnsafeScope {}
impl UnsafeScope {
/// 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)
}
}
impl ops::Deref for UnsafeScope {
type Target = OutputScope + 'static;
type Target = dyn OutputScope + 'static;
fn deref(&self) -> &Self::Target {
Rc::as_ref(&self.0)
}