Skip to content

Commit

Permalink
Implement MutUntyped::from(mut_typed) (#12406)
Browse files Browse the repository at this point in the history
# Objective

Allow to create MutUntyped<'a> instance from Mut<'a, T>.
Fixes #12405

## Solution

Added impl<'a, T> From<Mut<'a, T>> for MutUntyped<'>
  • Loading branch information
jkb0o committed Mar 10, 2024
1 parent aea9b4a commit f0a9864
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/bevy_ecs/src/change_detection.rs
Expand Up @@ -938,6 +938,15 @@ impl std::fmt::Debug for MutUntyped<'_> {
}
}

impl<'w, T> From<Mut<'w, T>> for MutUntyped<'w> {
fn from(value: Mut<'w, T>) -> Self {
MutUntyped {
value: value.value.into(),
ticks: value.ticks,
}
}
}

#[cfg(test)]
mod tests {
use bevy_ecs_macros::Resource;
Expand Down Expand Up @@ -1254,4 +1263,29 @@ mod tests {

assert!(new.is_changed());
}

#[test]
fn mut_untyped_from_mut() {
let mut component_ticks = ComponentTicks {
added: Tick::new(1),
changed: Tick::new(2),
};
let ticks = TicksMut {
added: &mut component_ticks.added,
changed: &mut component_ticks.changed,
last_run: Tick::new(3),
this_run: Tick::new(4),
};
let mut c = C {};
let mut_typed = Mut {
value: &mut c,
ticks,
};

let into_mut: MutUntyped = mut_typed.into();
assert_eq!(1, into_mut.ticks.added.get());
assert_eq!(2, into_mut.ticks.changed.get());
assert_eq!(3, into_mut.ticks.last_run.get());
assert_eq!(4, into_mut.ticks.this_run.get());
}
}

0 comments on commit f0a9864

Please sign in to comment.