<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="290395" data-post-id="290395">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="tessi" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/tessi/120/2919_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  tessi
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hey <a class="mention" href="/u/echojoys" rel="nofollow">@echojoys</a> ,</p>
<p>I want to follow up on the topic of “safe” string returns through multi-value returns from WASM. I implemented this feature <a href="https://github.com/tessi/wasmex/pull/438" rel="noopener nofollow ugc">here</a> and expect support for multi-value returns to land very soon in wasmex.</p>
<p>After the MR landed, you can have Rust code that returns a string pointer <em>and</em> length from the same function call:</p>
<pre data-code-wrap="rust"><code class="lang-rust">#[no_mangle]
pub fn to_string(a: i32) -&gt; (i32, i32) {
    let str = a.to_string();
    (str.as_ptr() as i32, str.len() as i32)
}
</code></pre>
<p>from Elixir you can call that function like any old function:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">{:ok, [pointer, length]} = Wasmex.call_function(instance, :to_string, [54321])
{:ok, memory} = Wasmex.memory(instance)
assert Wasmex.Memory.read_string(store, memory, pointer, length) == "54321"
</code></pre>
<p>(the code snippets are an extract from Wasmex tests, have a look at those wasmex_test.exs tests to see more examples)</p>
<p>I hope this helps! <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>cheers,<br>
tessi</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="290395" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/return-string-form-rust-in-wasm/51372/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-290395" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="290395"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="290604" data-post-id="290604">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="echojoys" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/echojoys/120/32669_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  echojoys
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Hey Tessi,</p>
<p>I cannot express how appreciative I am for your detailed and insightful response. Your solution is ingenious, and it’s exactly what I was looking for.</p>
<p>The code snippets you provided have clarified my understanding significantly. It’s enlightening to see how Rust can return a string pointer and length from the same function call, and how Elixir can subsequently call that function. Moreover, I must commend your patience and effort in helping me resolve this issue.</p>
<p>Once again, thank you so much for your time and assistance. I look forward to learning more from your expertise in the future.</p>
<p>Best regards</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="290604" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/return-string-form-rust-in-wasm/51372/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-290604" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="290604"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>