Skip to content

Commit d74bf7c

Browse files
authored
refactor: more consistent C binding pattern (#5162)
Signed-off-by: tison <[email protected]>
1 parent d3e2f77 commit d74bf7c

File tree

10 files changed

+192
-256
lines changed

10 files changed

+192
-256
lines changed

bindings/c/include/opendal.h

+27-61
Original file line numberDiff line numberDiff line change
@@ -81,60 +81,6 @@ typedef enum opendal_code {
8181
OPENDAL_RANGE_NOT_SATISFIED,
8282
} opendal_code;
8383

84-
/**
85-
* BlockingOperator is the entry for all public blocking APIs.
86-
*
87-
* Read [`concepts`][docs::concepts] for know more about [`Operator`].
88-
*
89-
* # Examples
90-
*
91-
* ## Init backends
92-
*
93-
* Read more backend init examples in [`services`]
94-
*
95-
* ```rust,no_run
96-
* # use anyhow::Result;
97-
* use opendal::services::Fs;
98-
* use opendal::BlockingOperator;
99-
* use opendal::Operator;
100-
*
101-
* fn main() -> Result<()> {
102-
* // Create fs backend builder.
103-
* let builder = Fs::default().root("/tmp");
104-
*
105-
* // Build an `BlockingOperator` to start operating the storage.
106-
* let _: BlockingOperator = Operator::new(builder)?.finish().blocking();
107-
*
108-
* Ok(())
109-
* }
110-
* ```
111-
*
112-
* ## Init backends with blocking layer
113-
*
114-
* Some services like s3, gcs doesn't have native blocking supports, we can use [`layers::BlockingLayer`]
115-
* to wrap the async operator to make it blocking.
116-
* # use anyhow::Result;
117-
* use opendal::layers::BlockingLayer;
118-
* use opendal::services::S3;
119-
* use opendal::BlockingOperator;
120-
* use opendal::Operator;
121-
*
122-
* async fn test() -> Result<()> {
123-
* // Create fs backend builder.
124-
* let mut builder = S3::default().bucket("test").region("us-east-1");
125-
*
126-
* // Build an `BlockingOperator` with blocking layer to start operating the storage.
127-
* let _: BlockingOperator = Operator::new(builder)?
128-
* .layer(BlockingLayer::create()?)
129-
* .finish()
130-
* .blocking();
131-
*
132-
* Ok(())
133-
* }
134-
* ```
135-
*/
136-
typedef struct BlockingOperator BlockingOperator;
137-
13884
/**
13985
* \brief opendal_bytes carries raw-bytes with its length
14086
*
@@ -188,6 +134,10 @@ typedef struct opendal_error {
188134
* @see opendal_list_entry_name()
189135
*/
190136
typedef struct opendal_entry {
137+
/**
138+
* The pointer to the opendal::Entry in the Rust code.
139+
* Only touch this on judging whether it is NULL.
140+
*/
191141
void *inner;
192142
} opendal_entry;
193143

@@ -219,6 +169,10 @@ typedef struct opendal_result_lister_next {
219169
* @see opendal_operator_list()
220170
*/
221171
typedef struct opendal_lister {
172+
/**
173+
* The pointer to the opendal::BlockingLister in the Rust code.
174+
* Only touch this on judging whether it is NULL.
175+
*/
222176
void *inner;
223177
} opendal_lister;
224178

@@ -259,7 +213,7 @@ typedef struct opendal_operator {
259213
* The pointer to the opendal::BlockingOperator in the Rust code.
260214
* Only touch this on judging whether it is NULL.
261215
*/
262-
const struct BlockingOperator *ptr;
216+
const void *inner;
263217
} opendal_operator;
264218

265219
/**
@@ -297,7 +251,7 @@ typedef struct opendal_result_operator_new {
297251
*/
298252
typedef struct opendal_operator_options {
299253
/**
300-
* The pointer to the Rust HashMap<String, String>
254+
* The pointer to the HashMap<String, String> in the Rust code.
301255
* Only touch this on judging whether it is NULL.
302256
*/
303257
void *inner;
@@ -329,6 +283,10 @@ typedef struct opendal_result_read {
329283
* a opendal::BlockingReader, which is inside the Rust core code.
330284
*/
331285
typedef struct opendal_reader {
286+
/**
287+
* The pointer to the opendal::StdReader in the Rust code.
288+
* Only touch this on judging whether it is NULL.
289+
*/
332290
void *inner;
333291
} opendal_reader;
334292

@@ -355,6 +313,10 @@ typedef struct opendal_result_operator_reader {
355313
* an opendal::BlockingWriter, which is inside the Rust core code.
356314
*/
357315
typedef struct opendal_writer {
316+
/**
317+
* The pointer to the opendal::BlockingWriter in the Rust code.
318+
* Only touch this on judging whether it is NULL.
319+
*/
358320
void *inner;
359321
} opendal_writer;
360322

@@ -436,6 +398,10 @@ typedef struct opendal_result_list {
436398
* of operator.
437399
*/
438400
typedef struct opendal_operator_info {
401+
/**
402+
* The pointer to the opendal::OperatorInfo in the Rust code.
403+
* Only touch this on judging whether it is NULL.
404+
*/
439405
void *inner;
440406
} opendal_operator_info;
441407

@@ -658,7 +624,7 @@ void opendal_error_free(struct opendal_error *ptr);
658624
* For examples, please see the comment section of opendal_operator_list()
659625
* @see opendal_operator_list()
660626
*/
661-
struct opendal_result_lister_next opendal_lister_next(const struct opendal_lister *self);
627+
struct opendal_result_lister_next opendal_lister_next(struct opendal_lister *self);
662628

663629
/**
664630
* \brief Free the heap-allocated metadata used by opendal_lister
@@ -752,7 +718,7 @@ int64_t opendal_metadata_last_modified_ms(const struct opendal_metadata *self);
752718
* opendal_operator_free(op);
753719
* ```
754720
*/
755-
void opendal_operator_free(const struct opendal_operator *op);
721+
void opendal_operator_free(const struct opendal_operator *ptr);
756722

757723
/**
758724
* \brief Construct an operator based on `scheme` and `options`
@@ -1343,7 +1309,7 @@ struct opendal_capability opendal_operator_info_get_native_capability(const stru
13431309
/**
13441310
* \brief Frees the heap memory used by the opendal_bytes
13451311
*/
1346-
void opendal_bytes_free(struct opendal_bytes *bs);
1312+
void opendal_bytes_free(struct opendal_bytes *ptr);
13471313

13481314
/**
13491315
* \brief Construct a heap-allocated opendal_operator_options
@@ -1411,7 +1377,7 @@ void opendal_entry_free(struct opendal_entry *ptr);
14111377
/**
14121378
* \brief Read data from the reader.
14131379
*/
1414-
struct opendal_result_reader_read opendal_reader_read(const struct opendal_reader *self,
1380+
struct opendal_result_reader_read opendal_reader_read(struct opendal_reader *self,
14151381
uint8_t *buf,
14161382
uintptr_t len);
14171383

@@ -1423,7 +1389,7 @@ void opendal_reader_free(struct opendal_reader *ptr);
14231389
/**
14241390
* \brief Write data to the writer.
14251391
*/
1426-
struct opendal_result_writer_write opendal_writer_write(const struct opendal_writer *self,
1392+
struct opendal_result_writer_write opendal_writer_write(struct opendal_writer *self,
14271393
struct opendal_bytes bytes);
14281394

14291395
/**

bindings/c/src/entry.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use ::opendal as core;
2828
/// @see opendal_list_entry_name()
2929
#[repr(C)]
3030
pub struct opendal_entry {
31+
/// The pointer to the opendal::Entry in the Rust code.
32+
/// Only touch this on judging whether it is NULL.
3133
inner: *mut c_void,
3234
}
3335

@@ -77,8 +79,8 @@ impl opendal_entry {
7779
#[no_mangle]
7880
pub unsafe extern "C" fn opendal_entry_free(ptr: *mut opendal_entry) {
7981
if !ptr.is_null() {
80-
let _ = Box::from_raw((*ptr).inner as *mut core::Entry);
81-
let _ = Box::from_raw(ptr);
82+
drop(Box::from_raw((*ptr).inner as *mut core::Entry));
83+
drop(Box::from_raw(ptr));
8284
}
8385
}
8486
}

bindings/c/src/error.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ impl From<core::ErrorKind> for opendal_code {
6868
core::ErrorKind::RangeNotSatisfied => opendal_code::OPENDAL_RANGE_NOT_SATISFIED,
6969
// if this is triggered, check the [`core`] crate and add a
7070
// new error code accordingly
71-
_ => panic!("The newly added ErrorKind in core crate is not handled in C bindings"),
71+
_ => unimplemented!(
72+
"The newly added ErrorKind in core crate is not handled in C bindings"
73+
),
7274
}
7375
}
7476
}
@@ -112,16 +114,19 @@ impl opendal_error {
112114
#[no_mangle]
113115
pub unsafe extern "C" fn opendal_error_free(ptr: *mut opendal_error) {
114116
if !ptr.is_null() {
115-
let message_ptr = &(*ptr).message as *const opendal_bytes as *mut opendal_bytes;
117+
let message_ptr = &(*ptr).message;
118+
let message_ptr = message_ptr as *const opendal_bytes as *mut opendal_bytes;
116119
if !message_ptr.is_null() {
117-
let data_mut = unsafe { (*message_ptr).data as *mut u8 };
118-
let _ = unsafe {
119-
Vec::from_raw_parts(data_mut, (*message_ptr).len, (*message_ptr).len)
120-
};
120+
let data_mut = (*message_ptr).data as *mut u8;
121+
drop(Vec::from_raw_parts(
122+
data_mut,
123+
(*message_ptr).len,
124+
(*message_ptr).len,
125+
));
121126
}
122127

123128
// free the pointer
124-
let _ = unsafe { Box::from_raw(ptr) };
129+
drop(Box::from_raw(ptr))
125130
}
126131
}
127132
}

bindings/c/src/lister.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ use super::*;
2929
/// @see opendal_operator_list()
3030
#[repr(C)]
3131
pub struct opendal_lister {
32+
/// The pointer to the opendal::BlockingLister in the Rust code.
33+
/// Only touch this on judging whether it is NULL.
3234
inner: *mut c_void,
3335
}
3436

3537
impl opendal_lister {
36-
fn deref_mut(&self) -> &mut core::BlockingLister {
38+
fn deref_mut(&mut self) -> &mut core::BlockingLister {
3739
// Safety: the inner should never be null once constructed
3840
// The use-after-free is undefined behavior
3941
unsafe { &mut *(self.inner as *mut core::BlockingLister) }
@@ -55,7 +57,7 @@ impl opendal_lister {
5557
/// For examples, please see the comment section of opendal_operator_list()
5658
/// @see opendal_operator_list()
5759
#[no_mangle]
58-
pub unsafe extern "C" fn opendal_lister_next(&self) -> opendal_result_lister_next {
60+
pub unsafe extern "C" fn opendal_lister_next(&mut self) -> opendal_result_lister_next {
5961
let e = self.deref_mut().next();
6062
if e.is_none() {
6163
return opendal_result_lister_next {
@@ -83,8 +85,8 @@ impl opendal_lister {
8385
#[no_mangle]
8486
pub unsafe extern "C" fn opendal_lister_free(ptr: *mut opendal_lister) {
8587
if !ptr.is_null() {
86-
let _ = Box::from_raw((*ptr).inner as *mut core::BlockingLister);
87-
let _ = Box::from_raw(ptr);
88+
drop(Box::from_raw((*ptr).inner as *mut core::BlockingLister));
89+
drop(Box::from_raw(ptr));
8890
}
8991
}
9092
}

bindings/c/src/metadata.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ impl opendal_metadata {
5555
#[no_mangle]
5656
pub unsafe extern "C" fn opendal_metadata_free(ptr: *mut opendal_metadata) {
5757
if !ptr.is_null() {
58-
let _ = Box::from_raw((*ptr).inner as *mut core::Metadata);
59-
let _ = Box::from_raw(ptr);
58+
drop(Box::from_raw((*ptr).inner as *mut core::Metadata));
59+
drop(Box::from_raw(ptr));
6060
}
6161
}
6262

0 commit comments

Comments
 (0)