-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP demo nicer FFI types #229
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,115 @@ | ||
use std::ptr; | ||
use std::{ops::Deref, ptr}; | ||
|
||
use drop_struct_macro_derive::DropStructMacro; | ||
// `CodeAndMessage` is the trait implemented by `code_and_message_impl | ||
use ffi_toolkit::{code_and_message_impl, free_c_str, CodeAndMessage, FCPResponseStatus}; | ||
|
||
#[repr(C)] | ||
#[derive(DropStructMacro)] | ||
pub struct fil_Array<T: Sized> { | ||
ptr: *mut T, | ||
len: usize, | ||
} | ||
|
||
impl<T> Default for fil_Array<T> { | ||
fn default() -> Self { | ||
Self { | ||
ptr: ptr::null_mut(), | ||
len: 0, | ||
} | ||
} | ||
} | ||
|
||
pub type fil_Bytes = fil_Array<u8>; | ||
|
||
impl<T> From<Vec<T>> for fil_Array<T> { | ||
fn from(buf: Vec<T>) -> Self { | ||
if buf.is_empty() { | ||
return Default::default(); | ||
} | ||
buf.into_boxed_slice().into() | ||
} | ||
} | ||
|
||
impl<T> From<Box<[T]>> for fil_Array<T> { | ||
fn from(buf: Box<[T]>) -> Self { | ||
if buf.is_empty() { | ||
return Default::default(); | ||
} | ||
let len = buf.len(); | ||
let ptr = buf.as_mut_ptr(); | ||
Box::leak(buf); | ||
|
||
Self { ptr, len } | ||
} | ||
} | ||
|
||
impl<T> Drop for fil_Array<T> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
if !self.ptr.is_null() && self.len != 0 { | ||
let _ = Vec::from_raw_parts(self.ptr, self.len, self.len); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T> Deref for fil_Array<T> { | ||
type Target = [T]; | ||
fn deref(&self) -> &[T] { | ||
unsafe { | ||
if self.ptr.is_null() { | ||
std::slice::from_raw_parts(ptr::NonNull::dangling().as_ptr(), 0) | ||
} else { | ||
std::slice::from_raw_parts(self.ptr, self.len) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl From<&str> for fil_Bytes { | ||
fn from(s: &str) -> Self { | ||
if s.is_empty() { | ||
return Default::default(); | ||
} | ||
Box::<str>::from(s).into() | ||
} | ||
} | ||
|
||
impl From<String> for fil_Bytes { | ||
fn from(s: String) -> Self { | ||
if s.is_empty() { | ||
return Default::default(); | ||
} | ||
s.into_boxed_str().into() | ||
} | ||
} | ||
|
||
impl From<Box<str>> for fil_Bytes { | ||
fn from(s: Box<str>) -> Self { | ||
if s.is_empty() { | ||
return Default::default(); | ||
} | ||
let len = s.len(); | ||
let ptr = s.as_mut_ptr(); | ||
Box::leak(s); | ||
|
||
Self { ptr, len } | ||
} | ||
} | ||
|
||
#[repr(C)] | ||
pub struct fil_GpuDeviceResponse { | ||
pub status_code: FCPResponseStatus, | ||
pub error_msg: *const libc::c_char, | ||
pub devices_len: libc::size_t, | ||
pub devices_ptr: *const *const libc::c_char, | ||
pub error_msg: fil_Bytes, | ||
pub devices: fil_Array<fil_Bytes>, | ||
} | ||
Comment on lines
101
to
105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be: #[repr(C)]
struct fil_Result<T: Sized> {
pub status_code: FCPResponseStatus,
pub error_msg: fil_Bytes,
pub value: T,
}
impl<T> From<Result<T, E>> for fil_Result<T>
where
T: Sized + Default,
E: Display,
{
fn from(r: Result<T, E>) -> Self {
match r {
Ok(value) => Self {
status_code: FCPResponseStatus::FCPNoError,
error_msg: Default::default(),
value: value,
},
Err(e) => Self {
status_code: FCPResponseStatus::FCPUnclassifiedError,
error_msg: e.to_string().into(),
value: Default::default(),
},
}
}
} |
||
|
||
impl Default for fil_GpuDeviceResponse { | ||
fn default() -> Self { | ||
Self { | ||
error_msg: ptr::null(), | ||
error_msg: Default::default(), | ||
status_code: FCPResponseStatus::FCPNoError, | ||
devices_len: 0, | ||
devices_ptr: ptr::null(), | ||
devices: Default::default(), | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are those to lines the same as
let ptr = Box::into_raw(buf)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not quite.
Box::into_raw(buf)
will return*const [T]
instead of*const T
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I knew I must have missed something.