Guest User

Untitled

a guest
Nov 12th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.78 KB | None | 0 0
  1. /*
  2.   type t = data ref
  3.   and data =
  4.     | Array of int array
  5.     | Diff of int * int * t
  6. */
  7. #[derive(Debug)]
  8. enum Data {
  9.     Array(Vec<u32>),
  10.     Diff(usize, u32, T)
  11. }
  12.  
  13. type T = Box<Data>;
  14.  
  15. /*
  16.   let create n v = ref (Array (Array.create n v))
  17.   let init n f = ref (Array (Array.init n f))
  18. */
  19. fn create(n: usize, v: u32) -> T {
  20.     let mut data = Vec::new();
  21.     data.resize(n, v);
  22.     Box::new(Data::Array(data))
  23. }
  24.  
  25. #[allow(dead_code)]
  26. fn init<F>(n: usize, f: F) -> T where F : Fn(usize) -> u32 {
  27.     let mut data = Vec::new();
  28.     data.resize(n, 0);
  29.     for i in 0..n {
  30.         data[i] = f(i);
  31.     }
  32.     Box::new(Data::Array(data))
  33. }
  34.  
  35. /*
  36.   (* reroot t ensures that t becomes an Array node *)
  37.   let rec reroot t = match !t with
  38.     | Array _ -> ()
  39.     | Diff (i, v, t') ->
  40.         reroot t';
  41.         begin match !t' with
  42.           | Array a as n ->
  43.               let v' = a.(i) in
  44.               a.(i) <- v;
  45.               t := n;
  46.               t' := Diff (i, v', t)
  47.           | Diff _ -> assert false
  48.         end
  49. */
  50. fn reroot(t: &mut T) {
  51.     match &mut **t {
  52.         Data::Array(_) => (),
  53.         Data::Diff(i, v, t2) => {
  54.             reroot(t2);
  55.             match &mut **t2 {
  56.                 Data::Array(array) => {
  57.                     let mut tarray = std::mem::take(array);
  58.                     let v2 = tarray[*i];
  59.                     tarray[*i] = *v;
  60.                     **t = Data::Array(tarray);
  61. // the next line triggers an error. Everything is fine without it
  62.                     **t2 = Data::Diff(*i, v2, *t)
  63. /*
  64. error[E0506]: cannot assign to `**t` because it is borrowed
  65.   --> src/main.rs:62:21
  66.    |
  67. 53 |     match &mut **t {
  68.    |           -------- borrow of `**t` occurs here
  69. ...
  70. 62 |                     **t = Data::Array(tarray);
  71.    |                     ^^^ assignment to borrowed `**t` occurs here
  72. 63 |                     **t2 = Data::Diff(*i, v2, *t)
  73.    |                                       -- borrow later used here
  74.  
  75. error[E0507]: cannot move out of `*t` which is behind a mutable reference
  76.   --> src/main.rs:63:47
  77.    |
  78. 63 |                     **t2 = Data::Diff(*i, v2, *t)
  79.    |                                               ^^ move occurs because `*t` has type `std::boxed::Box<Data>`, which does not implement the `Copy` trait
  80.  
  81. error[E0505]: cannot move out of `*t` because it is borrowed
  82.   --> src/main.rs:63:47
  83.    |
  84. 53 |     match &mut **t {
  85.    |           -------- borrow of `**t` occurs here
  86. ...
  87. 63 |                     **t2 = Data::Diff(*i, v2, *t)
  88.    |                     ----                      ^^ move out of `*t` occurs here
  89.    |                     |
  90.    |                     borrow later used here
  91. */
  92.                 }
  93.                 Data::Diff(_, _, _) => panic!("reroot")
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment