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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I was hitting the same error messages as others have shown here. I think the example project (this? <a href="http://github.com/rusterlium/NifIo" class="inline-onebox" rel="noopener nofollow ugc">GitHub - rusterlium/NifIo: Rustler example. Implements file IO in a NIF. · GitHub</a>) hasn’t been updated for the latest rustler version.</p>
<p><a class="mention" href="/u/mickel8" rel="nofollow">@mickel8</a> thanks for the example code, I was able to make some solid progress working forward from this.</p>
<p>I’m working on a table data structure for time-series analysis, and I have the basic parts working in rust, so now I’m working thru the integration process with elixir. If it helps anyone, you can see my code here.<br>
<a href="https://github.com/rm-rf-etc/time_series_sliding_window" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/rm-rf-etc/time_series_sliding_window</a></p>
<p>After line 115 I have the rustler parts, all it does currently is instantiate structs that hold a mutable float, and there’s functions for update, read, and add. I had to keep things simple at first. Next I’ll be figuring out how to make this work with my <code>SlidingWindow</code> data structure.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I can’t figure out how to make <code>rustler::resource!(SlidingWindow, env);</code> work if <code>SlidingWindow</code> includes a field which requires a named lifetime parameter, such as either <code>&amp;[&amp;str]</code> or <code>rustler::types::list::ListIterator</code>.</p>
<p>First rust says,</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">error[E0726]: implicit elided lifetime not allowed here
   --&gt; src/lib.rs:157:24
    |
157 |     rustler::resource!(SlidingWindow, env);
    |                        ^^^^^^^^^^^^^- help: indicate the anonymous lifetime: `&lt;'_&gt;`
</code></pre>
<p>So we try that and then it says,</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">error[E0478]: lifetime bound not satisfied
   --&gt; src/lib.rs:157:5
    |
157 |     rustler::resource!(SlidingWindow&lt;'_&gt;, env);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: lifetime parameter instantiated with the lifetime `'_` as defined on the impl at 157:38
   --&gt; src/lib.rs:157:38
    |
157 |     rustler::resource!(SlidingWindow&lt;'_&gt;, env);
    |                                      ^^
    = note: but lifetime parameter must outlive the static lifetime
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
</code></pre>
<p>That last part seems like a clue: “note: this error originates in a macro”. And it makes sense, rust wants to know how long this reference will live for, but we’re trying to store it in an erlang term. Does anybody know how to resolve this?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208421" 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/34">Post #33</a>
	                </div>
	            </div>
              <div id="likers-container-208421" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208421"
                     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 #33"></div>
  </section>
</div>
    <div class="postbit" id="208447" data-post-id="208447">
  <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>Did you do <code>impl Encoder</code> with lifetime annotations?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208447" 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/35">Post #34</a>
	                </div>
	            </div>
              <div id="likers-container-208447" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208447"
                     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 #34"></div>
  </section>
</div>
    <div class="postbit" id="208463" data-post-id="208463">
  <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>Success! <img src="https://forum.elixirforum.com/images/emoji/apple/tada.png?v=15" title=":tada:" class="emoji" alt=":tada:" loading="lazy" width="20" height="20"><br>
Thanks for sharing your solution <a class="mention" href="/u/mickel8" rel="nofollow">@mickel8</a> - that was really helpful. I missed the load function.</p>
<p>As a note to others: if you need to implement a custom decoder/encoder do <em>not</em> use the <code>NifStruct</code> proc macro. Otherwise you’ll get a “Conflicting implementations” error. NifStruct tries to implement those itself.</p>
<p>Last but not least one other thing: I had to wrap my connection reference with a Mutex. The ODBC connection in odbc-rs is not <code>Sync</code>able by default, and hence not threadsafe by itself.</p> 
	            </div>

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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I’m looking at the rustler source here: <a href="https://docs.rs/rustler/0.18.0/src/rustler/types/list.rs.html#126" class="inline-onebox" rel="noopener nofollow ugc">list.rs.html -- source</a></p>
<p>So the goal is to return my struct as a <code>Term</code>? Are there any examples of how to make a struct into a <code>Term</code> with a list field? I’m confused how these go together.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208534" 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/37">Post #36</a>
	                </div>
	            </div>
              <div id="likers-container-208534" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208534"
                     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 #36"></div>
  </section>
</div>
    <div class="postbit" id="208543" data-post-id="208543">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Looks like we have to implement <code>ResourceTypeProvider</code> in order to implement <code>Encoder</code>.</p>
<p>EDIT: Decided to seek some help from the rust community, <a href="https://users.rust-lang.org/t/help-with-elixir-nif-for-table-data-structure/57635" rel="noopener nofollow ugc">here</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="208543" 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/38">Post #37</a>
	                </div>
	            </div>
              <div id="likers-container-208543" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208543"
                     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 #37"></div>
  </section>
</div>
    <div class="postbit" id="208596" data-post-id="208596">
  <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>Sorry dude, waaaaaaaaay too busy lately, plus bad health. I’ll get back to you guys, I promise.</p>
<aside class="quote no-group" data-username="rm-rf-etc" data-post="38" data-topic="37429">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/r/71e660/48.png" class="avatar"> rm-rf-etc:</div>
<blockquote>
<p>Looks like we have to implement <code>ResourceTypeProvider</code> in order to implement <code>Encoder</code>.</p>
</blockquote>
</aside>
<p>This is done for you when you do <code>rustler::resource!(...)</code>. If it’s not then you have to do the same for another data structure before that as well.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208596" 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/39">Post #38</a>
	                </div>
	            </div>
              <div id="likers-container-208596" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208596"
                     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 #38"></div>
  </section>
</div>
    <div class="postbit" id="208656" data-post-id="208656">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Found a solution. I was using the wrong datatypes. It builds now, but it will take more work to optimize.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208656" 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/40">Post #39</a>
	                </div>
	            </div>
              <div id="likers-container-208656" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208656"
                     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 #39"></div>
  </section>
</div>
    <div class="postbit" id="208658" data-post-id="208658">
  <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>Can you share some more details?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208658" 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/41">Post #40</a>
	                </div>
	            </div>
              <div id="likers-container-208658" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208658"
                     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 #40"></div>
  </section>
</div>
    <div class="postbit" id="208861" data-post-id="208861">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Sure, sorry. I dropped details over in the thread in the rust forums.</p>
<p>The problem was the lifetimes. I needed to replace any datatype that requires a lifetime, with the equivalent datatype that doesn’t (e.g. <code>&amp;str</code> → <code>String</code>, and <code>&amp;[&amp;str]</code> → <code>Vec&lt;String&gt;</code>). I didn’t understand that lifetimes aren’t needed for what I’m doing. They’re only used at compile-time, so in my situation it doesn’t make sense to accept a lifetime parameter. For example, <code>&amp;str</code> requires a known length <em>at compile-time</em>, but my string values are dynamic length.</p>
<p>So instead of…</p>
<pre data-code-wrap="rust"><code class="lang-rust">struct SlidingWindow&lt;'a&gt; {
    map: HashMap&lt;&amp;'a str, Vec&lt;Option&lt;f32&gt;&gt;&gt;,
    labels: &amp;'a [&amp;'a str],
    index: usize,
    length: usize,
    width: usize,
}
</code></pre>
<p>…we need to use…</p>
<pre data-code-wrap="rust"><code class="lang-rust">struct SlidingWindow {
    map: HashMap&lt;String, Vec&lt;Option&lt;f32&gt;&gt;&gt;,
    labels: Vec&lt;String&gt;,
    index: usize,
    length: usize,
    width: usize,
}
</code></pre>
<p>And one other helpful thing. I realized you can package probably any mutable data in this generic structure:</p>
<pre data-code-wrap="rust"><code class="lang-rust">struct Container {
    mutex: Mutex&lt;MyStruct&gt;,
}

rustler::init!(
    "Elixir.MyNif",
    [func1, func2],
    load = |env: Env, _| {
        rustler::resource!(Container, env);
        true
    }
);
</code></pre>
<p>So you put whatever you want into <code>MyStruct</code>, and you wrap it up for elixir like this…</p>
<pre data-code-wrap="rust"><code class="lang-rust">let my_new_struct = MyStruct { ... };
let mutex = Mutex::new(my_new_struct);
let container = Container { mutex };
Ok(ResourceArc::new(container))
</code></pre>
<p>…then you can receive the same thing back, lock the mutex, unpack it, and do whatever mutations you need.</p>
<pre data-code-wrap="rust"><code class="lang-rust">#[rustler::nif]
fn print(arc: ResourceArc&lt;Container&gt;) {
    arc.mutex.lock().unwrap().print();
}
</code></pre>
<p>I’m happy to say that I have everything working in my project, have a look at <code>master</code> if you’re interested.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="208861" 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/42">Post #41</a>
	                </div>
	            </div>
              <div id="likers-container-208861" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="208861"
                     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 #41"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <a class="load-more-button" data-turbo-stream="true" href="/topics/37429/load_more?page=5">Load more posts (5 remaining)</a>
</div></template></turbo-stream>