Minify macros by introducing store-level operation macro

This commit is contained in:
Matthias Beyer 2017-01-21 19:08:11 +01:00
parent d91243d4b4
commit 3a90077777
3 changed files with 60 additions and 66 deletions

View file

@ -88,82 +88,42 @@ impl_verified_object!(RFileLockEntry);
#[macro_export] #[macro_export]
macro_rules! call_on_fle_from_store { macro_rules! call_on_fle_from_store {
($itself:ident ($wrapper:ident) -> $name:ident -> $operation:block) => {{ ($itself:ident ($wrapper:ident) -> $name:ident -> $operation:block) => {{
use cache::RUBY_STORE_CACHE; let handle = $itself.get_data(&*$wrapper);
let store_handle = handle.store_handle();
let arc = RUBY_STORE_CACHE.clone(); call_on_store_by_handle!(store_handle -> store -> {
{ match store.get(handle.fle_handle().clone()) {
let lock = arc.lock(); Ok(Some(mut $name)) => {
match lock { $operation
Ok(mut hm) => { },
let handle = $itself.get_data(&*$wrapper); Ok(None) => {
match hm.get(handle.store_handle()) { VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist");
Some(store) => { NilClass::new().to_any_object()
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()
}
}
}, },
Err(e) => { Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description()); VM::raise(Class::from_existing("RuntimeError"), e.description());
NilClass::new().to_any_object() NilClass::new().to_any_object()
} },
} }
} })
}}; }};
($itself:ident ($wrapper:ident) -> $name:ident -> $operation: block on fail return $ex:expr) => {{ ($itself:ident ($wrapper:ident) -> $name:ident -> $operation: block on fail return $ex:expr) => {{
use cache::RUBY_STORE_CACHE; let handle = $itself.get_data(&*$wrapper);
let store_handle = handle.store_handle();
let arc = RUBY_STORE_CACHE.clone(); call_on_store_by_handle!(store_handle -> store -> {
{ match store.get(handle.fle_handle().clone()) {
let lock = arc.lock(); Ok(Some(mut $name)) => {
match lock { $operation
Ok(mut hm) => { },
let handle = $itself.get_data(&*$wrapper); Ok(None) => {
match hm.get(handle.store_handle()) { VM::raise(Class::from_existing("RuntimeError"), "Obj does not exist");
Some(store) => { $ex
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()
}
}
}, },
Err(e) => { Err(e) => {
VM::raise(Class::from_existing("RuntimeError"), e.description()); VM::raise(Class::from_existing("RuntimeError"), e.description());
NilClass::new().to_any_object() $ex
} },
} }
} })
}}; }};
} }

View file

@ -30,12 +30,12 @@ extern crate libimagrt;
#[macro_use] extern crate libimagutil; #[macro_use] extern crate libimagutil;
#[macro_use] mod util; #[macro_use] mod util;
#[macro_use] pub mod store;
mod cache; mod cache;
pub mod entry; pub mod entry;
pub mod imag; pub mod imag;
pub mod ruby_utils; pub mod ruby_utils;
pub mod store;
pub mod storeid; pub mod storeid;
pub mod toml_utils; pub mod toml_utils;

View file

@ -39,6 +39,40 @@ impl_wrap!(Store, STORE_WRAPPER);
impl_unwrap!(RStore, Store, STORE_WRAPPER); impl_unwrap!(RStore, Store, STORE_WRAPPER);
impl_verified_object!(RStore); 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!( methods!(
RStore, RStore,
itself, itself,