fralalonde/dipstick

Observe effectively unimplementable for out-of-crate types

vorner opened this issue · 3 comments

Hello

I have the spirit-dipstick library, whose purpose is to set up dipstick (its outputs, etc) from configuration files. I wrap the AtomicBucket type into a newtype and delegate to it.

However, it is impossible to do so with the Observe trait, because:

  • The return type of observe is ObserveWhen<Self, _>.
  • ObserveWhen has any methods only if that Self is WithAttributes.
  • WithAttributes is not exposed outside of the crate.

I'd see two possible solutions:

  • Add a type ObserveWhen to the Observe trait and return that. I'll then be able to return ObserveWhen<AtomicBucket, _> instead of ObserveWhen<Self, _>.
  • Make WithAttributes public and therefore implementable by out-of-crate types.

I can send a PR, I'd just like to know if you agree with either of these solutions.

Option A sounds innocuous, apart from maybe breaking the API for some cases. If you mean this:

/// Schedule a recurring task
pub trait Observe<T> {
    fn observe<F>(
        &self,
        metric: impl Deref<Target = InputMetric>,
        operation: F,
    ) -> ObserveWhen<T, F>
    where
        F: Fn(Instant) -> MetricValue + Send + Sync + 'static,
        Self: Sized;
}

impl<T> Observe<T> for T
    where T: InputScope + WithAttributes
{
...

...Then it's fine with me. I'll leave you the PR :)

I mean an associated type, not type parameter. The latter would (probably) make the compiler unable to guess the correct T as there could be more than one for each type. Anyway, PR sent.