Repository: diesel-rs/diesel
File: diesel/src/connection/instrumentation.rs (Rust)

Code before:
            no_instrumentation: NoInstrumentation,
        }
    }

    #[diesel_derives::__diesel_public_if(
        feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
    )]
    pub(crate) fn on_connection_event(&mut self, event: InstrumentationEvent<'_>) {
        if let Some(inner) = self.inner.as_deref_mut() {
            inner.on_connection_event(event)
        }
    }
}
impl<I: Instrumentation> From<I> for DynInstrumentation {
    fn from(instrumentation: I) -> Self {
        Self {
            inner: Some(unpack_instrumentation(Box::new(instrumentation))),
            no_instrumentation: NoInstrumentation,
        }
    }
}

struct NoInstrumentation;
impl Instrumentation for NoInstrumentation {
    fn on_connection_event(&mut self, _: InstrumentationEvent<'_>) {}
}

/// Unwrap unnecessary boxing levels
fn unpack_instrumentation(
    mut instrumentation: Box<dyn Instrumentation>,
) -> Box<dyn Instrumentation> {
    loop {
        match instrumentation.downcast::<Box<dyn Instrumentation>>() {
            Ok(extra_boxed_instrumentation) => instrumentation = *extra_boxed_instrumentation,
            Err(not_extra_boxed_instrumentation) => {
                break not_extra_boxed_instrumentation;
            }
        }
    }
}


Review comment (suggestion):
```suggestion

impl<I: Instrumentation> From<I> for DynInstrumentation {
```

Code after:
    }

    #[diesel_derives::__diesel_public_if(
        feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
    )]
    pub(crate) fn none() -> Self {
        Self {
            inner: None,
            no_instrumentation: NoInstrumentation,
        }
    }

    #[diesel_derives::__diesel_public_if(
        feature = "i-implement-a-third-party-backend-and-opt-into-breaking-changes"
    )]
    pub(crate) fn on_connection_event(&mut self, event: InstrumentationEvent<'_>) {
        // This implementation is not necessary to be able to call this method on this object
        // because of the already existing Deref impl.
        // However it allows avoiding the dynamic dispatch to the stub value
        if let Some(inner) = self.inner.as_deref_mut() {
            inner.on_connection_event(event)
        }
    }
}

impl<I: Instrumentation> From<I> for DynInstrumentation {
    fn from(instrumentation: I) -> Self {
        Self {
            inner: Some(unpack_instrumentation(Box::new(instrumentation))),
            no_instrumentation: NoInstrumentation,
        }
    }
}

struct NoInstrumentation;

impl Instrumentation for NoInstrumentation {
    fn on_connection_event(&mut self, _: InstrumentationEvent<'_>) {}
}

/// Unwrap unnecessary boxing levels
fn unpack_instrumentation(
    mut instrumentation: Box<dyn Instrumentation>,
) -> Box<dyn Instrumentation> {
    loop {
        match instrumentation.downcast::<Box<dyn Instrumentation>>() {
            Ok(extra_boxed_instrumentation) => instrumentation = *extra_boxed_instrumentation,
            Err(not_extra_boxed_instrumentation) => {
                break not_extra_boxed_instrumentation;
            }
        }