Crates.io | easy-ffi-wrapper |
lib.rs | easy-ffi-wrapper |
version | 0.1.1 |
source | src |
created_at | 2023-04-26 06:39:59.623965 |
updated_at | 2023-05-15 14:03:49.966204 |
description | Types and utilities to enable writing C FFI wrappers easily. |
homepage | |
repository | https://github.com/heremaps/easy-ffi |
max_upload_size | |
id | 849092 |
size | 29,339 |
This crate provides utilities for bindings to FFI interfaces
# #[macro_use] extern crate easy_ffi_wrapper;
mod bindings {
# use std::os::raw::c_char;
pub enum DataHandle {}
pub enum DataItem {}
extern {
pub fn data_create() -> *mut DataHandle;
pub fn data_release(_: *mut DataHandle);
pub fn data_clone(_: *const DataHandle) -> *mut DataHandle;
pub fn data_numbers(_: *const DataHandle, start: *mut *const u32, size: *mut usize);
pub fn data_name(_: *const DataHandle, start: *mut *const c_char, size: *mut usize);
pub fn data_item_len(_: *const DataHandle) -> usize;
pub fn data_item_get(_: *const DataHandle, pos: usize) -> *const DataItem;
}
}
use bindings::DataHandle as Data;
use bindings::DataItem as DataItem;
easy_ffi_wrapper::ffi_box!(
Data,
BoxedData,
delete(bindings::data_release),
new(bindings::data_create),
clone(bindings::data_clone)
);
easy_ffi_wrapper::ffi_iter!(
ItemIter(Data) -> DataItem,
len(bindings::data_item_len),
get(bindings::data_item_get)
);
impl Data {
pub fn name(&self) -> easy_ffi_wrapper::Str {
let mut value = easy_ffi_wrapper::StrBuilder::new();
unsafe {
bindings::data_name(self, &mut value.ptr, &mut value.len);
value.build()
}
}
pub fn numbers(&self) -> &[u32] {
unsafe {
let (mut ptr, mut len) = (std::ptr::null(), 0);
bindings::data_numbers(self, &mut ptr, &mut len);
easy_ffi_wrapper::slice_from_raw_parts(ptr, len)
}
}
pub fn items(&self) -> ItemIter
{
unsafe{ ItemIter::new(self) }
}
}