Skip to content

Commit

Permalink
relax Sized bounds around change detection types (bevyengine#5917)
Browse files Browse the repository at this point in the history
# Objective

I wanted to run the code
```rust
let reflect_resource: ReflectResource = ...;
let value: Mut<dyn Reflect> = reflect_resource.reflect(world);
value.deref();
// ^ ERROR: deref method doesn't exist because `dyn Reflect` doesnt satisfy `: Sized`.
```

## Solution

Relax `Sized` bounds in all the methods and trait implementations for `Mut` and friends.
  • Loading branch information
jakobhellermann authored and ItsDoot committed Feb 1, 2023
1 parent fd5d7c6 commit 8acfb64
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions crates/bevy_ecs/src/change_detection.rs
Expand Up @@ -46,7 +46,7 @@ pub trait DetectChanges {
/// The type contained within this smart pointer
///
/// For example, for `Res<T>` this would be `T`.
type Inner;
type Inner: ?Sized;

/// Returns `true` if this value was added after the system last ran.
fn is_added(&self) -> bool;
Expand Down Expand Up @@ -90,7 +90,7 @@ pub trait DetectChanges {

macro_rules! change_detection_impl {
($name:ident < $( $generics:tt ),+ >, $target:ty, $($traits:ident)?) => {
impl<$($generics),* $(: $traits)?> DetectChanges for $name<$($generics),*> {
impl<$($generics),* : ?Sized $(+ $traits)?> DetectChanges for $name<$($generics),*> {
type Inner = $target;

#[inline]
Expand Down Expand Up @@ -130,7 +130,7 @@ macro_rules! change_detection_impl {
}
}

impl<$($generics),* $(: $traits)?> Deref for $name<$($generics),*> {
impl<$($generics),*: ?Sized $(+ $traits)?> Deref for $name<$($generics),*> {
type Target = $target;

#[inline]
Expand All @@ -139,7 +139,7 @@ macro_rules! change_detection_impl {
}
}

impl<$($generics),* $(: $traits)?> DerefMut for $name<$($generics),*> {
impl<$($generics),* : ?Sized $(+ $traits)?> DerefMut for $name<$($generics),*> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
self.set_changed();
Expand All @@ -165,7 +165,7 @@ macro_rules! change_detection_impl {

macro_rules! impl_into_inner {
($name:ident < $( $generics:tt ),+ >, $target:ty, $($traits:ident)?) => {
impl<$($generics),* $(: $traits)?> $name<$($generics),*> {
impl<$($generics),* : ?Sized $(+ $traits)?> $name<$($generics),*> {
/// Consume `self` and return a mutable reference to the
/// contained value while marking `self` as "changed".
#[inline]
Expand All @@ -179,12 +179,12 @@ macro_rules! impl_into_inner {

macro_rules! impl_debug {
($name:ident < $( $generics:tt ),+ >, $($traits:ident)?) => {
impl<$($generics),* $(: $traits)?> std::fmt::Debug for $name<$($generics),*>
impl<$($generics),* : ?Sized $(+ $traits)?> std::fmt::Debug for $name<$($generics),*>
where T: std::fmt::Debug
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple(stringify!($name))
.field(self.value)
.field(&self.value)
.finish()
}
}
Expand All @@ -209,7 +209,7 @@ pub(crate) struct Ticks<'a> {
/// Panics when used as a [`SystemParam`](crate::system::SystemParam) if the resource does not exist.
///
/// Use `Option<ResMut<T>>` instead if the resource might not always exist.
pub struct ResMut<'a, T: Resource> {
pub struct ResMut<'a, T: ?Sized + Resource> {
pub(crate) value: &'a mut T,
pub(crate) ticks: Ticks<'a>,
}
Expand Down Expand Up @@ -241,7 +241,7 @@ impl<'a, T: Resource> From<ResMut<'a, T>> for Mut<'a, T> {
/// Panics when used as a `SystemParameter` if the resource does not exist.
///
/// Use `Option<NonSendMut<T>>` instead if the resource might not always exist.
pub struct NonSendMut<'a, T: 'static> {
pub struct NonSendMut<'a, T: ?Sized + 'static> {
pub(crate) value: &'a mut T,
pub(crate) ticks: Ticks<'a>,
}
Expand Down

0 comments on commit 8acfb64

Please sign in to comment.