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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Oh, I did exactly the same for my sqlite3 library! Glad that you found the idiom!</p>
<p>Sigh, I should start blogging on these topics. I know it will help a lot of people.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208865" data-batch-url="/posts/batch_likers">
                        3
                      </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/rustler-return-and-pass-reference-from-rust-to-elixir-and-back-to-rust/37429/43">Post #42</a>
	                </div>
	            </div>
              <div id="likers-container-208865" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208865"
                     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 #42"></div>
  </section>
</div>
    <div class="postbit" id="253478" data-post-id="253478">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="a3pelawi" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  a3pelawi
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thanx <a class="mention" href="/u/rm-rf-etc" rel="nofollow">@rm-rf-etc</a>, <a class="mention" href="/u/dimitarvp" rel="nofollow">@dimitarvp</a><br>
This code work for me. I’m sqlite memory for data store.<br>
this my code.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">pub struct MyStruct {
    pub a: i64,
    pub conn: Connection
}
//
fn load(env: Env, _info: Term) -&gt; bool {
    rustler::resource!(Container, env);
    true
}

struct Container {
    mutex: Mutex&lt;MyStruct&gt;,
}

#[rustler::nif]
fn add(a: i64, b: i64) -&gt; ResourceArc&lt;Container&gt; {
    let conn = sqlite::open(":memory:").unwrap();
    let my_new_struct = MyStruct {a: a+b, conn };
    let mutex = Mutex::new(my_new_struct);
    let container = Container { mutex };
    ResourceArc::new(container)
}

#[rustler::nif]
fn print(arc: ResourceArc&lt;Container&gt;) {
    let r = arc.mutex.lock().unwrap();
    println!("{:?}, {:?}", r.a, r.conn.type_id());

    let mut statement = r.conn
        .prepare("SELECT 99")
        .unwrap();


    while let State::Row = statement.next().unwrap() {
        println!("result = {}", statement.read::&lt;i64&gt;(0).unwrap());
    }
}

rustler::init!("Elixir.PwTemplate", [add, print], load = load);
</code></pre>
<p>and test from elixir:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(2)&gt; x = PwTemplate.add(1,2)
#Reference&lt;0.4084525009.3044147210.45533&gt;
</code></pre>
<p>and test feedback:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(3)&gt; PwTemplate.print(x)    
3, TypeId { t: 4628004102010010283 }
                                    result = 99
                                               {}
</code></pre>
<p>Thanx all <img src="https://forum.elixirforum.com/images/emoji/apple/blush.png?v=15" title=":blush:" class="emoji" alt=":blush:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="253478" data-batch-url="/posts/batch_likers">
                        1
                      </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/rustler-return-and-pass-reference-from-rust-to-elixir-and-back-to-rust/37429/44">Post #43</a>
	                </div>
	            </div>
              <div id="likers-container-253478" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="253478"
                     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 #43"></div>
  </section>
</div>
    <div class="postbit" id="294292" data-post-id="294292">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I decided to cheat and pass JSON strings back and forth. I care more about time to working code than how fast it runs. <a href="https://github.com/benhaney/Jsonrs" class="inline-onebox" rel="noopener nofollow ugc">GitHub - benhaney/Jsonrs: Rust powered JSON library for Elixir · GitHub</a></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="294292" data-batch-url="/posts/batch_likers">
                        0
                      </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/rustler-return-and-pass-reference-from-rust-to-elixir-and-back-to-rust/37429/46">Post #45</a>
	                </div>
	            </div>
              <div id="likers-container-294292" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="294292"
                     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 #45"></div>
  </section>
</div>
    <div class="postbit" id="295914" data-post-id="295914">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="mmmrrr" data-post="1" data-topic="37429">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mmmrrr/48/12227_2.png" class="avatar"> mmmrrr:</div>
<blockquote>
<p>The starting point: The Erlang ODBC client has a “bug” where it only allows 4096 bytes to be returned per cell. The Rust odbc client has no such limitations.</p>
</blockquote>
</aside>
<p>I’m a little late to this conversation, but was there any reason why you couldn’t fix the “bug” in the Erlang implementation? Surely, others would have found this limitation to be a hindrance too right? Fixing it in the Erlang implementation may have been less work, and more beneficial to others..</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="295914" data-batch-url="/posts/batch_likers">
                        0
                      </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/rustler-return-and-pass-reference-from-rust-to-elixir-and-back-to-rust/37429/47">Post #46</a>
	                </div>
	            </div>
              <div id="likers-container-295914" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="295914"
                     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 #46"></div>
  </section>
</div>
    <div class="postbit" id="296042" data-post-id="296042">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mmmrrr" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mmmrrr/120/12227_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mmmrrr
                    <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 Benjamin!</p>
<p>Yes, there were several reasons:</p>
<ol>
<li>It’s not a bug. Whoever implemented this feature knew what they were doing when they decided to <em>not</em> allow more data per cell.</li>
<li>There was an endeavor to have a patched version of the ODBC driver in C: <a href="https://github.com/arcusfelis/eodbc" class="inline-onebox" rel="noopener nofollow ugc">GitHub - arcusfelis/eodbc: ODBC backend from OTP + some fixes · GitHub</a> which I tried for my use case. It kinda worked but was far from stable, so I started fooling around with the code but it lead nowhere. And then I had to make a choice because:</li>
<li>I <em>really, really, really</em> don’t enjoy writing C code. Especially not like in this case where you can easily introduce subtle memory sizing bugs (since you basically wouldn’t know how large the payload might be).</li>
</ol>
<p>So I used rustler, rust odbc and the mentioned custom bridge for the DB and wrote a custom Ecto wrapper on top of it in the end (which really payed dividents because now it’s trivial to change the driver/adapter code). This setup worked remarkably well and we never had a memory corruption bug in the Rust or the Elixir code and the performance was really good.</p>
<p>All in all, I’m not convinced bringing such  change upstream would have been a good idea under the given circumstances and it would absolutely have been much, <em>much</em> more work. At least for me. <img src="https://forum.elixirforum.com/images/emoji/apple/smiley.png?v=15" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="296042" data-batch-url="/posts/batch_likers">
                        0
                      </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/rustler-return-and-pass-reference-from-rust-to-elixir-and-back-to-rust/37429/48">Post #47</a>
	                </div>
	            </div>
              <div id="likers-container-296042" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="296042"
                     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>