Maraiah/maraiah/ffi.rs

98 lines
2.0 KiB
Rust
Raw Normal View History

2019-03-22 18:54:39 -07:00
//! Foreign function interface utilities.
2019-06-13 18:09:07 -07:00
use crate::err::*;
2019-06-17 22:40:39 -07:00
pub use std::{ffi::*, os::raw::*, ptr::{null, null_mut}};
2019-03-22 18:54:39 -07:00
/// Creates a C string from a literal.
2019-06-17 22:40:39 -07:00
///
/// This is done by concatenating a null byte with the string and converting it
/// to a pointer.
///
/// # Examples
///
/// ```
/// use maraiah::c_str;
///
/// let st = c_str!("test");
///
/// assert!(!st.is_null());
///
/// unsafe {
/// assert_eq!(std::slice::from_raw_parts(st, 5), &[116, 101, 115, 116, 0]);
/// }
/// ```
2019-03-22 18:54:39 -07:00
#[macro_export]
macro_rules! c_str {
2019-04-01 06:05:06 -07:00
($s:expr) => {
2019-06-13 18:09:07 -07:00
concat!($s, "\0").as_ptr() as $crate::ffi::NT
2019-04-01 06:05:06 -07:00
};
2019-03-22 18:54:39 -07:00
}
2019-06-17 22:40:39 -07:00
/// Returns [`null`] as a [`*const c_void`].
///
/// [`null`]: fn.null.html
/// [`*const c_void`]: enum.c_void.html
2019-03-29 11:32:17 -07:00
#[inline]
2019-04-01 06:05:06 -07:00
pub const fn null_void() -> *const c_void {null()}
2019-03-29 11:32:17 -07:00
2019-06-17 22:40:39 -07:00
/// Returns [`null_mut`] as a [`*mut c_void`].
///
/// [`null_mut`]: fn.null_mut.html
/// [`*mut c_void`]: enum.c_void.html
2019-03-29 11:32:17 -07:00
#[inline]
2019-04-01 06:05:06 -07:00
pub const fn null_mut_void() -> *mut c_void {null_mut()}
2019-03-29 11:32:17 -07:00
2019-03-22 18:54:39 -07:00
impl CStringVec
{
/// Creates a new `CStringVec` from an iterator.
2019-03-29 11:32:17 -07:00
#[inline]
2019-04-11 18:38:33 -07:00
pub fn new_from_iter<'a, I>(it: I) -> ResultS<Self>
where I: Iterator<Item = &'a str>
2019-03-22 18:54:39 -07:00
{
2019-04-11 18:38:33 -07:00
let mut v = Self::default();
2019-03-22 18:54:39 -07:00
for st in it {
v.push(CString::new(st)?);
}
Ok(v)
}
/// Pushes a new `CString`.
2019-03-29 11:32:17 -07:00
#[inline]
2019-03-22 18:54:39 -07:00
pub fn push(&mut self, st: CString)
{
2019-03-23 05:33:04 -07:00
self.cv.insert(self.cv.len() - 1, st.as_ptr());
2019-03-22 18:54:39 -07:00
self.sv.push(st);
}
/// Returns the FFI pointer.
2019-03-29 11:32:17 -07:00
#[inline]
2019-04-01 06:05:06 -07:00
pub fn as_ptr(&self) -> *const NT {self.cv.as_ptr()}
2019-03-22 18:54:39 -07:00
/// Returns the FFI pointer mutably.
2019-03-29 11:32:17 -07:00
#[inline]
2019-04-01 06:05:06 -07:00
pub fn as_mut_ptr(&mut self) -> *mut NT {self.cv.as_mut_ptr()}
2019-03-22 18:54:39 -07:00
}
2019-03-27 11:07:33 -07:00
impl Default for CStringVec
{
2019-04-11 18:38:33 -07:00
/// Creates a new empty CStringVec.
2019-03-29 11:32:17 -07:00
#[inline]
2019-04-11 18:38:33 -07:00
fn default() -> Self {Self{sv: Vec::new(), cv: vec![null()]}}
2019-03-27 11:07:33 -07:00
}
2019-03-22 20:05:38 -07:00
/// An owned null-terminated string vector.
#[derive(Debug)]
2019-03-22 18:54:39 -07:00
pub struct CStringVec
{
sv: Vec<CString>,
2019-03-22 20:05:38 -07:00
cv: Vec<NT>,
2019-03-22 18:54:39 -07:00
}
2019-03-22 20:05:38 -07:00
/// A null-terminated byte string pointer.
pub type NT = *const c_char;
2019-03-22 18:54:39 -07:00
// EOF