When providing a blanket implmentation of T also optionally provide an appropriate as_boxed
method
#7
Labels
enhancement
New feature or request
as_boxed
method
#7
Let's say I have a
Box<T>
that implementsT
, and some functionfn f(...) -> impl T
.f
can return aBox<T>
, becauseBox<T>
implementsT
. If the caller needs to put the returnt
in a box, then the caller has to do:Box::new(t)
. That's unfortunate, becauset
was actually already aBox<T>
, we just didn't know (or rather, couldn't exploit the fact).A solution is to add a method to
T
:fn as_boxed(self) -> Box<Self>
. The implementation onT
would do:return Box::new(self)
, and the implementation onBox<T>
would do:return self
. No double boxing. This is what we use in Sequoia 1, 2 where we ended up with an object being boxed millions of time.As far as I can tell, blanket wraps all functions. So, we can't use blanket in this case. I'd like an option to either not wrap particular functions, or a mechanism to add an
as_boxed
method. As I can't imagine other functions that shouldn't be wrapped, this point solution is probably sufficient.The text was updated successfully, but these errors were encountered: