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,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-
5750macro_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
116105macro_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
172160macro_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 ) ]
387374pub struct opendal_async_operator {
388375 /// Internal pointer to the Rust async Operator.
389- inner : * mut core :: Operator ,
376+ inner : * mut c_void ,
390377}
391378
392379impl 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(
486473pub 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
585572impl_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
704704impl_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