forked from dtolnay/cxx
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
396 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use core::{ffi::c_void, marker::PhantomData}; | ||
|
||
/// | ||
pub unsafe trait CxxFunctionImpl<Ret, Args> { | ||
#[doc(hidden)] | ||
fn __invoke(f: *mut c_void, a: Args) -> Ret; | ||
} | ||
|
||
/// Bridge to std::function<Fn> | ||
#[repr(C)] | ||
pub struct CxxFunction<I: ?Sized, F> { | ||
// A thing, because repr(C) structs are not allowed to consist exclusively | ||
// of PhantomData fields. | ||
_void: [c_void; 0], | ||
_impl: PhantomData<I>, | ||
_fn: PhantomData<F>, | ||
} | ||
|
||
impl<I, Out> CxxFunction<I, fn() -> Out> | ||
where | ||
I: CxxFunctionImpl<Out, ()>, | ||
{ | ||
/// Run the std::function | ||
pub fn invoke(&mut self) -> Out { | ||
<I as CxxFunctionImpl<Out, ()>>::__invoke(self as *mut _ as _, ()) | ||
} | ||
} | ||
|
||
impl<I, In, Out> CxxFunction<I, fn(In) -> Out> | ||
where | ||
I: CxxFunctionImpl<Out, (In,)>, | ||
{ | ||
/// Run the std::function | ||
pub fn invoke(&mut self, a: In) -> Out { | ||
<I as CxxFunctionImpl<Out, (In,)>>::__invoke(self as *mut _ as _, (a,)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.