1616// under the License.
1717
1818use std:: collections:: HashMap ;
19+ use std:: ffi:: c_void;
1920use std:: os:: raw:: c_char;
2021use std:: str:: FromStr ;
2122use 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 ) ]
3739pub enum opendal_future_status {
3840 /// Future is still pending.
3941 OPENDAL_FUTURE_PENDING = 0 ,
@@ -45,26 +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 =
50- opendal_future_status:: OPENDAL_FUTURE_PENDING ;
51- #[ no_mangle]
52- pub static OPENDAL_FUTURE_READY : opendal_future_status =
53- opendal_future_status:: OPENDAL_FUTURE_READY ;
54- #[ no_mangle]
55- pub static OPENDAL_FUTURE_ERROR : opendal_future_status =
56- opendal_future_status:: OPENDAL_FUTURE_ERROR ;
57- #[ no_mangle]
58- pub static OPENDAL_FUTURE_CANCELED : opendal_future_status =
59- opendal_future_status:: OPENDAL_FUTURE_CANCELED ;
60-
6150macro_rules! impl_poll_result {
62- ( $fn_name: ident, $future_ty: ty, $out_ty: ty, $ok_ctor: expr, $err_ctor: expr) => {
63- #[ no_mangle]
64- pub unsafe extern "C" fn $fn_name(
65- future: * mut $future_ty,
66- out: * mut $out_ty,
67- ) -> 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 {
6853 if future. is_null( ) || out. is_null( ) {
6954 return opendal_future_status:: OPENDAL_FUTURE_ERROR ;
7055 }
@@ -118,9 +103,8 @@ macro_rules! impl_poll_result {
118103}
119104
120105macro_rules! impl_poll_error_only {
121- ( $fn_name: ident, $future_ty: ty) => {
122- #[ no_mangle]
123- pub unsafe extern "C" fn $fn_name(
106+ ( $fn_impl: ident, $future_ty: ty) => {
107+ unsafe fn $fn_impl(
124108 future: * mut $future_ty,
125109 error_out: * mut * mut opendal_error,
126110 ) -> opendal_future_status {
@@ -174,9 +158,8 @@ macro_rules! impl_poll_error_only {
174158}
175159
176160macro_rules! impl_is_ready_fn {
177- ( $fn_name: ident, $future_ty: ty) => {
178- #[ no_mangle]
179- 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 {
180163 if future. is_null( ) {
181164 return false ;
182165 }
@@ -390,7 +373,7 @@ unsafe impl Send for opendal_result_future_delete {}
390373#[ repr( C ) ]
391374pub struct opendal_async_operator {
392375 /// Internal pointer to the Rust async Operator.
393- inner : * mut core :: Operator ,
376+ inner : * mut c_void ,
394377}
395378
396379impl opendal_async_operator {
@@ -402,7 +385,7 @@ impl opendal_async_operator {
402385 /// and that the lifetime of the returned reference does not exceed the lifetime
403386 /// of the `opendal_async_operator`.
404387 pub ( crate ) unsafe fn as_ref ( & self ) -> & core:: Operator {
405- & * self . inner
388+ & * ( self . inner as * mut core :: Operator )
406389 }
407390}
408391
@@ -463,7 +446,7 @@ pub unsafe extern "C" fn opendal_async_operator_new(
463446 op = op. layer ( core:: layers:: RetryLayer :: new ( ) ) ;
464447
465448 let async_op = Box :: into_raw ( Box :: new ( opendal_async_operator {
466- inner : Box :: into_raw ( Box :: new ( op) ) ,
449+ inner : Box :: into_raw ( Box :: new ( op) ) as * mut c_void ,
467450 } ) ) ;
468451
469452 // We reuse opendal_result_operator_new, but the `op` field now points
@@ -490,7 +473,7 @@ pub unsafe extern "C" fn opendal_async_operator_new(
490473pub unsafe extern "C" fn opendal_async_operator_free ( op : * const opendal_async_operator ) {
491474 if !op. is_null ( ) {
492475 // Drop the inner Operator
493- drop ( Box :: from_raw ( ( * op) . inner ) ) ;
476+ drop ( Box :: from_raw ( ( * op) . inner as * mut core :: Operator ) ) ;
494477 // Drop the container struct itself
495478 drop ( Box :: from_raw ( op as * mut opendal_async_operator ) ) ;
496479 }
@@ -587,7 +570,7 @@ pub unsafe extern "C" fn opendal_future_stat_await(
587570}
588571
589572impl_poll_result ! (
590- opendal_future_stat_poll ,
573+ opendal_future_stat_poll_impl ,
591574 opendal_future_stat,
592575 opendal_result_stat,
593576 |metadata| opendal_result_stat {
@@ -600,7 +583,20 @@ impl_poll_result!(
600583 }
601584) ;
602585
603- 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+ }
604600
605601/// \brief Cancel and free a stat future without awaiting it.
606602#[ no_mangle]
@@ -706,7 +702,7 @@ pub unsafe extern "C" fn opendal_future_read_await(
706702}
707703
708704impl_poll_result ! (
709- opendal_future_read_poll ,
705+ opendal_future_read_poll_impl ,
710706 opendal_future_read,
711707 opendal_result_read,
712708 |buffer| opendal_result_read {
@@ -719,7 +715,20 @@ impl_poll_result!(
719715 }
720716) ;
721717
722- 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+ }
723732
724733/// \brief Cancel and free a read future without awaiting it.
725734#[ no_mangle]
@@ -820,9 +829,24 @@ pub unsafe extern "C" fn opendal_future_write_await(
820829 opendal_future_write_await_impl ( future)
821830}
822831
823- 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) ;
824843
825- 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+ }
826850
827851/// \brief Cancel and free a write future without awaiting it.
828852#[ no_mangle]
@@ -912,9 +936,24 @@ pub unsafe extern "C" fn opendal_future_delete_await(
912936 opendal_future_delete_await_impl ( future)
913937}
914938
915- 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) ;
916950
917- 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+ }
918957
919958/// \brief Cancel and free a delete future without awaiting it.
920959#[ no_mangle]
0 commit comments