Guest User

Untitled

a guest
Apr 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. use std::ops::Deref;
  2.  
  3. enum Foo {
  4. A(i32),
  5. B(i32),
  6. C(i32),
  7. }
  8.  
  9. struct SBox<T> {
  10. val: T,
  11. }
  12.  
  13. impl<T> SBox<T> {
  14. pub fn new(val: T) -> Self {
  15. SBox { val }
  16. }
  17. }
  18.  
  19. impl<T> Deref for SBox<T> {
  20. type Target = T;
  21. fn deref(&self) -> &T {
  22. &self.val
  23. }
  24. }
  25.  
  26. fn foo(foo: SBox<Box<Foo>>) -> SBox<Box<Foo>> {
  27. match **foo {
  28. Foo::A(ref f) => {
  29. let f = SBox::new(Box::new(Foo::B(-f)));
  30. f
  31. }
  32. _ => foo,
  33. }
  34. }
  35.  
  36. fn main() {
  37. let mut a = SBox::new(Box::new(Foo::A(5)));
  38. a = foo(a);
  39. }
Add Comment
Please, Sign In to add comment