add ffi module
parent
7e92521e2e
commit
8c2b8edded
|
@ -0,0 +1,61 @@
|
|||
//! Foreign function interface utilities.
|
||||
|
||||
use crate::durandal::err::*;
|
||||
pub use std::{ffi::*, os::raw::*, ptr::{null, null_mut}};
|
||||
|
||||
/// Creates a C string from a literal.
|
||||
#[macro_export]
|
||||
macro_rules! c_str {
|
||||
($s:expr) => {concat!($s, "\0").as_ptr() as *const c_char};
|
||||
}
|
||||
|
||||
impl CStringVec
|
||||
{
|
||||
/// Creates a new empty CStringVec.
|
||||
pub fn new() -> Self
|
||||
{
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Creates a new `CStringVec` from an iterator.
|
||||
pub fn new_from_iter<'a, I: Iterator<Item = &'a str>>(it: I)
|
||||
-> ResultS<Self>
|
||||
{
|
||||
let mut v = Self::new();
|
||||
|
||||
for st in it {
|
||||
v.push(CString::new(st)?);
|
||||
}
|
||||
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
/// Pushes a new `CString`.
|
||||
pub fn push(&mut self, st: CString)
|
||||
{
|
||||
self.cv.push(st.as_c_str().as_ptr());
|
||||
self.sv.push(st);
|
||||
}
|
||||
|
||||
/// Returns the FFI pointer.
|
||||
pub fn as_ptr(&self) -> *const *const c_char
|
||||
{
|
||||
self.cv.as_ptr()
|
||||
}
|
||||
|
||||
/// Returns the FFI pointer mutably.
|
||||
pub fn as_mut_ptr(&mut self) -> *mut *const c_char
|
||||
{
|
||||
self.cv.as_mut_ptr()
|
||||
}
|
||||
}
|
||||
|
||||
/// An owned FFI-compatible string vector.
|
||||
#[derive(Default)]
|
||||
pub struct CStringVec
|
||||
{
|
||||
sv: Vec<CString>,
|
||||
cv: Vec<*const c_char>,
|
||||
}
|
||||
|
||||
// EOF
|
|
@ -1,5 +1,7 @@
|
|||
//! Library for utilities.
|
||||
|
||||
#[macro_use]
|
||||
pub mod ffi;
|
||||
#[macro_use]
|
||||
pub mod err;
|
||||
#[macro_use]
|
||||
|
|
Loading…
Reference in New Issue