Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. use std::sync::Arc;
  2. use std::ops::Deref;
  3.  
  4. struct Foo {
  5. val: usize,
  6. }
  7.  
  8. #[derive(Clone)]
  9. struct ArcFoo {
  10. inner: Arc<Foo>,
  11. }
  12.  
  13. impl Deref for ArcFoo {
  14. type Target = Foo;
  15.  
  16. #[inline]
  17. fn deref(&self) -> &Foo {
  18. &self.inner
  19. }
  20. }
  21.  
  22. impl ArcFoo {
  23. fn new(f: Foo) -> ArcFoo {
  24. ArcFoo {
  25. inner: Arc::new(f),
  26. }
  27. }
  28.  
  29. fn custom_function(&self) {
  30. println!("custom function for Arc<Foo>: {}", self.inner.val)
  31. }
  32. }
  33.  
  34. fn main() {
  35. let custom_arc = ArcFoo::new(Foo{val: 3});
  36. println!("hello world {}", custom_arc.val);
  37. custom_arc.custom_function();
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement