Skip to content

Commit

Permalink
pass a closure instead of an option to HDDlog.run
Browse files Browse the repository at this point in the history
  • Loading branch information
Remy Wang authored and ryzhyk committed Jul 16, 2019
1 parent 2e1e42d commit 5c0f67a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
21 changes: 15 additions & 6 deletions rust/template/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ impl HDDlog {

pub fn run<F>(workers: usize,
do_store: bool,
cb: Option<F>) -> HDDlog
cb: F) -> HDDlog
where F: Callback
{
Self::do_run(workers,
do_store,
cb.map(CallbackUpdateHandler::new),
CallbackUpdateHandler::new(cb),
None)
}

Expand Down Expand Up @@ -245,7 +245,7 @@ impl HDDlog {
impl HDDlog {
fn do_run<UH>(workers: usize,
do_store: bool,
cb: Option<UH>,
cb: UH,
print_err: Option<extern "C" fn(msg: *const raw::c_char)>) -> HDDlog
where UH: UpdateHandler<Value> + Send + 'static
{
Expand All @@ -271,15 +271,17 @@ impl HDDlog {
} else {
None
};
let cb_handler = cb.map(|h| { nhandlers += 1; Box::new(h) as Box<dyn UpdateHandler<Value> + Send> });

let cb_handler = Box::new(cb) as Box<dyn UpdateHandler<Value> + Send>;
nhandlers += 1;

let handler: Box<dyn UpdateHandler<Value>> = if nhandlers == 1 {
Box::new(delta_handler)
} else {
let mut handlers: Vec<Box<dyn UpdateHandler<Value>>> = Vec::new();
handlers.push(Box::new(delta_handler));
if let Some(h) = store_handler { handlers.push(Box::new(h))};
if let Some(h) = cb_handler { handlers.push(h) };
handlers.push(cb_handler);
Box::new(ChainedUpdateHandler::new(handlers))
};
handler
Expand Down Expand Up @@ -489,10 +491,17 @@ pub extern "C" fn ddlog_run(
cb_arg: libc::uintptr_t,
print_err: Option<extern "C" fn(msg: *const raw::c_char)>) -> *const HDDlog
{
extern "C" fn no_op(_arg: libc::uintptr_t,
_table: libc::size_t,
_rec: *const record::Record,
_pol: bool) {};

let callback = if let Some(f) = cb { f } else { no_op };

Arc::into_raw(Arc::new(
HDDlog::do_run(workers as usize,
do_store,
cb.map(|f| ExternCUpdateHandler::new(f, cb_arg)),
ExternCUpdateHandler::new(callback, cb_arg),
print_err)))
}

Expand Down
12 changes: 7 additions & 5 deletions rust/template/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,15 @@ pub fn main() {
panic!("Invalid command line arguments; try -h for help");
}

fn record_upd(table:usize, rec: &Record, pol: bool) {
eprintln!("{} {:?} {}", if pol { "insert" } else { "delete" }, table, *rec);
}
fn no_op(_table:usize, _rec: &Record, _pol: bool) {}
let cb = if args.print { record_upd } else { no_op };

let hddlog = HDDlog::run(args.workers,
args.store,
if args.print {
Some(|table:usize, rec: &Record, pol: bool| eprintln!("{} {:?} {}", if pol { "insert" } else { "delete" }, table, *rec))
} else {
None
});
cb);

let ret = run_interactive(hddlog, args.delta);
exit(ret);
Expand Down

0 comments on commit 5c0f67a

Please sign in to comment.