-
Notifications
You must be signed in to change notification settings - Fork 46
Open
Labels
enhancementNew feature or requestNew feature or request
Description
What behavior of the library made you think about the improvement?
The library may have an abstraction to expose the guide objects into the cpp space.
How would you like it to behave?
Sample code, based on #210
This code uses slightly different abstraction to the original rust code
use super::ErrorCode;
use crate::{guide::Guide, RegexGuide};
use log;
#[repr(C)]
pub struct RegexGuideHandle {
_private: [u8; 0],
}
/// Destroys a regex guide.
///
/// # Safety
///
/// * The `handle` must be a valid pointer to a regex guide.
#[no_mangle]
pub unsafe extern "C" fn destroy_guide(handle: *mut RegexGuideHandle) {
if !handle.is_null() {
let _ = Box::from_raw(handle as *mut RegexGuide);
}
}
/// Checks if the guide can advance.
///
/// # Safety
///
/// * The `handle` must be a valid pointer to a regex guide.
#[no_mangle]
pub unsafe extern "C" fn guide_can_advance(handle: *const RegexGuideHandle) -> bool {
let guide = &*(handle as *const RegexGuide);
guide.can_advance()
}
/// Checks if the guide is allowed to advance with a given token ID.
///
/// # Safety
///
/// * The `handle` must be a valid pointer to a regex guide.
#[no_mangle]
pub unsafe extern "C" fn guide_is_allowed(handle: *const RegexGuideHandle, token_id: u32) -> bool {
let guide = &*(handle as *const RegexGuide);
guide.is_allowed(&token_id)
}
/// Advances the guide with a given token ID.
///
/// # Safety
///
/// * The `handle` must be a valid pointer to a regex guide.
#[no_mangle]
pub unsafe extern "C" fn guide_advance(
handle: *mut RegexGuideHandle,
token_id: u32,
error_code: *mut ErrorCode,
) {
let guide = &mut *(handle as *mut RegexGuide);
match guide.advance(&token_id) {
Ok(()) => {
*error_code = ErrorCode::Success;
}
Err(e) => {
log::error!("{}", e);
*error_code = e.into();
}
}
}
/// Checks if the guide is finished.
///
/// # Safety
///
/// * The `handle` must be a valid pointer to a regex guide.
#[no_mangle]
pub unsafe extern "C" fn guide_is_finished(handle: *const RegexGuideHandle) -> bool {
let guide = &*(handle as *const RegexGuide);
guide.is_finished()
}
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request