title: Is it possible to associate memory from one struct with another in Rust?
body: <p>I know that in Rust a compiler does not guarantee you to get your struct data in the order you declare them, in order to save memory (I also believe some C code optimizer are doing the same thing). Assume now I have a binary tree and want to convert it to a double linked list. In C I would declare two structs:</p>
  
  <pre><code>typedef struct tree{
      void* left_child;
      void* right_child;
      void* data;
  }tree_t;
  </code></pre>
  
  <p>for the tree, and:</p>
  
  <pre><code>typedef struct list{
      void* before;
      void* after;
      void* data;
  }list_t;
  </code></pre>
  
  <p>for the linked list. If I now want to convert the tree to a list I can do this in place, I just associate the memory of the tree with the list struct and change the pointers:</p>
  
  <pre><code>tree_t mytree;
  /*fill tree*/
  list_t *list_p;
  list_p = (list_t)&amp;mytree;
  /*change pointers accordingly*/
  </code></pre>
  
  <p>But How can I do such a thing in Rust? Is it even possible without using <code>unsafe</code> code?
  Till now I have my tree:</p>
  
  <pre><code>struct TreeNode&lt;'a, T&gt; {
      left_child: BinaryTreeLink&lt;'a, T&gt;,
      right_child: BinaryTreeLink&lt;'a, T&gt;,
      data : &amp;'a T,
  }
  
  type BinaryTreeLink&lt;'a, T&gt; = Option&lt;Box&lt;TreeNode&lt;'a, T&gt;&gt;&gt;;
  </code></pre>
  
  <p>and the list would be:</p>
  
  <pre><code>struct ListNode&lt;'a, T&gt; {
      before: ListLink&lt;'a, T&gt;,
      after: ListLink&lt;'a, T&gt;,
      data : &amp;'a T,
  }
  
  type ListLink&lt;'a, T&gt; = Option&lt;Box&lt;ListNode&lt;'a, T&gt;&gt;&gt;;
  </code></pre>
  
  <p>But how can I now convert them efficiently in place?</p>