Open nagisa opened 4 years ago
If I remember correctly, the Box
lives for a reason that inside register()
the collector need to be put inside a Vec
, which has to be Vec<Box<dyn Collector>>
. Thus, for a &C
interface, a Box
inside register
is still needed.
However I agree that manual boxing is not friendly.
The Box
is needed to store multiple Collectors of different type via dynamic dispatching in the RegistryCore
:
/* do the boxing inside */
While reducing the noise of a register
invocation it would as well hide the heap allocation.
/* does this even need a box??? */
I guess not as unregister
only needs the collector ids.
I would guess register
and unregister
are not in the hot path of most users applications. Thus I would be careful doing any changes to these APIs without benchmarks proving that not dynamically dispatching actually has an impact.
Currently
register
andunregister
methods ofRegistry
takeBox<dyn Collector>
, however in practice this means that most calls of this method will end up looking like this:This feels unsatisfactory in multiple ways, for example I cannot sleep well knowing there’s an unnecessary double indirection in my code base (
Box
toArc
to the actual metric, could instead be justArc<dyn Collector>
). But also the API is not too convenient and fails to hide implementation details. It could instead be like thisand no longer force users to deal with the manual
Box
ing.