How to wrap a borrow based library (regex)? #722
Replies: 1 comment
-
The short answer is that it's not possible. Borrowing and lifetimes is a compile-time construct in Rust. Rune as a dynamic language hosted in Rust can't check lifetimes. What I think is more doable would be to re-use the plumbing of regex. If we look at the implementation of impl Regex {
pub fn captures_at<'h>(
&self,
haystack: &'h str,
start: usize,
) -> Option<Captures<'h>> {
let input = Input::new(haystack).span(start..haystack.len());
let mut caps = self.meta.create_captures();
self.meta.search_captures(&input, &mut caps);
if caps.is_match() {
let static_captures_len = self.static_captures_len();
Some(Captures { haystack, caps, static_captures_len })
} else {
None
}
}
} We could quite easily make an implementation that looks like this, where impl Regex {
#[rune::function]
pub fn captures_at(
&self,
haystack: Ref<str>,
start: usize,
) -> Option<Captures<'h>> {
let input = Input::new(&*haystack).span(start..haystack.len());
let mut caps = self.meta.create_captures();
self.meta.search_captures(&input, &mut caps);
if caps.is_match() {
let static_captures_len = self.static_captures_len();
Some(Captures { haystack, caps, static_captures_len })
} else {
None
}
}
}
pub struct Captures {
haystack: Ref<str>,
caps: captures::Captures,
static_captures_len: Option<usize>,
} Essentially what I think would be possible here is to reproduce the high level API on top of |
Beta Was this translation helpful? Give feedback.
-
I'm finding myself having to wrap some functionality of the regex crate in my current project.
One issue I'm running into is that regex is heavily borrow based. E.g.
Regex::captures<'h>(&self, haystack: &'h haystack) -> Captures<'h>
. Not sure how to properly wrap that up for rune other than eagerly evaluating it to aVec<Option<String>>
, though that also means loosing out on additional functionality like named capture groups.This sort of pattern is all over the regex crate, so I would appreciate some general guidance on how to deal with it with a dynamic script engine like rune.
Also: Any interest in (once I figure out what the proper API actually is) to have it contributed back to
rune-modules
?Beta Was this translation helpful? Give feedback.
All reactions