-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Cleaned up std::any
.
#12667
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
Cleaned up std::any
.
#12667
Conversation
LGTM. A bench would be nice, but not required. |
@@ -39,12 +40,6 @@ pub enum Void { } | |||
pub trait Any { | |||
/// Get the `TypeId` of `self` | |||
fn get_type_id(&self) -> TypeId; | |||
|
|||
/// Get a void pointer to `self` | |||
fn as_void_ptr(&self) -> *Void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are these removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As explained in the PR description, they existed only to reliably get a pointer to self in a time with managed headers in some owned boxes.
Furthermore, they polluted the global namespace if you use Any
, as every type suddenly gets a as_[mut_]void_ptr()
method for something that should only be an implementatin detail.
Finally, their usage meant un-erasing a type from an Any
trait object needing two virtual calls, instead of just one.
pub unsafe fn repr_of<T>(t: &T) -> TraitObject { | ||
cast::transmute_copy(t) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with the addition of a TraitObject
struct, but this method seems like it may be excessively unsafe. You have no guarantee that T
is a trait object.
Perhaps this should just add the TraitObject
type and you can transmute to that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, on second though that'd be probably better.
- Added `TraitObject` representation to `std::raw`. - Added doc to `std::raw`. - Removed `Any::as_void_ptr()` and `Any::as_mut_void_ptr()` methods as they are uneccessary now after the removal of headers on owned boxes. This reduces the number of virtual calls needed. - Made the `..Ext` implementations work directly with the repr of a trait object. - Removed `Any`-related traits from the prelude. - Added bench for `Any`
- Added `TraitObject` representation to `std::raw`. - Added doc to `std::raw`. - Removed `Any::as_void_ptr()` and `Any::as_mut_void_ptr()` methods as they are uneccessary now after the removal of headers on owned boxes. This reduces the number of virtual calls needed from 2 to 1. - Made the `..Ext` implementations work directly with the repr of a trait object. - Removed `Any`-related traits from the prelude. - Added bench. Bench before/after: ~~~ 7 ns/iter (+/- 0) 4 ns/iter (+/- 0) ~~~
TraitObject
representation tostd::raw
.std::raw
.Any::as_void_ptr()
andAny::as_mut_void_ptr()
methods as they are uneccessary now after the removal of
headers on owned boxes. This reduces the number of virtual calls needed from 2 to 1.
..Ext
implementations work directly with the repr ofa trait object.
Any
-related traits from the prelude.Bench before/after: