bindgen strip nix_ prefix

(cherry picked from commit d07ec1990015f5d12b39da40ad0cb1ef0e798642)
This commit is contained in:
Taeer Bar-Yam 2024-05-09 12:29:32 -04:00 committed by Robert Hensing
parent 1edaffcc09
commit c2159c8834
6 changed files with 32 additions and 23 deletions

View file

@ -2,6 +2,14 @@ use bindgen;
use std::env; use std::env;
use std::path::PathBuf; use std::path::PathBuf;
#[derive(Debug)]
struct StripNixPrefix {}
impl bindgen::callbacks::ParseCallbacks for StripNixPrefix {
fn item_name(&self, name: &str) -> Option<String> {
name.strip_prefix("nix_").map(String::from)
}
}
fn main() { fn main() {
// Tell cargo to invalidate the built crate whenever the wrapper changes // Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=include/nix-c-raw.h"); println!("cargo:rerun-if-changed=include/nix-c-raw.h");
@ -14,6 +22,7 @@ fn main() {
// Tell cargo to invalidate the built crate whenever any of the // Tell cargo to invalidate the built crate whenever any of the
// included header files changed. // included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new())) .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
.parse_callbacks(Box::new(StripNixPrefix {}))
// Finish the builder and generate the bindings. // Finish the builder and generate the bindings.
.generate() .generate()
// Unwrap the Result and panic on failure. // Unwrap the Result and panic on failure.

View file

@ -17,7 +17,7 @@ lazy_static! {
} }
let context: Context = Context::new(); let context: Context = Context::new();
unsafe { unsafe {
raw::nix_libexpr_init(context.ptr()); raw::libexpr_init(context.ptr());
} }
context.check_err()?; context.check_err()?;
Ok(()) Ok(())
@ -46,7 +46,7 @@ impl EvalState {
init()?; init()?;
let eval_state = unsafe { let eval_state = unsafe {
raw::nix_state_create( raw::state_create(
context.ptr(), context.ptr(),
/* searchPath */ null_mut(), /* searchPath */ null_mut(),
store.raw_ptr(), store.raw_ptr(),
@ -75,7 +75,7 @@ impl EvalState {
let value = self.new_value_uninitialized(); let value = self.new_value_uninitialized();
unsafe { unsafe {
let ctx_ptr = self.context.ptr(); let ctx_ptr = self.context.ptr();
raw::nix_expr_eval_from_string( raw::expr_eval_from_string(
ctx_ptr, ctx_ptr,
self.raw_ptr(), self.raw_ptr(),
expr_ptr.as_ptr(), expr_ptr.as_ptr(),
@ -89,13 +89,13 @@ impl EvalState {
/** Try turn any Value into a Value that isn't a Thunk. */ /** Try turn any Value into a Value that isn't a Thunk. */
pub fn force(&self, v: &Value) -> Result<()> { pub fn force(&self, v: &Value) -> Result<()> {
unsafe { unsafe {
raw::nix_value_force(self.context.ptr(), self.raw_ptr(), v.raw_ptr()); raw::value_force(self.context.ptr(), self.raw_ptr(), v.raw_ptr());
} }
self.context.check_err() self.context.check_err()
} }
pub fn value_is_thunk(&self, value: &Value) -> bool { pub fn value_is_thunk(&self, value: &Value) -> bool {
let r = unsafe { let r = unsafe {
raw::nix_get_type(self.context.ptr(), value.raw_ptr()) == raw::ValueType_NIX_TYPE_THUNK raw::get_type(self.context.ptr(), value.raw_ptr()) == raw::ValueType_NIX_TYPE_THUNK
}; };
self.context.check_err().unwrap(); self.context.check_err().unwrap();
r r
@ -104,14 +104,14 @@ impl EvalState {
if self.value_is_thunk(value) { if self.value_is_thunk(value) {
self.force(value)?; self.force(value)?;
} }
let r = unsafe { raw::nix_get_type(self.context.ptr(), value.raw_ptr()) }; let r = unsafe { raw::get_type(self.context.ptr(), value.raw_ptr()) };
Ok(ValueType::from_raw(r)) Ok(ValueType::from_raw(r))
} }
/// Not exposed, because the caller must always explicitly handle the context or not accept one at all. /// Not exposed, because the caller must always explicitly handle the context or not accept one at all.
fn get_string(&self, value: &Value) -> Result<String> { fn get_string(&self, value: &Value) -> Result<String> {
let mut raw_buffer: Vec<u8> = Vec::new(); let mut raw_buffer: Vec<u8> = Vec::new();
unsafe { unsafe {
raw::nix_get_string( raw::get_string(
self.context.ptr(), self.context.ptr(),
value.raw_ptr(), value.raw_ptr(),
Some(callback_get_vec_u8), Some(callback_get_vec_u8),
@ -132,14 +132,14 @@ impl EvalState {
} }
fn new_value_uninitialized(&self) -> Value { fn new_value_uninitialized(&self) -> Value {
let value = unsafe { raw::nix_alloc_value(self.context.ptr(), self.raw_ptr()) }; let value = unsafe { raw::alloc_value(self.context.ptr(), self.raw_ptr()) };
Value::new(value) Value::new(value)
} }
} }
pub fn gc_now() { pub fn gc_now() {
unsafe { unsafe {
raw::nix_gc_now(); raw::gc_now();
} }
} }
@ -182,7 +182,7 @@ pub fn gc_register_my_thread() -> Result<()> {
impl Drop for EvalState { impl Drop for EvalState {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
raw::nix_state_free(self.raw_ptr()); raw::state_free(self.raw_ptr());
} }
} }
} }

View file

@ -58,7 +58,7 @@ impl Drop for Value {
fn drop(&mut self) { fn drop(&mut self) {
let context = Context::new(); let context = Context::new();
unsafe { unsafe {
raw::nix_gc_decref(context.ptr(), self.inner.as_ptr()); raw::gc_decref(context.ptr(), self.inner.as_ptr());
} }
// ignore error from context, because drop should not panic // ignore error from context, because drop should not panic
} }
@ -66,7 +66,7 @@ impl Drop for Value {
impl Clone for Value { impl Clone for Value {
fn clone(&self) -> Self { fn clone(&self) -> Self {
let context = Context::new(); let context = Context::new();
unsafe { raw::nix_gc_incref(context.ptr(), self.inner.as_ptr()) }; unsafe { raw::gc_incref(context.ptr(), self.inner.as_ptr()) };
context.check_err().unwrap(); context.check_err().unwrap();
Value { inner: self.inner } Value { inner: self.inner }
} }

View file

@ -12,7 +12,7 @@ lazy_static! {
static ref INIT: Result<()> = { static ref INIT: Result<()> = {
unsafe { unsafe {
let context: Context = Context::new(); let context: Context = Context::new();
raw::nix_libstore_init(context.ptr()); raw::libstore_init(context.ptr());
context.check_err() context.check_err()
} }
}; };
@ -29,7 +29,7 @@ impl StoreRef {
impl Drop for StoreRef { impl Drop for StoreRef {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
raw::nix_store_free(self.inner.as_ptr()); raw::store_free(self.inner.as_ptr());
} }
} }
} }
@ -54,7 +54,7 @@ impl Store {
let uri_ptr = CString::new(url)?; let uri_ptr = CString::new(url)?;
let store = unsafe { let store = unsafe {
raw::nix_store_open( raw::store_open(
context.ptr(), context.ptr(),
uri_ptr.as_ptr(), uri_ptr.as_ptr(),
null_mut::<*mut *const i8>(), null_mut::<*mut *const i8>(),
@ -80,7 +80,7 @@ impl Store {
pub fn get_uri(&self) -> Result<String> { pub fn get_uri(&self) -> Result<String> {
let mut raw_buffer: Vec<u8> = Vec::new(); let mut raw_buffer: Vec<u8> = Vec::new();
unsafe { unsafe {
raw::nix_store_get_uri( raw::store_get_uri(
self.context.ptr(), self.context.ptr(),
self.inner.ptr(), self.inner.ptr(),
Some(callback_get_vec_u8), Some(callback_get_vec_u8),

View file

@ -4,12 +4,12 @@ use std::ptr::null_mut;
use std::ptr::NonNull; use std::ptr::NonNull;
pub struct Context { pub struct Context {
inner: NonNull<raw::nix_c_context>, inner: NonNull<raw::c_context>,
} }
impl Context { impl Context {
pub fn new() -> Self { pub fn new() -> Self {
let ctx = unsafe { raw::nix_c_context_create() }; let ctx = unsafe { raw::c_context_create() };
if ctx.is_null() { if ctx.is_null() {
panic!("nix_c_context_create returned a null pointer"); panic!("nix_c_context_create returned a null pointer");
} }
@ -18,14 +18,14 @@ impl Context {
}; };
ctx ctx
} }
pub fn ptr(&self) -> *mut raw::nix_c_context { pub fn ptr(&self) -> *mut raw::c_context {
self.inner.as_ptr() self.inner.as_ptr()
} }
pub fn check_err(&self) -> Result<()> { pub fn check_err(&self) -> Result<()> {
let err = unsafe { raw::nix_err_code(self.inner.as_ptr()) }; let err = unsafe { raw::err_code(self.inner.as_ptr()) };
if err != raw::NIX_OK.try_into().unwrap() { if err != raw::NIX_OK.try_into().unwrap() {
// msgp is a borrowed pointer, so we don't need to free it // msgp is a borrowed pointer, so we don't need to free it
let msgp = unsafe { raw::nix_err_msg(null_mut(), self.inner.as_ptr(), null_mut()) }; let msgp = unsafe { raw::err_msg(null_mut(), self.inner.as_ptr(), null_mut()) };
// Turn the i8 pointer into a Rust string by copying // Turn the i8 pointer into a Rust string by copying
let msg: &str = unsafe { core::ffi::CStr::from_ptr(msgp).to_str()? }; let msg: &str = unsafe { core::ffi::CStr::from_ptr(msgp).to_str()? };
bail!("{}", msg); bail!("{}", msg);
@ -37,7 +37,7 @@ impl Context {
impl Drop for Context { impl Drop for Context {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
raw::nix_c_context_free(self.inner.as_ptr()); raw::c_context_free(self.inner.as_ptr());
} }
} }
} }

View file

@ -20,7 +20,7 @@ mod tests {
use nix_c_raw as raw; use nix_c_raw as raw;
/// Typecheck the function signature against the generated bindings in nix_c_raw. /// Typecheck the function signature against the generated bindings in nix_c_raw.
static _CALLBACK_GET_VEC_U8: raw::nix_get_string_callback = Some(callback_get_vec_u8); static _CALLBACK_GET_VEC_U8: raw::get_string_callback = Some(callback_get_vec_u8);
#[test] #[test]
fn test_callback_get_vec_u8_empty() { fn test_callback_get_vec_u8_empty() {