Skip to content

Commit

Permalink
fix zig cases
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Oct 8, 2024
1 parent 843d83b commit 6e9b244
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 17 deletions.
6 changes: 3 additions & 3 deletions bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ typedef struct opendal_bytes {
* represents no error has taken placed**. If any error has taken place, the caller should check
* the error code and print the error message.
*
* The error code is represented in opendal_code, which is a enum on different type of errors.
* The error code is represented in opendal_code, which is an enum on different type of errors.
* The error messages is represented in opendal_bytes, which is a non-null terminated byte array.
*
* \note 1. The error message is on heap, so the error needs to be freed by the caller, by calling
Expand Down Expand Up @@ -814,7 +814,7 @@ struct opendal_result_operator_new opendal_operator_new(const char *scheme,
*/
struct opendal_error *opendal_operator_write(const struct opendal_operator *op,
const char *path,
struct opendal_bytes bytes);
const struct opendal_bytes *bytes);

/**
* \brief Blocking read the data from `path`.
Expand Down Expand Up @@ -1395,7 +1395,7 @@ void opendal_reader_free(struct opendal_reader *ptr);
* \brief Write data to the writer.
*/
struct opendal_result_writer_write opendal_writer_write(struct opendal_writer *self,
struct opendal_bytes bytes);
const struct opendal_bytes *bytes);

/**
* \brief Frees the heap memory used by the opendal_writer.
Expand Down
4 changes: 2 additions & 2 deletions bindings/c/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl From<core::ErrorKind> for opendal_code {
/// represents no error has taken placed**. If any error has taken place, the caller should check
/// the error code and print the error message.
///
/// The error code is represented in opendal_code, which is a enum on different type of errors.
/// The error code is represented in opendal_code, which is an enum on different type of errors.
/// The error messages is represented in opendal_bytes, which is a non-null terminated byte array.
///
/// \note 1. The error message is on heap, so the error needs to be freed by the caller, by calling
Expand Down Expand Up @@ -114,7 +114,7 @@ impl opendal_error {
#[no_mangle]
pub unsafe extern "C" fn opendal_error_free(ptr: *mut opendal_error) {
if !ptr.is_null() {
drop(Box::from_raw(ptr))
drop(Box::from_raw(ptr));
}
}
}
2 changes: 1 addition & 1 deletion bindings/c/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ pub unsafe extern "C" fn opendal_operator_new(
pub unsafe extern "C" fn opendal_operator_write(
op: &opendal_operator,
path: *const c_char,
bytes: opendal_bytes,
bytes: &opendal_bytes,
) -> *mut opendal_error {
assert!(!path.is_null());
let path = std::ffi::CStr::from_ptr(path)
Expand Down
10 changes: 5 additions & 5 deletions bindings/c/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ impl opendal_bytes {
/// \brief Frees the heap memory used by the opendal_bytes
#[no_mangle]
pub unsafe extern "C" fn opendal_bytes_free(bs: opendal_bytes) {
if !bs.data.is_null() {
drop(Vec::from_raw_parts(bs.data, bs.len, bs.capacity));
}
drop(bs);
}
}

Expand All @@ -74,12 +72,14 @@ impl Drop for opendal_bytes {
drop(Vec::from_raw_parts(self.data, self.len, self.capacity));
}
self.data = std::ptr::null_mut();
self.len = 0;
self.capacity = 0;
}
}
}

impl From<opendal_bytes> for Buffer {
fn from(v: opendal_bytes) -> Self {
impl From<&opendal_bytes> for Buffer {
fn from(v: &opendal_bytes) -> Self {
let slice = unsafe { std::slice::from_raw_parts(v.data, v.len) };
Buffer::from(bytes::Bytes::copy_from_slice(slice))
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl opendal_writer {
#[no_mangle]
pub unsafe extern "C" fn opendal_writer_write(
&mut self,
bytes: opendal_bytes,
bytes: &opendal_bytes,
) -> opendal_result_writer_write {
let size = bytes.len;
match self.deref_mut().write(bytes) {
Expand Down
10 changes: 5 additions & 5 deletions bindings/zig/test/bdd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ test "Opendal BDD test" {

// When Blocking write path "test" with content "Hello, World!"
const data: opendal.c.opendal_bytes = .{
.data = testkit.content,
.data = @constCast(testkit.content),
.len = std.mem.len(testkit.content),
.capacity = std.mem.len(testkit.content),
};
const result = opendal.c.opendal_operator_write(testkit.p, testkit.path, data);
const result = opendal.c.opendal_operator_write(testkit.p, testkit.path, &data);
try testing.expectEqual(result, null);

// The blocking file "test" should exist
Expand All @@ -85,11 +85,11 @@ test "Opendal BDD test" {
const r: opendal.c.opendal_result_read = opendal.c.opendal_operator_read(testkit.p, testkit.path);
defer opendal.c.opendal_bytes_free(r.data);
try testing.expect(r.@"error" == null);
try testing.expectEqual(std.mem.len(testkit.content), r.data.*.len);
try testing.expectEqual(std.mem.len(testkit.content), r.data.len);

var count: usize = 0;
while (count < r.data.*.len) : (count += 1) {
try testing.expectEqual(testkit.content[count], r.data.*.data[count]);
while (count < r.data.len) : (count += 1) {
try testing.expectEqual(testkit.content[count], r.data.data[count]);
}
}

Expand Down

0 comments on commit 6e9b244

Please sign in to comment.