title: Should I use Pin when making C++ call a Rust method through a pointer?
body: <p>I have C++ code that calls Rust code with data. It knows which object to send the data to. Here's an example of the Rust function that C++ calls back:</p>
  <pre class="lang-rust prettyprint-override"><code>extern &quot;C&quot; fn on_open_vpn_receive(
      instance: Box&lt;OpenVpn&gt;,
      data: *mut c_uchar,
      size: *mut size_t,
  ) -&gt; u8 
  </code></pre>
  <p>It receives the pointer as a <code>Box</code>, so I created a function <code>openvpn_set_rust_parent</code> that sets which object the C++ must call back. This object is a pointer to itself. I'm using <code>Pin</code> so the <code>Box</code> is not reallocated to somewhere else, making C++ call an invalid address.</p>
  <pre class="lang-rust prettyprint-override"><code>impl OpenVpn {
      pub fn new() -&gt; Pin&lt;Box&lt;OpenVpn&gt;&gt; {
          let instance = unsafe { interface::openvpn_new(profile.as_ptr()) };
          let o = OpenVpn { instance: instance };
          let p = Box::pin(o);
          unsafe {
              interface::openvpn_set_rust_parent(o.instance, p.as_ptr());
          };
          p
      }
  }
  </code></pre>
  <p>Signature:</p>
  <pre class="lang-rust prettyprint-override"><code>pub fn openvpn_set_rust_parent(instance: *mut OpenVpnInstance, parent: *mut OpenVpn)
  </code></pre>
  <p>I don't know how to transform <code>p</code> into <code>*mut OpenVpn</code> to pass to C++. Is my idea ok? I think the usage of <code>Pin</code> is good here, and I think this is a good way of calling the object from C++.</p>