Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. trait CloneFn: Fn() {
  2. fn clone_erased(&self) -> Box<dyn CloneFn>;
  3. }
  4.  
  5. impl<F: Fn() + Clone + 'static> CloneFn for F {
  6. fn clone_erased(&self) -> Box<dyn CloneFn> {
  7. Box::new(self.clone())
  8. }
  9. }
  10.  
  11. fn foo(f: &dyn CloneFn) {
  12. f();
  13. let g = f.clone_erased();
  14. g();
  15. let h = g.clone_erased();
  16. h();
  17. }
  18.  
  19. fn main() {
  20. use std::sync::Arc;
  21. let a = Arc::new(());
  22. foo(& move || { println!("{}", Arc::strong_count(&a)) })
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement