From 3a90077777ee050e4b4ce96673d0e706dfa0e96d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 21 Jan 2017 19:08:11 +0100 Subject: [PATCH] Minify macros by introducing store-level operation macro --- libimagruby/src/entry.rs | 90 +++++++++++----------------------------- libimagruby/src/lib.rs | 2 +- libimagruby/src/store.rs | 34 +++++++++++++++ 3 files changed, 60 insertions(+), 66 deletions(-) diff --git a/libimagruby/src/entry.rs b/libimagruby/src/entry.rs index d7a255a4..a2ecfbf1 100644 --- a/libimagruby/src/entry.rs +++ b/libimagruby/src/entry.rs @@ -88,82 +88,42 @@ impl_verified_object!(RFileLockEntry); #[macro_export] macro_rules! call_on_fle_from_store { ($itself:ident ($wrapper:ident) -> $name:ident -> $operation:block) => {{ - use cache::RUBY_STORE_CACHE; - - let arc = RUBY_STORE_CACHE.clone(); - { - let lock = arc.lock(); - match lock { - Ok(mut hm) => { - let handle = $itself.get_data(&*$wrapper); - match hm.get(handle.store_handle()) { - Some(store) => { - match store.get(handle.fle_handle().clone()) { - Ok(Some(mut $name)) => { - $operation - }, - Ok(None) => { - VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist"); - NilClass::new().to_any_object() - }, - Err(e) => { - VM::raise(Class::from_existing("RuntimeError"), e.description()); - NilClass::new().to_any_object() - }, - } - }, - None => { - VM::raise(Class::from_existing("RuntimeError"), - "Tried to operate on non-existing object"); - NilClass::new().to_any_object() - } - } + let handle = $itself.get_data(&*$wrapper); + let store_handle = handle.store_handle(); + call_on_store_by_handle!(store_handle -> store -> { + match store.get(handle.fle_handle().clone()) { + Ok(Some(mut $name)) => { + $operation + }, + Ok(None) => { + VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist"); + NilClass::new().to_any_object() }, Err(e) => { VM::raise(Class::from_existing("RuntimeError"), e.description()); NilClass::new().to_any_object() - } + }, } - } + }) }}; ($itself:ident ($wrapper:ident) -> $name:ident -> $operation: block on fail return $ex:expr) => {{ - use cache::RUBY_STORE_CACHE; - - let arc = RUBY_STORE_CACHE.clone(); - { - let lock = arc.lock(); - match lock { - Ok(mut hm) => { - let handle = $itself.get_data(&*$wrapper); - match hm.get(handle.store_handle()) { - Some(store) => { - match store.get(handle.fle_handle().clone()) { - Ok(Some(mut $name)) => { - $operation - }, - Ok(None) => { - VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist"); - $ex - }, - Err(e) => { - VM::raise(Class::from_existing("RuntimeError"), e.description()); - $ex - }, - } - }, - None => { - VM::raise(Class::from_existing("RuntimeError"), - "Tried to operate on non-existing object"); - NilClass::new().to_any_object() - } - } + let handle = $itself.get_data(&*$wrapper); + let store_handle = handle.store_handle(); + call_on_store_by_handle!(store_handle -> store -> { + match store.get(handle.fle_handle().clone()) { + Ok(Some(mut $name)) => { + $operation + }, + Ok(None) => { + VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist"); + $ex }, Err(e) => { VM::raise(Class::from_existing("RuntimeError"), e.description()); - NilClass::new().to_any_object() - } + $ex + }, } - } + }) }}; } diff --git a/libimagruby/src/lib.rs b/libimagruby/src/lib.rs index cb9c496b..7ff0a287 100644 --- a/libimagruby/src/lib.rs +++ b/libimagruby/src/lib.rs @@ -30,12 +30,12 @@ extern crate libimagrt; #[macro_use] extern crate libimagutil; #[macro_use] mod util; +#[macro_use] pub mod store; mod cache; pub mod entry; pub mod imag; pub mod ruby_utils; -pub mod store; pub mod storeid; pub mod toml_utils; diff --git a/libimagruby/src/store.rs b/libimagruby/src/store.rs index 0eb3a66f..c9463d7f 100644 --- a/libimagruby/src/store.rs +++ b/libimagruby/src/store.rs @@ -39,6 +39,40 @@ impl_wrap!(Store, STORE_WRAPPER); impl_unwrap!(RStore, Store, STORE_WRAPPER); impl_verified_object!(RStore); +macro_rules! call_on_store_by_handle { + ($store_handle:ident -> $name:ident -> $operation:block) => {{ + use cache::RUBY_STORE_CACHE; + + let arc = RUBY_STORE_CACHE.clone(); + { + let lock = arc.lock(); + match lock { + Ok(mut hm) => { + match hm.get($store_handle) { + Some($name) => { $operation }, + None => { + VM::raise(Class::from_existing("RuntimeError"), + "Tried to operate on non-existing object"); + NilClass::new().to_any_object() + } + } + }, + Err(e) => { + VM::raise(Class::from_existing("RuntimeError"), e.description()); + NilClass::new().to_any_object() + } + } + } + }} +} + +macro_rules! call_on_store { + ($itself:ident ($wrapper:ident) -> $name:ident -> $operation:block) => {{ + let handle = $itself.get_data(&*$wrapper).store_handle(); + call_on_store_by_handle!(handle -> $name -> $operation) + }}; +} + methods!( RStore, itself,