Skip to content

add bindings for the readline callback interface#8

Open
tlilyh wants to merge 2 commits intoshaleh:masterfrom
tlilyh:master
Open

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

Conversation

@tlilyh
Copy link

@tlilyh tlilyh commented May 8, 2016

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.
/// 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
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
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
Copy link
Owner

shaleh commented May 9, 2016

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

@tlilyh
Copy link
Author

tlilyh commented May 10, 2016

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
Copy link
Owner

shaleh commented May 10, 2016

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

@shaleh
Copy link
Owner

shaleh commented May 10, 2016

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
Copy link
Author

tlilyh commented May 11, 2016

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
Copy link
Owner

shaleh commented May 11, 2016

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