Skip to content
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
wants to merge 1 commit into from
Closed

WIP demo nicer FFI types #229

wants to merge 1 commit into from

Conversation

Stebalien
Copy link
Member

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.

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.
Comment on lines 101 to 105
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>,
}
Copy link
Member Author

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(),
            },
        }
    }
}

Comment on lines +39 to +40
let ptr = buf.as_mut_ptr();
Box::leak(buf);
Copy link
Contributor

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)?

Copy link
Member Author

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.

Copy link
Contributor

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.

dignifiedquire added a commit that referenced this pull request Mar 21, 2022
@raulk
Copy link
Member

raulk commented May 3, 2022

IUC, this has been superseded by the FFI safety refactor that landed here: #247. Reopen if not.

@raulk raulk closed this May 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants