Skip to content

Commit 27cbac5

Browse files
fix
fmt fmt fix build clippy fmt
1 parent de401c3 commit 27cbac5

File tree

9 files changed

+468
-362
lines changed

9 files changed

+468
-362
lines changed

bindings/c/examples/compare_sync_async.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ int main(void)
124124
const opendal_async_operator* op = (const opendal_async_operator*)r.op;
125125

126126
const char* msg = "hello poll";
127-
opendal_bytes data = {.data = (uint8_t*)msg, .len = strlen(msg), .capacity = strlen(msg)};
127+
opendal_bytes data = { .data = (uint8_t*)msg, .len = strlen(msg), .capacity = strlen(msg) };
128128
opendal_result_future_write wf = opendal_async_operator_write(op, "poll.txt", &data);
129129
opendal_error* werr = opendal_future_write_await(wf.future);
130-
if (werr) { opendal_error_free(werr); }
130+
if (werr) {
131+
opendal_error_free(werr);
132+
}
131133

132134
opendal_result_future_read rf = opendal_async_operator_read(op, "poll.txt");
133-
opendal_result_read rd = {0};
135+
opendal_result_read rd = { 0 };
134136
while (1) {
135137
opendal_future_status st = opendal_future_read_poll(rf.future, &rd);
136138
if (st == OPENDAL_FUTURE_PENDING) {
@@ -152,7 +154,8 @@ int main(void)
152154

153155
opendal_result_future_delete df = opendal_async_operator_delete(op, "poll.txt");
154156
opendal_error* derr = opendal_future_delete_await(df.future);
155-
if (derr) opendal_error_free(derr);
157+
if (derr)
158+
opendal_error_free(derr);
156159

157160
opendal_async_operator_free(op);
158161

bindings/c/include/opendal.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ typedef struct opendal_async_operator {
464464
/**
465465
* Internal pointer to the Rust async Operator.
466466
*/
467-
Operator *inner;
467+
void *inner;
468468
} opendal_async_operator;
469469

470470
/**
@@ -772,14 +772,6 @@ typedef struct opendal_result_writer_write {
772772
extern "C" {
773773
#endif // __cplusplus
774774

775-
extern const enum opendal_future_status OPENDAL_FUTURE_PENDING;
776-
777-
extern const enum opendal_future_status OPENDAL_FUTURE_READY;
778-
779-
extern const enum opendal_future_status OPENDAL_FUTURE_ERROR;
780-
781-
extern const enum opendal_future_status OPENDAL_FUTURE_CANCELED;
782-
783775
/**
784776
* \brief Frees the opendal_error, ok to call on NULL
785777
*/
@@ -1511,6 +1503,11 @@ struct opendal_result_future_stat opendal_async_operator_stat(const struct opend
15111503

15121504
struct opendal_result_stat opendal_future_stat_await(struct opendal_future_stat *future);
15131505

1506+
enum opendal_future_status opendal_future_stat_poll(struct opendal_future_stat *future,
1507+
struct opendal_result_stat *out);
1508+
1509+
bool opendal_future_stat_is_ready(const struct opendal_future_stat *future);
1510+
15141511
/**
15151512
* \brief Cancel and free a stat future without awaiting it.
15161513
*/
@@ -1527,6 +1524,11 @@ struct opendal_result_future_read opendal_async_operator_read(const struct opend
15271524

15281525
struct opendal_result_read opendal_future_read_await(struct opendal_future_read *future);
15291526

1527+
enum opendal_future_status opendal_future_read_poll(struct opendal_future_read *future,
1528+
struct opendal_result_read *out);
1529+
1530+
bool opendal_future_read_is_ready(const struct opendal_future_read *future);
1531+
15301532
/**
15311533
* \brief Cancel and free a read future without awaiting it.
15321534
*/
@@ -1541,6 +1543,11 @@ struct opendal_result_future_write opendal_async_operator_write(const struct ope
15411543

15421544
struct opendal_error *opendal_future_write_await(struct opendal_future_write *future);
15431545

1546+
enum opendal_future_status opendal_future_write_poll(struct opendal_future_write *future,
1547+
struct opendal_error **error_out);
1548+
1549+
bool opendal_future_write_is_ready(const struct opendal_future_write *future);
1550+
15441551
/**
15451552
* \brief Cancel and free a write future without awaiting it.
15461553
*/
@@ -1554,6 +1561,11 @@ struct opendal_result_future_delete opendal_async_operator_delete(const struct o
15541561

15551562
struct opendal_error *opendal_future_delete_await(struct opendal_future_delete *future);
15561563

1564+
enum opendal_future_status opendal_future_delete_poll(struct opendal_future_delete *future,
1565+
struct opendal_error **error_out);
1566+
1567+
bool opendal_future_delete_is_ready(const struct opendal_future_delete *future);
1568+
15571569
/**
15581570
* \brief Cancel and free a delete future without awaiting it.
15591571
*/

bindings/c/src/async_operator.rs

Lines changed: 84 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// under the License.
1717

1818
use std::collections::HashMap;
19+
use std::ffi::c_void;
1920
use std::os::raw::c_char;
2021
use std::str::FromStr;
2122
use tokio::sync::oneshot;
@@ -34,6 +35,7 @@ use crate::types::opendal_operator_options; // Keep this
3435

3536
/// Status returned by non-blocking future polling.
3637
#[repr(C)]
38+
#[derive(Copy, Clone)]
3739
pub enum opendal_future_status {
3840
/// Future is still pending.
3941
OPENDAL_FUTURE_PENDING = 0,
@@ -45,22 +47,9 @@ pub enum opendal_future_status {
4547
OPENDAL_FUTURE_CANCELED = 3,
4648
}
4749

48-
#[no_mangle]
49-
pub static OPENDAL_FUTURE_PENDING: opendal_future_status = opendal_future_status::OPENDAL_FUTURE_PENDING;
50-
#[no_mangle]
51-
pub static OPENDAL_FUTURE_READY: opendal_future_status = opendal_future_status::OPENDAL_FUTURE_READY;
52-
#[no_mangle]
53-
pub static OPENDAL_FUTURE_ERROR: opendal_future_status = opendal_future_status::OPENDAL_FUTURE_ERROR;
54-
#[no_mangle]
55-
pub static OPENDAL_FUTURE_CANCELED: opendal_future_status = opendal_future_status::OPENDAL_FUTURE_CANCELED;
56-
5750
macro_rules! impl_poll_result {
58-
($fn_name:ident, $future_ty:ty, $out_ty:ty, $ok_ctor:expr, $err_ctor:expr) => {
59-
#[no_mangle]
60-
pub unsafe extern "C" fn $fn_name(
61-
future: *mut $future_ty,
62-
out: *mut $out_ty,
63-
) -> opendal_future_status {
51+
($fn_impl:ident, $future_ty:ty, $out_ty:ty, $ok_ctor:expr, $err_ctor:expr) => {
52+
unsafe fn $fn_impl(future: *mut $future_ty, out: *mut $out_ty) -> opendal_future_status {
6453
if future.is_null() || out.is_null() {
6554
return opendal_future_status::OPENDAL_FUTURE_ERROR;
6655
}
@@ -82,7 +71,7 @@ macro_rules! impl_poll_result {
8271
let mut handle_box = Box::from_raw(future.handle);
8372
future.handle = std::ptr::null_mut();
8473
if let Some(handle) = (*handle_box).take() {
85-
let _ = handle;
74+
drop(handle);
8675
}
8776
}
8877

@@ -96,7 +85,7 @@ macro_rules! impl_poll_result {
9685
let mut handle_box = Box::from_raw(future.handle);
9786
future.handle = std::ptr::null_mut();
9887
if let Some(handle) = (*handle_box).take() {
99-
let _ = handle;
88+
drop(handle);
10089
}
10190
}
10291
let out_ref = &mut *out;
@@ -114,9 +103,8 @@ macro_rules! impl_poll_result {
114103
}
115104

116105
macro_rules! impl_poll_error_only {
117-
($fn_name:ident, $future_ty:ty) => {
118-
#[no_mangle]
119-
pub unsafe extern "C" fn $fn_name(
106+
($fn_impl:ident, $future_ty:ty) => {
107+
unsafe fn $fn_impl(
120108
future: *mut $future_ty,
121109
error_out: *mut *mut opendal_error,
122110
) -> opendal_future_status {
@@ -141,7 +129,7 @@ macro_rules! impl_poll_error_only {
141129
let mut handle_box = Box::from_raw(future.handle);
142130
future.handle = std::ptr::null_mut();
143131
if let Some(handle) = (*handle_box).take() {
144-
let _ = handle;
132+
drop(handle);
145133
}
146134
}
147135
*error_out = std::ptr::null_mut();
@@ -153,7 +141,7 @@ macro_rules! impl_poll_error_only {
153141
let mut handle_box = Box::from_raw(future.handle);
154142
future.handle = std::ptr::null_mut();
155143
if let Some(handle) = (*handle_box).take() {
156-
let _ = handle;
144+
drop(handle);
157145
}
158146
}
159147
*error_out = opendal_error::new(err);
@@ -170,9 +158,8 @@ macro_rules! impl_poll_error_only {
170158
}
171159

172160
macro_rules! impl_is_ready_fn {
173-
($fn_name:ident, $future_ty:ty) => {
174-
#[no_mangle]
175-
pub unsafe extern "C" fn $fn_name(future: *const $future_ty) -> bool {
161+
($fn_impl:ident, $future_ty:ty) => {
162+
unsafe fn $fn_impl(future: *const $future_ty) -> bool {
176163
if future.is_null() {
177164
return false;
178165
}
@@ -222,13 +209,13 @@ macro_rules! impl_await_result {
222209
}
223210
};
224211

225-
let recv_result = crate::operator::RUNTIME.block_on(async { rx.await });
212+
let recv_result = crate::operator::RUNTIME.block_on(rx);
226213

227214
if !future.handle.is_null() {
228215
let mut handle_box = Box::from_raw(future.handle);
229216
future.handle = std::ptr::null_mut();
230217
if let Some(handle) = (*handle_box).take() {
231-
let _ = handle;
218+
drop(handle);
232219
}
233220
}
234221

@@ -275,13 +262,13 @@ macro_rules! impl_await_error_only {
275262
}
276263
};
277264

278-
let recv_result = crate::operator::RUNTIME.block_on(async { rx.await });
265+
let recv_result = crate::operator::RUNTIME.block_on(rx);
279266

280267
if !future.handle.is_null() {
281268
let mut handle_box = Box::from_raw(future.handle);
282269
future.handle = std::ptr::null_mut();
283270
if let Some(handle) = (*handle_box).take() {
284-
let _ = handle;
271+
drop(handle);
285272
}
286273
}
287274

@@ -386,7 +373,7 @@ unsafe impl Send for opendal_result_future_delete {}
386373
#[repr(C)]
387374
pub struct opendal_async_operator {
388375
/// Internal pointer to the Rust async Operator.
389-
inner: *mut core::Operator,
376+
inner: *mut c_void,
390377
}
391378

392379
impl opendal_async_operator {
@@ -398,7 +385,7 @@ impl opendal_async_operator {
398385
/// and that the lifetime of the returned reference does not exceed the lifetime
399386
/// of the `opendal_async_operator`.
400387
pub(crate) unsafe fn as_ref(&self) -> &core::Operator {
401-
&*self.inner
388+
&*(self.inner as *mut core::Operator)
402389
}
403390
}
404391

@@ -459,7 +446,7 @@ pub unsafe extern "C" fn opendal_async_operator_new(
459446
op = op.layer(core::layers::RetryLayer::new());
460447

461448
let async_op = Box::into_raw(Box::new(opendal_async_operator {
462-
inner: Box::into_raw(Box::new(op)),
449+
inner: Box::into_raw(Box::new(op)) as *mut c_void,
463450
}));
464451

465452
// We reuse opendal_result_operator_new, but the `op` field now points
@@ -486,7 +473,7 @@ pub unsafe extern "C" fn opendal_async_operator_new(
486473
pub unsafe extern "C" fn opendal_async_operator_free(op: *const opendal_async_operator) {
487474
if !op.is_null() {
488475
// Drop the inner Operator
489-
drop(Box::from_raw((*op).inner));
476+
drop(Box::from_raw((*op).inner as *mut core::Operator));
490477
// Drop the container struct itself
491478
drop(Box::from_raw(op as *mut opendal_async_operator));
492479
}
@@ -583,7 +570,7 @@ pub unsafe extern "C" fn opendal_future_stat_await(
583570
}
584571

585572
impl_poll_result!(
586-
opendal_future_stat_poll,
573+
opendal_future_stat_poll_impl,
587574
opendal_future_stat,
588575
opendal_result_stat,
589576
|metadata| opendal_result_stat {
@@ -596,7 +583,20 @@ impl_poll_result!(
596583
}
597584
);
598585

599-
impl_is_ready_fn!(opendal_future_stat_is_ready, opendal_future_stat);
586+
#[no_mangle]
587+
pub unsafe extern "C" fn opendal_future_stat_poll(
588+
future: *mut opendal_future_stat,
589+
out: *mut opendal_result_stat,
590+
) -> opendal_future_status {
591+
opendal_future_stat_poll_impl(future, out)
592+
}
593+
594+
impl_is_ready_fn!(opendal_future_stat_is_ready_impl, opendal_future_stat);
595+
596+
#[no_mangle]
597+
pub unsafe extern "C" fn opendal_future_stat_is_ready(future: *const opendal_future_stat) -> bool {
598+
opendal_future_stat_is_ready_impl(future)
599+
}
600600

601601
/// \brief Cancel and free a stat future without awaiting it.
602602
#[no_mangle]
@@ -702,7 +702,7 @@ pub unsafe extern "C" fn opendal_future_read_await(
702702
}
703703

704704
impl_poll_result!(
705-
opendal_future_read_poll,
705+
opendal_future_read_poll_impl,
706706
opendal_future_read,
707707
opendal_result_read,
708708
|buffer| opendal_result_read {
@@ -715,7 +715,20 @@ impl_poll_result!(
715715
}
716716
);
717717

718-
impl_is_ready_fn!(opendal_future_read_is_ready, opendal_future_read);
718+
#[no_mangle]
719+
pub unsafe extern "C" fn opendal_future_read_poll(
720+
future: *mut opendal_future_read,
721+
out: *mut opendal_result_read,
722+
) -> opendal_future_status {
723+
opendal_future_read_poll_impl(future, out)
724+
}
725+
726+
impl_is_ready_fn!(opendal_future_read_is_ready_impl, opendal_future_read);
727+
728+
#[no_mangle]
729+
pub unsafe extern "C" fn opendal_future_read_is_ready(future: *const opendal_future_read) -> bool {
730+
opendal_future_read_is_ready_impl(future)
731+
}
719732

720733
/// \brief Cancel and free a read future without awaiting it.
721734
#[no_mangle]
@@ -816,9 +829,24 @@ pub unsafe extern "C" fn opendal_future_write_await(
816829
opendal_future_write_await_impl(future)
817830
}
818831

819-
impl_poll_error_only!(opendal_future_write_poll, opendal_future_write);
832+
impl_poll_error_only!(opendal_future_write_poll_impl, opendal_future_write);
833+
834+
#[no_mangle]
835+
pub unsafe extern "C" fn opendal_future_write_poll(
836+
future: *mut opendal_future_write,
837+
error_out: *mut *mut opendal_error,
838+
) -> opendal_future_status {
839+
opendal_future_write_poll_impl(future, error_out)
840+
}
841+
842+
impl_is_ready_fn!(opendal_future_write_is_ready_impl, opendal_future_write);
820843

821-
impl_is_ready_fn!(opendal_future_write_is_ready, opendal_future_write);
844+
#[no_mangle]
845+
pub unsafe extern "C" fn opendal_future_write_is_ready(
846+
future: *const opendal_future_write,
847+
) -> bool {
848+
opendal_future_write_is_ready_impl(future)
849+
}
822850

823851
/// \brief Cancel and free a write future without awaiting it.
824852
#[no_mangle]
@@ -908,9 +936,24 @@ pub unsafe extern "C" fn opendal_future_delete_await(
908936
opendal_future_delete_await_impl(future)
909937
}
910938

911-
impl_poll_error_only!(opendal_future_delete_poll, opendal_future_delete);
939+
impl_poll_error_only!(opendal_future_delete_poll_impl, opendal_future_delete);
940+
941+
#[no_mangle]
942+
pub unsafe extern "C" fn opendal_future_delete_poll(
943+
future: *mut opendal_future_delete,
944+
error_out: *mut *mut opendal_error,
945+
) -> opendal_future_status {
946+
opendal_future_delete_poll_impl(future, error_out)
947+
}
948+
949+
impl_is_ready_fn!(opendal_future_delete_is_ready_impl, opendal_future_delete);
912950

913-
impl_is_ready_fn!(opendal_future_delete_is_ready, opendal_future_delete);
951+
#[no_mangle]
952+
pub unsafe extern "C" fn opendal_future_delete_is_ready(
953+
future: *const opendal_future_delete,
954+
) -> bool {
955+
opendal_future_delete_is_ready_impl(future)
956+
}
914957

915958
/// \brief Cancel and free a delete future without awaiting it.
916959
#[no_mangle]

0 commit comments

Comments
 (0)