From 0492f88d66265dd887f47ddab1c99561ebeb1904 Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 8 Oct 2024 03:22:46 -0600 Subject: [PATCH] try fix go case Signed-off-by: tison --- bindings/c/include/opendal.h | 2 +- bindings/c/src/types.rs | 6 +++--- bindings/go/error.go | 2 +- bindings/go/reader.go | 9 ++++----- bindings/go/types.go | 8 ++++---- bindings/zig/test/bdd.zig | 8 +++++--- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/bindings/c/include/opendal.h b/bindings/c/include/opendal.h index 882fb8b8e9a..8eeacf7c512 100644 --- a/bindings/c/include/opendal.h +++ b/bindings/c/include/opendal.h @@ -1314,7 +1314,7 @@ struct opendal_capability opendal_operator_info_get_native_capability(const stru /** * \brief Frees the heap memory used by the opendal_bytes */ -void opendal_bytes_free(struct opendal_bytes *bs); +void opendal_bytes_free(struct opendal_bytes *ptr); /** * \brief Construct a heap-allocated opendal_operator_options diff --git a/bindings/c/src/types.rs b/bindings/c/src/types.rs index dcce095706d..cba8f254006 100644 --- a/bindings/c/src/types.rs +++ b/bindings/c/src/types.rs @@ -59,9 +59,9 @@ impl opendal_bytes { /// \brief Frees the heap memory used by the opendal_bytes #[no_mangle] - pub unsafe extern "C" fn opendal_bytes_free(bs: *mut opendal_bytes) { - if !bs.is_null() { - let bs = &mut *bs; + pub unsafe extern "C" fn opendal_bytes_free(ptr: *mut opendal_bytes) { + if !ptr.is_null() { + let bs = &mut *ptr; if !bs.data.is_null() { drop(Vec::from_raw_parts(bs.data, bs.len, bs.capacity)); bs.data = std::ptr::null_mut(); diff --git a/bindings/go/error.go b/bindings/go/error.go index 33ed9b8c28f..37ea26f0707 100644 --- a/bindings/go/error.go +++ b/bindings/go/error.go @@ -77,7 +77,7 @@ func parseError(ctx context.Context, err *opendalError) error { defer free(err) return &Error{ code: ErrorCode(err.code), - message: string(parseBytes(&err.message)), + message: string(parseBytes(err.message)), } } diff --git a/bindings/go/reader.go b/bindings/go/reader.go index c6fbb68238a..ce26ce663ec 100644 --- a/bindings/go/reader.go +++ b/bindings/go/reader.go @@ -68,8 +68,7 @@ func (op *Operator) Read(path string) ([]byte, error) { data := parseBytes(bytes) if len(data) > 0 { free := getFFI[bytesFree](op.ctx, symBytesFree) - free(bytes) - + free(&bytes) } return data, nil } @@ -203,17 +202,17 @@ func (r *Reader) Close() error { const symOperatorRead = "opendal_operator_read" -type operatorRead func(op *opendalOperator, path string) (*opendalBytes, error) +type operatorRead func(op *opendalOperator, path string) (opendalBytes, error) var withOperatorRead = withFFI(ffiOpts{ sym: symOperatorRead, rType: &typeResultRead, aTypes: []*ffi.Type{&ffi.TypePointer, &ffi.TypePointer}, }, func(ctx context.Context, ffiCall func(rValue unsafe.Pointer, aValues ...unsafe.Pointer)) operatorRead { - return func(op *opendalOperator, path string) (*opendalBytes, error) { + return func(op *opendalOperator, path string) (opendalBytes, error) { bytePath, err := unix.BytePtrFromString(path) if err != nil { - return nil, err + return opendalBytes{}, err } var result resultRead ffiCall( diff --git a/bindings/go/types.go b/bindings/go/types.go index 613825af510..3389b0deb1c 100644 --- a/bindings/go/types.go +++ b/bindings/go/types.go @@ -38,7 +38,7 @@ var ( typeResultRead = ffi.Type{ Type: ffi.Struct, Elements: &[]*ffi.Type{ - &ffi.TypePointer, + &typeBytes, &ffi.TypePointer, nil, }[0], @@ -217,7 +217,7 @@ type resultOperatorNew struct { type opendalOperator struct{} type resultRead struct { - data *opendalBytes + data opendalBytes error *opendalError } @@ -300,8 +300,8 @@ func toOpendalBytes(data []byte) opendalBytes { } } -func parseBytes(b *opendalBytes) (data []byte) { - if b == nil || b.len == 0 { +func parseBytes(b opendalBytes) (data []byte) { + if b.len == 0 { return nil } data = make([]byte, b.len) diff --git a/bindings/zig/test/bdd.zig b/bindings/zig/test/bdd.zig index 042710f7db0..8d2f6ce79f6 100644 --- a/bindings/zig/test/bdd.zig +++ b/bindings/zig/test/bdd.zig @@ -57,11 +57,13 @@ test "Opendal BDD test" { var testkit = OpendalBDDTest.init(); defer testkit.deinit(); + const allocator = std.heap.page_allocator; + const dupe_content = try allocator.dupeZ(u8, std.mem.span(testkit.content)); // When Blocking write path "test" with content "Hello, World!" const data: opendal.c.opendal_bytes = .{ - .data = @constCast(testkit.content), - .len = std.mem.len(testkit.content), - .capacity = std.mem.len(testkit.content), + .data = dupe_content.ptr, + .len = dupe_content.len, + .capacity = dupe_content.len, }; const result = opendal.c.opendal_operator_write(testkit.p, testkit.path, &data); try testing.expectEqual(result, null);