SHARE
TWEET
Untitled
a guest
Dec 25th, 2018
15
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
- From: belovedeagle <>
- Date: Tue, 25 Dec 2018 06:59:17 +0000
- Subject: [PATCH 1/2] Remove two unnecessary Arc layers.
- MIME-Version: 1.0
- Content-Type: multipart/mixed; boundary="------------QCH+vwZuai4vjjjitHhzi13H"
- This is a multi-part message in MIME format.
- --------------QCH+vwZuai4vjjjitHhzi13H
- Content-Type: text/plain; charset=UTF-8; format=fixed
- Content-Transfer-Encoding: 8bit
- Holy Arc, Batman!
- ---
- src/lib.rs | 36 +++++++++++++++++++++++++-----------
- 1 file changed, 25 insertions(+), 11 deletions(-)
- --------------QCH+vwZuai4vjjjitHhzi13H
- Content-Type: text/x-patch; name="0001-Remove-two-unnecessary-Arc-layers.patch"
- Content-Transfer-Encoding: 8bit
- Content-Disposition: attachment; filename="0001-Remove-two-unnecessary-Arc-layers.patch"
- diff --git a/src/lib.rs b/src/lib.rs
- index 658e955..2d9a128 100644
- --- a/src/lib.rs
- +++ b/src/lib.rs
- @@ -173,7 +173,6 @@ mod id_map {
- use std::sync::atomic::ATOMIC_USIZE_INIT;
- use std::sync::atomic::Ordering as AtomicOrdering;
- use std::sync::Mutex;
- - use std::sync::Arc;
- use super::Event;
- use super::Handlers;
- lazy_static! {
- @@ -187,8 +186,8 @@ mod id_map {
- pub fn get_event_id<T: Event + ?Sized>() -> usize {
- static EVENT_ID_COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
- EVENT_ID_MAP.lock().expect("failed to allocate event id").entry::<EventId<T>>().or_insert_with(|| {
- - let handlers: Arc<Handlers<T>> = Default::default();
- - Arc::make_mut(&mut super::HANDLERS.write().expect("???")).push(handlers);
- + let handlers: Box<Handlers<T>> = Default::default();
- + super::HANDLERS.write().expect("???").push(handlers);
- EventId { id: EVENT_ID_COUNT.fetch_add(1, AtomicOrdering::SeqCst), _t: PhantomData }
- }).id
- }
- @@ -212,7 +211,21 @@ impl<T: Event + ?Sized> Default for Handlers<T> {
- }
- lazy_static! {
- - static ref HANDLERS: RwArcVec<Arc<dyn Any + Send + Sync>> = Default::default();
- + static ref HANDLERS: RwLock<Vec<Box<dyn Any + Send + Sync>>> = Default::default();
- +}
- +
- +/// FIXFIX: Does a thing, need to explain it (although this function isn't pub).
- +///
- +/// # Panics
- +///
- +/// Panics if `id` doesn't match the given `T` (but not if the given `id` doesn't exist).
- +fn with_handlers<T: Event + ?Sized, R, F: FnOnce(&Handlers<T>) -> R>(id: usize, f: F) -> Option<R> {
- + HANDLERS.read().expect("failed to lock for reading").get(id).map(|any| {
- + // normally one doesn't need to use deref explicitly,
- + // but here even the &* pattern won't work due to the inferred type being "Any"
- + let box_contents = std::ops::Deref::deref(any);
- + f(Any::downcast_ref(box_contents).expect("Wrong id"))
- + })
- }
- /// Low-level event handling function, returns the handlers that should be called to post the
- @@ -224,10 +237,11 @@ lazy_static! {
- ///
- /// Panics if `id` doesn't match the given `T` (but not if the given `id` doesn't exist).
- pub fn get_post_targets<T: Event + ?Sized>(bus: &EventBus, _event: &mut T, id: usize) -> Arc<Vec<(i32, Arc<dyn Fn(&mut T) + Send + Sync>)>> {
- - let target: Option<Arc<Vec<_>>> = HANDLERS.read().expect("failed to lock for reading").get(id).map(|arc| Arc::clone(arc))
- - .map(|any| Arc::downcast::<Handlers<T>>(any).expect("Wrong id"))
- - .and_then(|handlers| handlers.0.read().expect("failed to lock for reading").get(bus.id).map(|hooks| Arc::clone(&*hooks.read().expect("failed to lock for reading"))));
- - target.unwrap_or_else(|| Arc::new(Vec::new()))
- + with_handlers(id, |handlers| {
- + handlers.0.read().expect("failed to lock for reading").get(bus.id).map(|hooks| Arc::clone(&*hooks.read().expect("failed to lock for reading")))
- + })
- + .and_then(|x| x)
- + .unwrap_or_else(|| Arc::new(Vec::new()))
- }
- /// Low-level event handling function, registers an event.
- @@ -239,9 +253,9 @@ pub fn get_post_targets<T: Event + ?Sized>(bus: &EventBus, _event: &mut T, id: u
- ///
- /// Panics if `id` doesn't match the given `T`, or if `id` doesn't exist.
- pub fn register<T: Event + ?Sized, F: for<'a> Fn(&'a mut T) + Send + Sync + 'static>(bus: &EventBus, priority: i32, handler: F, id: usize) {
- - HANDLERS.read().expect("failed to lock for reading").get(id).map(|arc| Arc::clone(arc))
- - .map(|any| Arc::downcast::<Handlers<T>>(any).expect("Wrong id"))
- - .map(|handlers| {
- + // FIXFIX: bad formatting to simplify diff
- + let () =
- + with_handlers(id, |handlers| {
- let mut lock = handlers.0.read().expect("failed to lock for reading");
- if lock.len() <= id {
- mem::drop(lock);
- --------------QCH+vwZuai4vjjjitHhzi13H--
- From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
- From: belovedeagle <>
- Date: Tue, 25 Dec 2018 06:59:46 +0000
- Subject: [PATCH 2/2] Fix indenting.
- MIME-Version: 1.0
- Content-Type: multipart/mixed; boundary="------------QCH+vwZuai4vjjjitHhzi13H"
- This is a multi-part message in MIME format.
- --------------QCH+vwZuai4vjjjitHhzi13H
- Content-Type: text/plain; charset=UTF-8; format=fixed
- Content-Transfer-Encoding: 8bit
- ---
- src/lib.rs | 54 ++++++++++++++++++++++++++----------------------------
- 1 file changed, 26 insertions(+), 28 deletions(-)
- --------------QCH+vwZuai4vjjjitHhzi13H
- Content-Type: text/x-patch; name="0002-Fix-indenting.patch"
- Content-Transfer-Encoding: 8bit
- Content-Disposition: attachment; filename="0002-Fix-indenting.patch"
- diff --git a/src/lib.rs b/src/lib.rs
- index 2d9a128..0ca2ef9 100644
- --- a/src/lib.rs
- +++ b/src/lib.rs
- @@ -253,36 +253,34 @@ pub fn get_post_targets<T: Event + ?Sized>(bus: &EventBus, _event: &mut T, id: u
- ///
- /// Panics if `id` doesn't match the given `T`, or if `id` doesn't exist.
- pub fn register<T: Event + ?Sized, F: for<'a> Fn(&'a mut T) + Send + Sync + 'static>(bus: &EventBus, priority: i32, handler: F, id: usize) {
- - // FIXFIX: bad formatting to simplify diff
- - let () =
- - with_handlers(id, |handlers| {
- - let mut lock = handlers.0.read().expect("failed to lock for reading");
- - if lock.len() <= id {
- - mem::drop(lock);
- - let mut wlock = handlers.0.write().expect("failed to lock for writing");
- - if wlock.len() <= id { // another thread may have touched this
- - let vec = Arc::get_mut(&mut wlock).expect("failed to mutate busses");
- - let count = id - vec.len() + 1;
- - vec.reserve_exact(count);
- - for _ in 0..count {
- - vec.push(Default::default());
- - }
- + let () = with_handlers(id, |handlers| {
- + let mut lock = handlers.0.read().expect("failed to lock for reading");
- + if lock.len() <= id {
- + mem::drop(lock);
- + let mut wlock = handlers.0.write().expect("failed to lock for writing");
- + if wlock.len() <= id { // another thread may have touched this
- + let vec = Arc::get_mut(&mut wlock).expect("failed to mutate busses");
- + let count = id - vec.len() + 1;
- + vec.reserve_exact(count);
- + for _ in 0..count {
- + vec.push(Default::default());
- }
- - // there's no way to downgrade a write lock, but as long as we never shrink the
- - // vecs it should be fine.
- - mem::drop(wlock);
- - lock = handlers.0.read().expect("failed to lock for reading");
- }
- - let option = lock.get(bus.id);
- - // last RwLock
- - let hooks = option.expect("failed to make vecs");
- - let hook: (i32, Arc<dyn Fn(&mut T) + Send + Sync + 'static>) = (priority, Arc::new(handler));
- - let mut wlock = hooks.write().expect("failed to lock for writing");
- - let vec = Arc::make_mut(&mut wlock);
- - // would be nice if Result had a .coalesce()
- - let pos = match vec.binary_search_by(|probe| probe.0.cmp(&priority)) { Ok(p) => p, Err(p) => p };
- - vec.insert(pos, hook);
- - }).expect("No such id");
- + // there's no way to downgrade a write lock, but as long as we never shrink the
- + // vecs it should be fine.
- + mem::drop(wlock);
- + lock = handlers.0.read().expect("failed to lock for reading");
- + }
- + let option = lock.get(bus.id);
- + // last RwLock
- + let hooks = option.expect("failed to make vecs");
- + let hook: (i32, Arc<dyn Fn(&mut T) + Send + Sync + 'static>) = (priority, Arc::new(handler));
- + let mut wlock = hooks.write().expect("failed to lock for writing");
- + let vec = Arc::make_mut(&mut wlock);
- + // would be nice if Result had a .coalesce()
- + let pos = match vec.binary_search_by(|probe| probe.0.cmp(&priority)) { Ok(p) => p, Err(p) => p };
- + vec.insert(pos, hook);
- + }).expect("No such id");
- }
- static BUS_ID: AtomicUsize = ATOMIC_USIZE_INIT;
- --------------QCH+vwZuai4vjjjitHhzi13H--
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
