-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
78b3bb2
commit 60fce40
Showing
4 changed files
with
84 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use crate::static_global::byond; | ||
use crate::value::ByondValue; | ||
use byondapi_sys::CByondValue; | ||
use std::os::raw::c_void; | ||
|
||
struct CallbackData<F: FnOnce() -> ByondValue + Send> { | ||
callback: Option<F>, | ||
} | ||
|
||
extern "C" fn trampoline<F: FnOnce() -> ByondValue + Send>(data: *mut c_void) -> CByondValue { | ||
let data = unsafe { Box::from_raw(data as *mut CallbackData<F>) }; | ||
(data.callback.unwrap())().into_inner() | ||
} | ||
|
||
pub fn thread_sync<F>(callback: F, block: bool) -> ByondValue | ||
where | ||
F: FnOnce() -> ByondValue + Send + 'static, | ||
{ | ||
let data = Box::new(CallbackData { | ||
callback: Some(callback), | ||
}); | ||
let data_ptr = Box::into_raw(data) as *mut c_void; | ||
|
||
ByondValue(unsafe { byond().Byond_ThreadSync(Some(trampoline::<F>), data_ptr, block) }) | ||
} |