Skip to content

add bindings for the readline callback interface - #8

Open
tlilyh wants to merge 2 commits into
shaleh:masterfrom
tlilyh:master
Open

add bindings for the readline callback interface#8
tlilyh wants to merge 2 commits into
shaleh:masterfrom
tlilyh:master

Conversation

@tlilyh

@tlilyh tlilyh commented May 8, 2016

Copy link
Copy Markdown

add bindings for rl_callback_handler_install, rl_callback_read_char,
and rl_callback_handler_remove, as well as two private utility
functions and a global store for the non-extern handler.

add bindings for `rl_callback_handler_install`, `rl_callback_read_char`,
and `rl_callback_handler_remove`, as well as two private utility
functions and a global store for the non-extern handler.
Comment thread src/lib.rs
/// panicking.
pub fn rl_callback_handler_install(prompt: &str, lhandler: fn(Option<String>)) {
let cprmt = CString::new(prompt).unwrap().as_ptr();
unsafe { _lhandler = Some(lhandler); }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmmm. Not sure how I feel about this. lhandler is left as a Some value but it won't be useful to the next caller. what if coerced_callback was used as a curried/partial function instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I would like to do in the ideal case, however I don't know how to correctly pass a curried function (=closure) as a pointer to a c function.

@shaleh

shaleh commented May 9, 2016

Copy link
Copy Markdown
Owner

I like the idea of this patch. However, I am not sold on the dangling static lhandler. Let's discuss alternatives.

@tlilyh

tlilyh commented May 10, 2016

Copy link
Copy Markdown
Author

You're right; that's definitely a hack. I've been contemplating this for a bit, but I haven't thought of anything better (yet, hopefully!)

The primary issue is that readline really wants a pointer to a (extern c) function, and I'm not sure how to coerce any kind of closure to that.

@shaleh

shaleh commented May 10, 2016

Copy link
Copy Markdown
Owner

I will take a swing at it but it might be a few days.

@shaleh

shaleh commented May 10, 2016

Copy link
Copy Markdown
Owner

Could you either update example.rs or include a new async-example.rs that demonstrates the use of the new routines? I am a big fan of shipping tests with code. Plus, it gives me something to work with when I try to experiment with patches.

@tlilyh

tlilyh commented May 11, 2016

Copy link
Copy Markdown
Author

Done. It's pretty simple, closely paralleling the other example, and doesn't usefully use the aynchronous property of the callback interface, but it demos how it works.

It may be worth abstracting from the C interface such that rl_callback_read_char() returns an Option directly, but the obvious way to do this still involves nasty bare globals.

@shaleh

shaleh commented May 11, 2016

Copy link
Copy Markdown
Owner

Perfect. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants