Guest User

Untitled

a guest
Feb 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. use std::sync::mpsc::channel;
  2. use std::time::Duration;
  3. use std::thread;
  4.  
  5. fn init_and_spawn<'a, F, G> (f : F) where
  6. F: 'a + Send + FnOnce() -> G,
  7. G: 'static + FnOnce(),
  8. {
  9. let (send, recv) = channel();
  10. let boxed = Box::new(f);
  11. let raw = Box::into_raw(boxed) as usize;
  12. thread::spawn (move || {
  13. let boxed = unsafe { Box::from_raw(raw as *mut F) };
  14. let f = *boxed;
  15. let g = f();
  16. let _ = send.send(());
  17. g()
  18. });
  19. let _ = recv.recv();
  20. }
  21.  
  22. fn main() {
  23. let ref hello = String::from("hello");
  24. init_and_spawn(|| {
  25. let clone = hello.clone();
  26. move || {
  27. println!("{}", clone);
  28. }
  29. });
  30. thread::sleep(Duration::from_millis(1000));
  31. }
Add Comment
Please, Sign In to add comment