-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Deprecate upcasting methods for reflection #21772
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
Open
alice-i-cecile
wants to merge
6
commits into
bevyengine:main
Choose a base branch
from
alice-i-cecile:upcasting-stabilized
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2dc4f8e
Deprecate methods
alice-i-cecile ec892d8
Remove internal uses of deprecated methods
alice-i-cecile f61b100
Add migration guides
alice-i-cecile 5421c15
Deprecate any methods
alice-i-cecile ab72d44
Fix deprecated any methods
alice-i-cecile c24d5c2
Merge branch 'main' into upcasting-stabilized
alice-i-cecile File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -416,25 +416,43 @@ pub trait Reflect: PartialReflect + DynamicTyped + Any { | |
| /// Returns the value as a [`Box<dyn Any>`][core::any::Any]. | ||
| /// | ||
| /// For remote wrapper types, this will return the remote type instead. | ||
| #[deprecated( | ||
| note = "Upcasting coercion is now automatic; this method is no longer necessary. Add type annotations as needed." | ||
| )] | ||
| fn into_any(self: Box<Self>) -> Box<dyn Any>; | ||
|
|
||
| /// Returns the value as a [`&dyn Any`][core::any::Any]. | ||
| /// | ||
| /// For remote wrapper types, this will return the remote type instead. | ||
| #[deprecated( | ||
| note = "Upcasting coercion is now automatic; this method is no longer necessary. Add type annotations as needed." | ||
| )] | ||
| fn as_any(&self) -> &dyn Any; | ||
|
|
||
| /// Returns the value as a [`&mut dyn Any`][core::any::Any]. | ||
| /// | ||
| /// For remote wrapper types, this will return the remote type instead. | ||
| #[deprecated( | ||
| note = "Upcasting coercion is now automatic; this method is no longer necessary. Add type annotations as needed." | ||
| )] | ||
| fn as_any_mut(&mut self) -> &mut dyn Any; | ||
alice-i-cecile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Casts this type to a boxed, fully-reflected value. | ||
| #[deprecated( | ||
| note = "Upcasting coercion is now automatic; this method is no longer necessary. Add type annotations as needed." | ||
| )] | ||
| fn into_reflect(self: Box<Self>) -> Box<dyn Reflect>; | ||
|
|
||
| /// Casts this type to a fully-reflected value. | ||
| #[deprecated( | ||
| note = "Upcasting coercion is now automatic; this method is no longer necessary. Add type annotations as needed." | ||
| )] | ||
| fn as_reflect(&self) -> &dyn Reflect; | ||
|
|
||
| /// Casts this type to a mutable, fully-reflected value. | ||
| #[deprecated( | ||
| note = "Upcasting coercion is now automatic; this method is no longer necessary. Add type annotations as needed." | ||
| )] | ||
| fn as_reflect_mut(&mut self) -> &mut dyn Reflect; | ||
|
|
||
| /// Performs a type-checked assignment of a reflected value to this value. | ||
|
|
@@ -527,7 +545,7 @@ impl dyn Reflect { | |
| /// For remote types, `T` should be the type itself rather than the wrapper type. | ||
| pub fn downcast<T: Any>(self: Box<dyn Reflect>) -> Result<Box<T>, Box<dyn Reflect>> { | ||
| if self.is::<T>() { | ||
| Ok(self.into_any().downcast().unwrap()) | ||
| Ok(self.downcast().unwrap()) | ||
| } else { | ||
| Err(self) | ||
| } | ||
|
|
@@ -556,7 +574,7 @@ impl dyn Reflect { | |
| /// [`FromReflect`]: crate::FromReflect | ||
| #[inline] | ||
| pub fn is<T: Any>(&self) -> bool { | ||
| self.as_any().type_id() == TypeId::of::<T>() | ||
| self.type_id() == TypeId::of::<T>() | ||
| } | ||
|
|
||
| /// Downcasts the value to type `T` by reference. | ||
|
|
@@ -566,7 +584,8 @@ impl dyn Reflect { | |
| /// For remote types, `T` should be the type itself rather than the wrapper type. | ||
| #[inline] | ||
| pub fn downcast_ref<T: Any>(&self) -> Option<&T> { | ||
| self.as_any().downcast_ref::<T>() | ||
| let any = self as &dyn Any; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Splitting this out explicitly is required to disambiguate and avoid infinite recursion. |
||
| any.downcast_ref::<T>() | ||
| } | ||
|
|
||
| /// Downcasts the value to type `T` by mutable reference. | ||
|
|
@@ -576,7 +595,8 @@ impl dyn Reflect { | |
| /// For remote types, `T` should be the type itself rather than the wrapper type. | ||
| #[inline] | ||
| pub fn downcast_mut<T: Any>(&mut self) -> Option<&mut T> { | ||
| self.as_any_mut().downcast_mut::<T>() | ||
| let any = self as &mut dyn Any; | ||
| any.downcast_mut::<T>() | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
release-content/migration-guides/reflection_upcasting_methods.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| title: "Deprecated `Reflect` methods: `into_reflect`, `as_reflect`, and `as_reflect_mut`" | ||
| pull_requests: [21772] | ||
| --- | ||
|
|
||
| The `into_any`, `as_any`, `as_any_mut`, `into_reflect`, `as_reflect`, and `as_reflect_mut` methods on the `Reflect` trait have been deprecated, | ||
| as [trait upcasting was stabilized](https://github.com/rust-lang/rust/issues/65991) in [Rust 1.86](https://doc.rust-lang.org/beta/releases.html#language-3). | ||
|
|
||
| In many cases, these method calls can simply be removed, and the compiler will infer what you meant to do. | ||
|
|
||
| In some cases however, it may need a bit of help. Type annotations (e.g. `let foo: Box<dyn Reflect>`) can be quite useful, | ||
| and if you are trying to pass in a reference to a method, judicious use of `&*` may be required to resolve compiler errors. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These removed docs are for
PartialReflect::*, which are not deprecated in this PR.