From 78c8ebf280d12fd4ee4125ca6356d3fd3b387710 Mon Sep 17 00:00:00 2001 From: Lachlan Deakin Date: Mon, 12 Aug 2024 10:45:09 +1000 Subject: [PATCH] Change `zarrs{LastError,ArrayGetMetadataString,ArrayGetAttributesString}` to return non-const pointers --- CHANGELOG.md | 1 + Cargo.toml | 2 +- src/array.rs | 4 ++-- src/lib.rs | 4 +++- zarrs.h | 12 +++++------- 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 01f74bc..468d68c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Bump `cbindgen` to 0.27 + - Change `zarrs{LastError,ArrayGetMetadataString,ArrayGetAttributesString}` to return non-const pointers ### Fixed - Bump MSRV to 1.75 (needed since 0.6.1) diff --git a/Cargo.toml b/Cargo.toml index c56c9e1..e348175 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "zarrs_ffi" description = "FFI bindings for the zarrs crate" -version = "0.8.1" +version = "0.8.2" authors = ["Lachlan Deakin "] edition = "2021" rust-version = "1.75" diff --git a/src/array.rs b/src/array.rs index 6a30380..4010a6d 100644 --- a/src/array.rs +++ b/src/array.rs @@ -474,7 +474,7 @@ pub unsafe extern "C" fn zarrsArrayGetSubsetSize( pub unsafe extern "C" fn zarrsArrayGetMetadataString( array: ZarrsArray, pretty: bool, - pMetadataString: *mut *const c_char, + pMetadataString: *mut *mut c_char, ) -> ZarrsResult { // Validation if array.is_null() { @@ -509,7 +509,7 @@ pub unsafe extern "C" fn zarrsArrayGetMetadataString( pub unsafe extern "C" fn zarrsArrayGetAttributesString( array: ZarrsArray, pretty: bool, - pAttributesString: *mut *const c_char, + pAttributesString: *mut *mut c_char, ) -> ZarrsResult { // Validation if array.is_null() { diff --git a/src/lib.rs b/src/lib.rs index 92c1267..f57abfe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,8 +39,10 @@ pub enum ZarrsResult { static mut LAST_ERROR: Lazy = Lazy::new(|| "".to_string()); /// Get the last error string. +/// +/// The string must be freed with `zarrsFreeString`. #[no_mangle] -pub extern "C" fn zarrsLastError() -> *const c_char { +pub extern "C" fn zarrsLastError() -> *mut c_char { let c_str = CString::new(unsafe { LAST_ERROR.as_str() }).unwrap(); c_str.into_raw() } diff --git a/zarrs.h b/zarrs.h index 985fbf3..50bd3b4 100644 --- a/zarrs.h +++ b/zarrs.h @@ -104,9 +104,7 @@ extern "C" { * # Safety * `array` must be a valid `ZarrsArray` handle. */ -ZarrsResult zarrsArrayGetAttributesString(ZarrsArray array, - bool pretty, - const char **pAttributesString); +ZarrsResult zarrsArrayGetAttributesString(ZarrsArray array, bool pretty, char **pAttributesString); /** * Return the number of chunks in the chunk grid. @@ -241,9 +239,7 @@ ZarrsResult zarrsArrayGetInnerChunkShape(ZarrsArray array, * # Safety * `array` must be a valid `ZarrsArray` handle. */ -ZarrsResult zarrsArrayGetMetadataString(ZarrsArray array, - bool pretty, - const char **pMetadataString); +ZarrsResult zarrsArrayGetMetadataString(ZarrsArray array, bool pretty, char **pMetadataString); /** * Returns the shape of the array. @@ -481,8 +477,10 @@ ZarrsResult zarrsFreeString(char *string); /** * Get the last error string. + * + * The string must be freed with `zarrsFreeString`. */ -const char *zarrsLastError(void); +char *zarrsLastError(void); /** * Create a handle to an existing array (read/write capability).