Skip to content

Commit

Permalink
Safe Event implementation (#26)
Browse files Browse the repository at this point in the history
* rewrite Event

* fix errors

* send + sync in tests
  • Loading branch information
JieningYu authored Jan 23, 2024
1 parent 80b5350 commit 82198f5
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 168 deletions.
102 changes: 56 additions & 46 deletions core/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::{
any::{type_name, TypeId},
collections::HashMap,
ops::{Deref, DerefMut},
sync::Arc,
};

use bytes::Bytes;
use rimecraft_edcode::Encode;
use rimecraft_event::Event;
use rimecraft_event::{DefaultEvent, Event};
use rimecraft_primitives::{id, Id, SerDeUpdate};
use tracing::{trace_span, warn};

Expand Down Expand Up @@ -196,12 +197,12 @@ impl From<ComponentsBuilder> for Components {
}
}

type CompPEvent = Event<dyn Fn(TypeId, &mut Components)>;
type CompPEvent = DefaultEvent<dyn Fn(TypeId, &mut Components) + Send + Sync>;

static ATTACH_EVENTS: parking_lot::RwLock<CompPEvent> =
parking_lot::RwLock::new(Event::new(|listeners| {
Box::new(move |type_id, components| {
for listener in listeners {
Arc::new(move |type_id, components| {
for listener in &listeners {
listener(type_id, components)
}
})
Expand Down Expand Up @@ -362,34 +363,37 @@ where

let _ = span.enter();

match components
.get_mut::<Component<Event<dyn Fn(&mut HashMap<Id, Bytes>) -> anyhow::Result<()>>>>(
&NET_SEND_ID,
) {
Ok(Component(event)) => event.register(Box::new(move |map| {
let this = unsafe { &*ptr };
match components.get_mut::<Component<BytesPEvent>>(&NET_SEND_ID) {
Ok(Component(event)) => rimecraft_event::register!(
event,
Arc::new(move |map| {
let this = unsafe { &*ptr };

map.insert(this.1.clone(), {
let mut bytes_mut = bytes::BytesMut::new();
this.0.encode(&mut bytes_mut)?;
map.insert(this.1.clone(), {
let mut bytes_mut = bytes::BytesMut::new();
this.0.encode(&mut bytes_mut)?;

bytes_mut.into()
});
bytes_mut.into()
});

Ok(())
})),
Ok(())
})
),
Err(err) => {
warn!("network sending event not found: {err}");
}
}

match components.get_mut::<Component<BytesPEvent>>(&NET_RECV_ID) {
Ok(Component(event)) => event.register(Box::new(move |map| {
let this = unsafe { &mut *ptr };
let mut bytes = map.remove(&this.1).unwrap();

this.0.update(&mut bytes).map_err(From::from)
})),
Ok(Component(event)) => rimecraft_event::register!(
event,
Arc::new(move |map| {
let this = unsafe { &mut *ptr };
let mut bytes = map.remove(&this.1).unwrap();

this.0.update(&mut bytes).map_err(From::from)
})
),
Err(err) => {
warn!("network receiving event not found: {err}");
}
Expand Down Expand Up @@ -478,13 +482,14 @@ where
}
}

type BytesPEvent = Event<dyn Fn(&mut HashMap<Id, Bytes>) -> anyhow::Result<()>>;
type BytesPEvent =
DefaultEvent<dyn Fn(&mut HashMap<Id, Bytes>) -> anyhow::Result<()> + Send + Sync>;

#[inline]
fn net_event_comp() -> Component<BytesPEvent> {
Component(Event::new(|listeners| {
Box::new(move |map| {
for listener in listeners {
Arc::new(move |map| {
for listener in &listeners {
listener(map)?;
}

Expand Down Expand Up @@ -530,29 +535,32 @@ where

let _ = span.enter();

match components.get_mut::<Component<
Event<dyn Fn(&mut HashMap<Id, fastnbt::Value>) -> fastnbt::error::Result<()>>,
>>(&NBT_SAVE_ID)
{
Ok(Component(event)) => event.register(Box::new(move |map| {
let this = unsafe { &*ptr };
map.insert(
this.1.clone(),
this.0.serialize(&mut fastnbt::value::Serializer)?,
);

Ok(())
})),
match components.get_mut::<Component<ValuePEvent>>(&NBT_SAVE_ID) {
Ok(Component(event)) => rimecraft_event::register!(
event,
Arc::new(move |map| {
let this = unsafe { &*ptr };
map.insert(
this.1.clone(),
this.0.serialize(&mut fastnbt::value::Serializer)?,
);

Ok(())
})
),
Err(err) => {
warn!("nbt saving event not found: {err}");
}
}

match components.get_mut::<Component<ValuePEvent>>(&NBT_READ_ID) {
Ok(Component(event)) => event.register(Box::new(move |map| {
let this = unsafe { &mut *ptr };
this.0.update(&map.remove(&this.1).unwrap())
})),
Ok(Component(event)) => rimecraft_event::register!(
event,
Arc::new(move |map| {
let this = unsafe { &mut *ptr };
this.0.update(&map.remove(&this.1).unwrap())
})
),
Err(err) => {
warn!("nbt reading event not found: {err}");
}
Expand Down Expand Up @@ -641,12 +649,14 @@ where
}
}

type ValuePEvent = Event<dyn Fn(&mut HashMap<Id, fastnbt::Value>) -> fastnbt::error::Result<()>>;
type ValuePEvent = DefaultEvent<
dyn Fn(&mut HashMap<Id, fastnbt::Value>) -> fastnbt::error::Result<()> + Send + Sync,
>;

fn nbt_event_comp() -> Component<ValuePEvent> {
Component(Event::new(|listeners| {
Box::new(move |map| {
for listener in listeners {
Arc::new(move |map| {
for listener in &listeners {
listener(map)?;
}

Expand Down
21 changes: 12 additions & 9 deletions core/src/item/event.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use rimecraft_event::Event;
use std::sync::Arc;

use rimecraft_event::{DefaultEvent, Event};

use super::Item;

pub static POST_PROCESS_NBT: Event<dyn Fn(Item, &mut rimecraft_nbt_ext::Compound)> =
Event::new(|listeners| {
Box::new(move |item, nbt| {
for listener in listeners {
listener(item, nbt)
}
})
});
pub static POST_PROCESS_NBT: DefaultEvent<
dyn Fn(Item, &mut rimecraft_nbt_ext::Compound) + Send + Sync,
> = Event::new(|listeners| {
Arc::new(move |item, nbt| {
for listener in &listeners {
listener(item, nbt)
}
})
});
Loading

0 comments on commit 82198f5

Please sign in to comment.