pub struct Sendify<T> {
ptr: *const T,
}
impl<T> Sendify<T> {
pub fn wrap(val: &T) -> Sendify<T> {
Sendify { ptr: val }
}
pub unsafe fn unwrap<'a>(self) -> &'a T {
self.ptr.as_ref().unwrap()
}
}
unsafe impl<T: Send + 'static> Send for Sendify<T> {}
unsafe impl<T: Sync + 'static> Sync for Sendify<T> {}
#[cfg(test)]
mod tests {
use super::*;
use std::thread;
#[test]
fn test_sendify() {
let data = "my string".to_owned();
let ref_val = &data;
let sendify_val = Sendify::wrap(ref_val);
thread::spawn(move || {
let ref_val = unsafe { sendify_val.unwrap() };
assert_eq!(ref_val, "my string")
})
.join()
.unwrap();
assert_eq!(data, "my string")
}
}