Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When providing a blanket implmentation of T also optionally provide an appropriate as_boxed method #7

Open
nwalfield opened this issue Feb 21, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@nwalfield
Copy link

Let's say I have a Box<T> that implements T, and some function fn f(...) -> impl T. f can return a Box<T>, because Box<T> implements T. If the caller needs to put the return t in a box, then the caller has to do: Box::new(t). That's unfortunate, because t was actually already a Box<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 on T would do: return Box::new(self), and the implementation on Box<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.

@althonos althonos added the enhancement New feature or request label Feb 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants