Skip to content

Commit

Permalink
Add support for downcasting instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ten0 committed Jul 25, 2024
1 parent 5444b38 commit 034826a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
1 change: 1 addition & 0 deletions diesel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 44,7 @@ bitflags = { version = "2.0.0", optional = true }
r2d2 = { version = ">= 0.8.2, < 0.9.0", optional = true }
itoa = { version = "1.0.0", optional = true }
time = { version = "0.3.9", optional = true, features = ["macros"] }
downcast-rs = "1.2.1"

[dependencies.diesel_derives]
version = "~2.2.0"
Expand Down
15 changes: 9 additions & 6 deletions diesel/src/connection/instrumentation.rs
Original file line number Diff line number Diff line change
@@ -1,5 1,5 @@
use std::fmt::Debug;
use std::fmt::Display;
use downcast_rs::Downcast;
use std::fmt::{Debug, Display};
use std::num::NonZeroU32;
use std::ops::DerefMut;

Expand Down Expand Up @@ -242,10 242,11 @@ impl<'a> InstrumentationEvent<'a> {
/// More complex usages and integrations with frameworks like
/// `tracing` and `log` are supposed to be part of their own
/// crates.
pub trait Instrumentation: Send 'static {
pub trait Instrumentation: Downcast Send 'static {
/// The function that is invoced for each event
fn on_connection_event(&mut self, event: InstrumentationEvent<'_>);
}
downcast_rs::impl_downcast!(Instrumentation);

/// Get an instance of the default [`Instrumentation`]
///
Expand All @@ -266,9 267,11 @@ pub fn get_default_instrumentation() -> Option<Box<dyn Instrumentation>> {
///
/// // a simple logger that prints all events to stdout
/// fn simple_logger() -> Option<Box<dyn Instrumentation>> {
/// // we need the explicit argument type there due
/// // to bugs in rustc
/// Some(Box::new(|event: InstrumentationEvent<'_>| println!("{event:?}")))
/// // we need the explicit argument type there due
/// // to bugs in rustc
/// Some(Box::new(|event: InstrumentationEvent<'_>| {
/// println!("{event:?}")
/// }))
/// }
///
/// set_default_instrumentation(simple_logger);
Expand Down

0 comments on commit 034826a

Please sign in to comment.