Observe effectively unimplementable for out-of-crate types
vorner opened this issue · 3 comments
vorner commented
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
isObserveWhen<Self, _>
. ObserveWhen
has any methods only if thatSelf
isWithAttributes
.WithAttributes
is not exposed outside of the crate.
I'd see two possible solutions:
- Add a
type ObserveWhen
to theObserve
trait and return that. I'll then be able to returnObserveWhen<AtomicBucket, _>
instead ofObserveWhen<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.
fralalonde commented
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
{
...
fralalonde commented
...Then it's fine with me. I'll leave you the PR :)
vorner commented
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.