Merged
Conversation
Member
Author
|
@alexcrichton Some thoughts:
|
regex-capi/src/lib.rs
Outdated
| use libc::{c_char, uint32_t}; | ||
| use regex::bytes; | ||
|
|
||
| macro_rules! protect { |
Contributor
There was a problem hiding this comment.
This could theoretically incorporate more of the FFI set-up, e.g.
macro_rules! ffi_fn {
(fn $name: ident($($arg: ident: $arg_ty: ty),*) -> $ret: ty $body: block) => {
#[no_mangle]
pub extern fn $name($($arg: $arg_ty),*) -> $ret { ... }
}
}
ffi_fn! {
fn rust_compile_must(pattern: *const c_char) -> *const Regex { ... }
}
Member
Author
There was a problem hiding this comment.
Nice, thank you. I did this. It'd be cool to just have one ffi block and stuff all the extern fns in it, but I think that makes the macro much more complex.
This commit contains a new sub-crate called `regex-capi` which provides a C library called `rure`. A new `RegexBuilder` type was also added to the Rust API proper, which permits both users of C and Rust to tweak various knobs on a `Regex`. This fixes issue #166. Since it's likely that this API will be used to provide bindings to other languages, I've created bindings to Go as a proof of concept: https://github.com/BurntSushi/rure-go --- to my knowledge, the wrapper has as little overhead as it can. It was in particular important for the C library to not store any pointers provided by the caller, as this can be problematic in languages with managed runtimes and a moving GC. The C API doesn't expose `RegexSet` and a few other convenience functions such as splitting or replacing. That can be future work. Note that the regex-capi crate requires Rust 1.9, since it uses `panic::catch_unwind`. This also includes tests of basic API functionality and a commented example. Both should now run as part of CI.
This was referenced Oct 13, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit contains a new sub-crate called
regex-capiwhich providesa C library called
rure.A new
RegexBuildertype was also added to the Rust API proper, whichpermits both users of C and Rust to tweak various knobs on a
Regex.This fixes issue #166.
Since it's likely that this API will be used to provide bindings to
other languages, I've created bindings to Go as a proof of concept:
https://github.com/BurntSushi/rure-go --- to my knowledge, the wrapper
has as little overhead as it can. It was in particular important for the
C library to not store any pointers provided by the caller, as this can
be problematic in languages with managed runtimes and a moving GC.
The C API doesn't expose
RegexSetand a few other convenience functionssuch as splitting or replacing. That can be future work.
Note that the regex-capi crate requires Rust 1.9, since it uses
panic::catch_unwind.This also includes tests of basic API functionality and a commented
example. Both should now run as part of CI.