When providing a blanket implmentation of T also optionally provide an appropriate `as_boxed` method
- Ngôn ngữ chính
- Rust
- Star
- 26
- Fork
- 3
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
Let's say I have a `Box` that implements `T`, and some function `fn f(...) -> impl T`. `f` can return a `Box`, because `Box` 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`, 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`. The implementation on `T` would do: `return Box::new(self)`, and the implementation on `Box` would do: `return self`. No double boxing. This is what we use in Sequoia [1](https://gitlab.com/sequoia-pgp/sequoia/-/blob/main/buffered-reader/src/lib.rs#L851), [2](https://gitlab.com/sequoia-pgp/sequoia/-/blob/main/buffered-reader/src/lib.rs#L1003) 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.
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Đánh giá
Issue này chưa được đánh giá.