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_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
},
}
}
})
}};
}

View File

@ -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;

View File

@ -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,