@@ -5,7 +5,8 @@ use core::{
55} ;
66
77use crate :: {
8- AsSafe , AsUnsafe , WithAbi , WithArgs , WithOutput , WithSafety , abi,
8+ WithAbi , WithAbiImpl , WithArgs , WithArgsImpl , WithOutput , WithOutputImpl , WithSafety ,
9+ WithSafetyImpl , abi,
910 abi_value:: AbiValue ,
1011 safety:: { self , Safe , Unsafe } ,
1112 tuple:: Tuple ,
@@ -19,6 +20,8 @@ ffi_opaque::opaque! {
1920/// Type alias for a raw untyped function pointer.
2021pub type UntypedFnPtr = * const OpaqueFn ;
2122
23+ cfg_tt:: cfg_tt! { }
24+
2225cfg_tt:: cfg_tt! {
2326/// Marker trait for all function pointers.
2427pub trait FnPtr :
@@ -37,8 +40,32 @@ pub trait FnPtr:
3740 + UnwindSafe
3841 + RefUnwindSafe
3942 + Sized
40- #[ cfg( nightly_build) ]
41- ( + core:: marker:: FnPtr )
43+ + WithOutputImpl
44+ + WithArgsImpl
45+ + WithSafetyImpl <safety:: Safe >
46+ + WithSafetyImpl <safety:: Unsafe >
47+ + WithAbiImpl <abi:: Rust >
48+ + WithAbiImpl <abi:: C >
49+ + WithAbiImpl <abi:: CUnwind >
50+ + WithAbiImpl <abi:: System >
51+ + WithAbiImpl <abi:: SystemUnwind >
52+ #[ cfg( nightly_build) ] ( + core:: marker:: FnPtr )
53+ #[ cfg( has_abi_aapcs) ] ( + WithAbiImpl <abi:: Aapcs >)
54+ #[ cfg( has_abi_aapcs) ] ( + WithAbiImpl <abi:: AapcsUnwind >)
55+ #[ cfg( has_abi_cdecl) ] ( + WithAbiImpl <abi:: Cdecl >)
56+ #[ cfg( has_abi_cdecl) ] ( + WithAbiImpl <abi:: CdeclUnwind >)
57+ #[ cfg( has_abi_cdecl) ] ( + WithAbiImpl <abi:: Stdcall >)
58+ #[ cfg( has_abi_cdecl) ] ( + WithAbiImpl <abi:: StdcallUnwind >)
59+ #[ cfg( has_abi_cdecl) ] ( + WithAbiImpl <abi:: Fastcall >)
60+ #[ cfg( has_abi_cdecl) ] ( + WithAbiImpl <abi:: FastcallUnwind >)
61+ #[ cfg( has_abi_cdecl) ] ( + WithAbiImpl <abi:: Thiscall >)
62+ #[ cfg( has_abi_cdecl) ] ( + WithAbiImpl <abi:: ThiscallUnwind >)
63+ #[ cfg( has_abi_vectorcall) ] ( + WithAbiImpl <abi:: Vectorcall >)
64+ #[ cfg( has_abi_vectorcall) ] ( + WithAbiImpl <abi:: VectorcallUnwind >)
65+ #[ cfg( has_abi_sysv64) ] ( + WithAbiImpl <abi:: SysV64 >)
66+ #[ cfg( has_abi_sysv64) ] ( + WithAbiImpl <abi:: SysV64Unwind >)
67+ #[ cfg( has_abi_win64) ] ( + WithAbiImpl <abi:: Win64 >)
68+ #[ cfg( has_abi_win64) ] ( + WithAbiImpl <abi:: Win64Unwind >)
4269{
4370 /// The argument types as a tuple.
4471 type Args : Tuple ;
@@ -61,7 +88,7 @@ pub trait FnPtr:
6188 /// Whether the function pointer uses an extern calling convention.
6289 const IS_EXTERN : bool ;
6390
64- /// The ABI associated with this function pointer.
91+ /// The abi associated with this function pointer.
6592 const ABI : AbiValue ;
6693
6794 /// Returns the address of this function.
@@ -99,10 +126,7 @@ pub trait FnPtr:
99126
100127 /// Produces an unsafe version of this function pointer.
101128 #[ must_use]
102- fn as_unsafe( & self ) -> <Self as AsUnsafe >:: F
103- where
104- Self : AsUnsafe ,
105- {
129+ fn as_unsafe( & self ) -> <Self as WithSafety <Unsafe >>:: F {
106130 unsafe { FnPtr :: from_ptr( self . as_ptr( ) ) }
107131 }
108132
@@ -111,10 +135,7 @@ pub trait FnPtr:
111135 /// # Safety
112136 /// Caller must ensure the underlying function is actually safe to call.
113137 #[ must_use]
114- unsafe fn as_safe( & self ) -> <Self as AsSafe >:: F
115- where
116- Self : AsSafe ,
117- {
138+ unsafe fn as_safe( & self ) -> <Self as WithSafety <Safe >>:: F {
118139 self . cast( )
119140 }
120141
@@ -130,10 +151,10 @@ pub trait FnPtr:
130151 self . cast( )
131152 }
132153
133- /// Produces a version of this function pointer with the given ABI .
154+ /// Produces a version of this function pointer with the given abi .
134155 ///
135156 /// # Safety
136- /// Caller must ensure that the resulting ABI transformation is sound.
157+ /// Caller must ensure that the resulting abi transformation is sound.
137158 #[ must_use]
138159 unsafe fn with_abi<Abi : abi:: Abi >( & self ) -> <Self as WithAbi <Abi >>:: F
139160 where
@@ -147,7 +168,7 @@ pub trait FnPtr:
147168 /// # Safety
148169 /// Caller must ensure that the resulting transformation is sound.
149170 #[ must_use]
150- unsafe fn with_ret <Output >( & self ) -> <Self as WithOutput <Output >>:: F
171+ unsafe fn with_output <Output >( & self ) -> <Self as WithOutput <Output >>:: F
151172 where
152173 Self : WithOutput <Output >,
153174 {
@@ -212,3 +233,22 @@ pub trait UnsafeFnPtr: FnPtr<Safety = Unsafe> {
212233/// The return type and all parameter types have to be `'static`.
213234pub trait StaticFnPtr : FnPtr + ' static { }
214235impl < F : FnPtr + ' static > StaticFnPtr for F { }
236+
237+ #[ cfg( test) ]
238+ #[ allow( unused) ]
239+ mod test {
240+ use super :: * ;
241+
242+ fn h < F : FnPtr > ( f : F ) -> crate :: with_abi!( "system" , F ) {
243+ unsafe { f. cast ( ) }
244+ }
245+ fn f < F : FnPtr > ( f : F ) -> crate :: with_safety!( unsafe , F ) {
246+ unsafe { f. cast ( ) }
247+ }
248+ fn j < F : FnPtr > ( f : F ) -> crate :: with_output!( i32 , F ) {
249+ unsafe { f. cast ( ) }
250+ }
251+ fn k < F : FnPtr > ( f : F ) -> crate :: with_args!( ( i32 , ) , F ) {
252+ unsafe { f. cast ( ) }
253+ }
254+ }
0 commit comments