-
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
Conversation
This is a demonstration of nicer FFI types. 1. No more drop macro. 2. No more raw pointers in return types. 3. No more C strings. Future improvements: 1. Add a Default implementation to FCPResponseStatus so we can derive defaults for all responses. 2. Add a generic response type.
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>, | ||
} |
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.
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(),
},
}
}
}
let ptr = buf.as_mut_ptr(); | ||
Box::leak(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.
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.
IUC, this has been superseded by the FFI safety refactor that landed here: #247. Reopen if not. |
This is a demonstration of nicer FFI types.
Future improvements: