Skip to content

Commit 3a769a2

Browse files
committed
Add WithOutput trait
1 parent 34b68e4 commit 3a769a2

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

src/conversion.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
marker::{self, Safe, Unsafe},
44
};
55

6-
/// Helper trait to change the ABI of a function pointer type while preserving arity, safety, and signature.
6+
/// Helper trait to change the ABI of a function pointer type while preserving arity, safety, signature, etc.
77
pub trait WithAbi<Abi>: FnPtr
88
where
99
Abi: marker::Abi,
@@ -18,7 +18,7 @@ where
1818
> + HasAbi<Abi>;
1919
}
2020

21-
/// Helper trait to change the safety of a function pointer type while preserving arity, ABI, and signature.
21+
/// Helper trait to change the safety of a function pointer type while preserving arity, ABI, signature, etc.
2222
pub trait WithSafety<Safety>: FnPtr
2323
where
2424
Safety: marker::Safety,
@@ -33,14 +33,27 @@ where
3333
> + HasSafety<Safety>;
3434
}
3535

36-
/// Helper trait to compute the safe version of a function pointer type while preserving arity, ABI, and signature.
36+
/// Helper trait to compute the safe version of a function pointer type while preserving arity, ABI, signature, etc.
3737
pub trait AsSafe: WithSafety<Safe> {}
3838
impl<F: WithSafety<Safe>> AsSafe for F {}
3939

40-
/// Helper trait to compute the unsafe version of a function pointer type while preserving arity, ABI, and signature.
40+
/// Helper trait to compute the unsafe version of a function pointer type while preserving arity, ABI, signature, etc.
4141
pub trait AsUnsafe: WithSafety<Unsafe> {}
4242
impl<F: WithSafety<Unsafe>> AsUnsafe for F {}
4343

44+
/// Helper trait to change the return type of a function pointer type while preserving arguments, safety, signature, etc.
45+
pub trait WithOutput<T>: FnPtr {
46+
/// The function pointer type with the requested return type.
47+
type F: FnPtr<
48+
Args = Self::Args,
49+
Output = T,
50+
ArityMarker = Self::ArityMarker,
51+
SafetyMarker = Self::SafetyMarker,
52+
AbiMarker = Self::AbiMarker,
53+
>;
54+
}
55+
56+
4457
cfg_tt::cfg_tt! {
4558
/// Helper trait that simplifies generic bounds when converting between funciton pointer types.
4659
// using has_abi_cdecl instead of stdcall, fastcall, thiscall is to reduce compile times

src/impl.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ macro_rules! impl_fn {
102102
impl<Ret: 'static, $($ty: 'static),*> $crate::StaticFnPtr for $fn_type {
103103
}
104104

105+
// WithOutput
106+
#[automatically_derived]
107+
impl<Output, Ret, $($ty),*> $crate::WithOutput<Output> for $fn_type {
108+
type F = impl_fn!(@make_unsafe extern $call_conv fn($($ty),*) -> Output, $safety);
109+
}
105110
// WithSafety
106111
#[automatically_derived]
107112
impl<Ret, $($ty),*> $crate::WithSafety<$crate::marker::Safe> for $fn_type {

0 commit comments

Comments
 (0)