Skip to content

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

Merged
merged 1 commit into from
Mar 4, 2014
Merged

Cleaned up std::any. #12667

merged 1 commit into from
Mar 4, 2014

Conversation

Kimundi
Copy link
Member

@Kimundi Kimundi commented Mar 3, 2014

  • 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)

@eddyb
Copy link
Member

eddyb commented Mar 3, 2014

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these removed?

Copy link
Member Author

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)
}
}
Copy link
Member

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?

Copy link
Member Author

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`
bors added a commit that referenced this pull request Mar 4, 2014
- 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)
~~~
@bors bors closed this Mar 4, 2014
@bors bors merged commit 3158047 into rust-lang:master Mar 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants