Skip to content

Commit 7b954f8

Browse files
committed
improve ergonomics of the read/write label types
1 parent 8b5a374 commit 7b954f8

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

crates/bevy_ecs/src/event.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,31 @@ impl<E: Event> DerefMut for EventSequence<E> {
183183
}
184184

185185
/// [Label](SystemLabel) for a [`System`](crate::system::System) that reads events of type `E`.
186-
#[derive(SystemLabel)]
187-
#[system_label(ignore_fields)]
188-
pub struct ReadSystem<E: Event>(PhantomData<E>);
186+
pub struct Reads(());
187+
188+
impl Reads {
189+
/// Returns a [`SystemLabel`] for a system that reads events of type `E`.
190+
pub fn from<E: Event>() -> impl SystemLabel {
191+
struct ReadSystem<E>(PhantomData<E>);
192+
193+
impl<E: 'static> SystemLabel for ReadSystem<E> {
194+
fn as_str(&self) -> &'static str {
195+
// FIXME: using `type_name` for equality is kinda sketchy,
196+
// but we won't need it after https://github.com/bevyengine/bevy/pull/5377.
197+
// This *should* be fine for the time being though, as the behavior
198+
// of `type_name` is only subject to change between compiler versions,
199+
// so the only place it might be able to cause issues is with dynamic plugins.
200+
std::any::type_name::<Self>()
201+
}
202+
}
189203

190-
impl<E: Event> Default for ReadSystem<E> {
191-
fn default() -> Self {
192-
Self(PhantomData)
204+
ReadSystem::<E>(PhantomData)
193205
}
194206
}
195207

196208
/// Reads events of type `T` in order and tracks which events have already been read.
197209
#[derive(SystemParam)]
198-
#[system_param(label = ReadSystem::<E>::default())]
210+
#[system_param(label = Reads::from::<E>())]
199211
pub struct EventReader<'w, 's, E: Event> {
200212
reader: Local<'s, ManualEventReader<E>>,
201213
events: Res<'w, Events<E>>,
@@ -263,14 +275,21 @@ impl<'w, 's, E: Event> EventReader<'w, 's, E> {
263275
}
264276
}
265277

266-
/// [Label](SystemLabel) for a [`System`](crate::system::System) that writes events of type `E`.
267-
#[derive(SystemLabel)]
268-
#[system_label(ignore_fields)]
269-
pub struct WriteSystem<E: Event>(PhantomData<E>);
278+
/// [Label](SystemLabel) for a [`System`](crate::system::System) that can write events of type `E`.
279+
pub struct Writes(());
270280

271-
impl<E: Event> Default for WriteSystem<E> {
272-
fn default() -> Self {
273-
Self(PhantomData)
281+
impl Writes {
282+
/// Returns a [`SystemLabel`] for a system that can write events of type `E`.
283+
pub fn to<E: Event>() -> impl SystemLabel {
284+
struct WriteSystem<E>(PhantomData<E>);
285+
286+
impl<E: 'static> SystemLabel for WriteSystem<E> {
287+
fn as_str(&self) -> &'static str {
288+
std::any::type_name::<E>()
289+
}
290+
}
291+
292+
WriteSystem::<E>(PhantomData)
274293
}
275294
}
276295

@@ -318,7 +337,7 @@ impl<E: Event> Default for WriteSystem<E> {
318337
/// ```
319338
/// Note that this is considered *non-idiomatic*, and should only be used when `EventWriter` will not work.
320339
#[derive(SystemParam)]
321-
#[system_param(label = WriteSystem::<E>::default())]
340+
#[system_param(label = Writes::to::<E>())]
322341
pub struct EventWriter<'w, 's, E: Event> {
323342
events: ResMut<'w, Events<E>>,
324343
#[system_param(ignore)]

0 commit comments

Comments
 (0)