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

custom attachments #20

Merged
merged 13 commits into from
Nov 12, 2023
Next Next commit
Add mutable string accessor macro.
  • Loading branch information
brandon-reinhart committed Oct 23, 2023
commit b6b4a21ec8b1823b1d2be99c17a58ee5f8c40028
24 changes: 24 additions & 0 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
@@ -462,6 +462,30 @@ macro_rules! c_accessor_string {
};
}

macro_rules! c_accessor_string_mut {
($(#[$($attrss:tt)*])* $rust:ident, $rust_set:ident, $c:ident) => {
$(#[$($attrss)*])*
#[must_use]
pub fn $rust(&self) -> &str {
unsafe {
if !self.c_ptr_ref().$c.is_null() {
crate::c_interface::from_c_str(std::ffi::CStr::from_ptr(self.c_ptr_ref().$c))
} else {
""
}
}
}

$(#[$($attrss)*])*
pub fn $rust_set(&mut self, value: String) {
let c_str = std::ffi::CString::new(value).expect("Null byte found in provided string");
Copy link
Owner

Choose a reason for hiding this comment

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

I realize it's an exceedingly rare case, but I think we should bubble up the error here, wrapped in SpineError::NulError.

unsafe {
self.c_ptr_mut().$c = c_str.into_raw();
}
}
};
}

macro_rules! c_accessor_string_optional {
($(#[$($attrss:tt)*])* $rust:ident, $c:ident) => {
$(#[$($attrss)*])*