Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

fix: switch to using *mut ptrs, to indicate ownership #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions drop-struct-macro-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct FieldNameType {
field_type: proc_macro2::TokenStream,
}

/// The actual code to free the *const pointers
/// The actual code to free the *mut pointers
impl quote::ToTokens for FieldNameType {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let field_type = &self.field_type;
Expand Down Expand Up @@ -72,13 +72,13 @@ pub fn drop_struct_macro_derive(input: TokenStream) -> TokenStream {
// A list of fields that should get dropped
let mut to_be_dropped = Vec::new();

// Only take *const pointers into account (also not *mut)
// Only take *mut pointers into account (also not *mut)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that should then say "also not *const".

match ast.data {
syn::Data::Struct(ref data_struct) => {
if let syn::Fields::Named(ref fields_named) = data_struct.fields {
for field in fields_named.named.iter() {
if let syn::Type::Ptr(ref type_ptr) = field.ty {
if type_ptr.const_token.is_some() {
if type_ptr.mutability.is_some() {
if let syn::Type::Path(ref type_path) = *type_ptr.elem {
let field_name = field.ident.clone().unwrap().to_string();
let field_type = type_path.path.clone().into_token_stream();
Expand Down
6 changes: 3 additions & 3 deletions ffi-toolkit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ pub enum FCPResponseStatus {
/// All FFI responses need to implement this trait in order to be able to use `catch_panic()`
pub trait CodeAndMessage {
/// Set the status code and error message
fn set_error(&mut self, code_and_message: (FCPResponseStatus, *const libc::c_char));
fn set_error(&mut self, code_and_message: (FCPResponseStatus, *mut libc::c_char));
}

/// A simple macro to create implementations for the `CodeAndMessage` trait
///
/// The only requirement is that the response has an `status_code: FCPResponseStatus` and
/// `error_msg: *const libc::c_char` field.
/// `error_msg: *mut libc::c_char` field.
#[macro_export]
macro_rules! code_and_message_impl {
{ $response:ty } => {
impl CodeAndMessage for $response {
fn set_error(&mut self, (code, message): (FCPResponseStatus, *const libc::c_char)) {
fn set_error(&mut self, (code, message): (FCPResponseStatus, *mut libc::c_char)) {
self.status_code = code;
self.error_msg = message;
}
Expand Down
8 changes: 4 additions & 4 deletions ffi-toolkit/tests/catch_panic_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ use ffi_toolkit::{
#[derive(DropStructMacro)]
pub struct BasicResponse {
pub status_code: FCPResponseStatus,
pub error_msg: *const libc::c_char,
pub error_msg: *mut libc::c_char,
pub is_valid: bool,
}

impl Default for BasicResponse {
fn default() -> Self {
BasicResponse {
status_code: FCPResponseStatus::FCPNoError,
error_msg: ptr::null(),
error_msg: ptr::null_mut(),
is_valid: false,
}
}
Expand Down Expand Up @@ -55,7 +55,7 @@ fn does_not_panic() {
let response = fn_does_not_panic();
assert!((*response).is_valid);
assert_eq!((*response).status_code, FCPResponseStatus::FCPNoError);
assert_eq!((*response).error_msg, ptr::null());
assert!((*response).error_msg.is_null());
}
}

Expand All @@ -66,7 +66,7 @@ fn does_not_panic_with_catch_panic_response() {
let response = fn_does_not_panic_with_catch_panic();
assert!((*response).is_valid);
assert_eq!((*response).status_code, FCPResponseStatus::FCPNoError);
assert_eq!((*response).error_msg, ptr::null());
assert!((*response).error_msg.is_null());
}
}

Expand Down