Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bindings for the readline callback interface #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

anachrome
Copy link

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.

@anachrome
Copy link
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
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.

@anachrome
Copy link
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
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