Skip to content

Commit

Permalink
fix(bindings/c): use BlockWriter instead of StdWriter
Browse files Browse the repository at this point in the history
Signed-off-by: Hanchin Hsieh <[email protected]>
  • Loading branch information
yuchanns committed Sep 25, 2024
1 parent 8635a79 commit 1ab7ae1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
20 changes: 7 additions & 13 deletions bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ typedef struct BlockingLister BlockingLister;
*/
typedef struct BlockingOperator BlockingOperator;

/**
* BlockingWriter is designed to write data into given path in an blocking
* manner.
*/
typedef struct BlockingWriter BlockingWriter;

/**
* Entry returned by [`Lister`] or [`BlockingLister`] to represent a path and it's relative metadata.
*
Expand Down Expand Up @@ -216,18 +222,6 @@ typedef struct OperatorInfo OperatorInfo;
*/
typedef struct StdReader StdReader;

/**
* StdWriter is the adapter of [`std::io::Write`] for [`BlockingWriter`].
*
* Users can use this adapter in cases where they need to use [`std::io::Write`] related trait.
*
* # Notes
*
* Files are automatically closed when they go out of scope. Errors detected on closing are ignored
* by the implementation of Drop. Use the method `close` if these errors must be manually handled.
*/
typedef struct StdWriter StdWriter;

/**
* \brief opendal_bytes carries raw-bytes with its length
*
Expand Down Expand Up @@ -448,7 +442,7 @@ typedef struct opendal_result_operator_reader {
* a opendal::BlockingWriter, which is inside the Rust core code.
*/
typedef struct opendal_writer {
struct StdWriter *inner;
struct BlockingWriter *inner;
} opendal_writer;

/**
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ pub unsafe extern "C" fn opendal_operator_writer(
};

opendal_result_operator_writer {
writer: Box::into_raw(Box::new(opendal_writer::new(writer.into_std_write()))),
writer: Box::into_raw(Box::new(opendal_writer::new(writer))),
error: std::ptr::null_mut(),
}
}
Expand Down
16 changes: 6 additions & 10 deletions bindings/c/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
// specific language governing permissions and limitations
// under the License.

use std::io::Write;

use ::opendal as core;
use opendal::Buffer;

use super::*;

Expand All @@ -27,11 +24,11 @@ use super::*;
/// a opendal::BlockingWriter, which is inside the Rust core code.
#[repr(C)]
pub struct opendal_writer {
inner: *mut core::StdWriter,
inner: *mut core::BlockingWriter,
}

impl opendal_writer {
pub(crate) fn new(writer: core::StdWriter) -> Self {
pub(crate) fn new(writer: core::BlockingWriter) -> Self {
Self {
inner: Box::into_raw(Box::new(writer)),
}
Expand All @@ -44,11 +41,10 @@ impl opendal_writer {
bytes: opendal_bytes,
) -> opendal_result_writer_write {
let inner = unsafe { &mut *(*writer).inner };
let buf: Buffer = bytes.into();
let n = inner.write(&buf.to_vec());
match n {
Ok(n) => opendal_result_writer_write {
size: n,
let size = bytes.len;
match inner.write(bytes) {
Ok(()) => opendal_result_writer_write {
size,
error: std::ptr::null_mut(),
},
Err(e) => opendal_result_writer_write {
Expand Down
3 changes: 1 addition & 2 deletions bindings/go/tests/behavior_tests/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ func testWriterWrite(assert *require.Assertions, op *opendal.Operator, fixture *
}

path := fixture.NewFilePath()
// StdWriter capacity is 256KB
size := uint(256 * 1024)
size := uint(5 * 1024 * 1024)
contentA := genFixedBytes(size)
contentB := genFixedBytes(size)

Expand Down

0 comments on commit 1ab7ae1

Please sign in to comment.