title: Retrieving a stack-allocated value from a C++/CXX.rs-managed binding
body: <p>I am calling out to a C++ library from inside of Rust. I am using the CXX binding generator along with some C++ &quot;glue&quot; code. The idea is that I initialize an object (easy enough) and then call a method to get an &quot;enumerator&quot; data type. It doesn't matter that it's an enumerator but it is important to note the enumerator is passed back as a stack-allocated piece of data.</p>
  <p>The C++ library has this signature:</p>
  <pre><code>TautomerEnumeratorResult RDKit::MolStandardize::TautomerEnumerator::enumerate   (   const ROMol &amp;   mol )   const
  </code></pre>
  <p>So you create a <code>TautomerEnumerator</code> instance, pass in a <code>Mol</code> reference, and the method gives you a stack <code>TautomerEnumeratorResult</code>.</p>
  <p>The C++ wrapper side of the CXX bridge looks like:</p>
  <pre><code>#include &quot;rust/cxx.h&quot;
  #include &quot;GraphMol/GraphMol.h&quot;
  #include &quot;GraphMol/SmilesParse/SmilesParse.h&quot;
  #include &quot;GraphMol/SmilesParse/SmilesWrite.h&quot;
  #include &quot;DataStructs/ExplicitBitVect.h&quot;
  #include &quot;GraphMol/Fingerprints/Fingerprints.h&quot;
  #include &quot;GraphMol/MolStandardize/Tautomer.h&quot;
  
  namespace RDKit {
      using ExplicitBitVect = ::ExplicitBitVect;
      using TautomerEnumerator = MolStandardize::TautomerEnumerator;
      using TautomerEnumeratorResult = MolStandardize::TautomerEnumeratorResult;
  
      TautomerEnumeratorResult enumerate_tautomer(TautomerEnumerator *enumerator, std::shared_ptr&lt;ROMol&gt; mol) {
          return enumerator-&gt;enumerate(*mol);
      }
  }
  </code></pre>
  <p>which is fine. the Rust side of the CXX bridge looks like:</p>
  <pre><code>#[cxx::bridge(namespace = &quot;RDKit&quot;)]
  pub mod ffi {
      unsafe extern &quot;C++&quot; {
          include!(&quot;wrapper/include/rdmol.h&quot;);
  
          pub type TautomerEnumerator;
          pub type TautomerEnumeratorResult;
          pub unsafe fn tautomer_enumerator() -&gt; *mut TautomerEnumerator;
          pub unsafe fn enumerate_tautomer(mol: SharedPtr&lt;ROMol&gt;) -&gt; TautomerEnumeratorResult;
      }
  }
  </code></pre>
  <p>which blows at compile time with:</p>
  <pre><code>  error[cxxbridge]: returning opaque C++ type by value is not supported
      ┌─ src/cxx/bridge/mol_standardize.rs:9:68
      │
    9 │         pub unsafe fn enumerate_tautomer(mol: SharedPtr&lt;ROMol&gt;) -&gt; TautomerEnumeratorResult;
      │                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^ returning opaque C++ type by value is not supported
      │
      = hint: wrap it in a UniquePtr&lt;&gt; or SharedPtr&lt;&gt;
  </code></pre>
  <p>But since it's a stack allocated variable I can't return it. Is there a zero cost way of sharing <code>TautomerEnumeratorResult</code> with Rust over the stack? How can I make the variable &quot;non opaque&quot; when it's a class and not a struct?</p>