TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Neat Rust Tricks: Passing Closures to C

215 pointsby rabidferretover 5 years ago

8 comments

thenewwazooover 5 years ago
This is a really well-written article about a common way to trampoline Rust methods into C embedded RTOS tasks, which often work in exactly this way. I've done just this in the service of getting ChibiOS and uC/OS II running Rust code.
cheezover 5 years ago
Is this a neat trick or just standard operating procedure for calling C from &lt;your favorite lang&gt;? As it was billed as a trick, I was expecting some sort of runtime code generation to pass the data pointer and some jump instruction to jump to the right spot and unpack the data pointer.<p>Maybe I just overcomplicate things ;-)
评论 #21633412 未加载
评论 #21636496 未加载
评论 #21633327 未加载
KenanSulaymanover 5 years ago
Interestingly this is very similar to how I implemented passing closures into JavaScriptCore as hooks for JS class invocations (&quot;function calls&quot;). [0]<p>Essentially it&#x27;s taking advantage of the fact that closures are static methods with &quot;implicit&quot; data pointers. It should be fairly obvious that this is a massive violation of safety and undefined behavior, and most likely to break when debugging symbols etc. are inserted.<p>The safest way to do this until Rust has figured out a stable-enough-ABI for closure passing would be a thread-local trampoline, I guess. Not very nice..<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;psychonautwiki&#x2F;rust-ul&#x2F;blob&#x2F;master&#x2F;src&#x2F;helpers_internal.rs#L67-L118" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;psychonautwiki&#x2F;rust-ul&#x2F;blob&#x2F;master&#x2F;src&#x2F;he...</a>
评论 #21635830 未加载
评论 #21639680 未加载
kazinatorover 5 years ago
You can pass closures to C as C functions in TXR Lisp, a language I created.<p>Example:<p><a href="http:&#x2F;&#x2F;rosettacode.org&#x2F;wiki&#x2F;Window_creation#Win32.2FWin64" rel="nofollow">http:&#x2F;&#x2F;rosettacode.org&#x2F;wiki&#x2F;Window_creation#Win32.2FWin64</a><p>In this program, a translation of Microsoft&#x27;s &quot;Your First Windows Program&quot; from MSDN, <i>defun</i> is used to define a WindowsProc callback. <i>defun</i> generates a <i>lambda</i> under the hood, which carries a lexical scope.<p>The lambda is passed directly to Win32 as a callback, which is nicely called for repainting the window. (Or at least, things appear that way to the programmer.)<p>Setting this up requires a few steps. We need a target function, of course, which can be any callable object.<p>Then there is this incantation:<p><pre><code> (deffi-cb wndproc-fn LRESULT (HWND UINT LPARAM WPARAM)) </code></pre> The <i>deffi-cb</i> operator takes a name and some type specifications: return type and parameters. The name is defined as a function; so here we get a function called <i>wndproc-fn</i>. This function is a converter. If we pass it a Lisp function, it gives back a FFI closure object.<p>Then in the program, we instantiate this closure object, and stick it into the WNDPROC structure as required by the Windows API. Here we use the above wndproc-fn converter to obtain WindowProc in the shape of a FFI closure:<p><pre><code> (let* ((hInstance (GetModuleHandle nil)) (wc (new WNDCLASS lpfnWndProc [wndproc-fn WindowProc] ... </code></pre> The lpfnWndProc member of the WNDCLASS FFI structure is defined as having the FFI type <i>closure</i>; that will correspond to a function pointer on the C side. The rest is just Windows:<p><pre><code> (RegisterClass wc) </code></pre> register the class, and then CreateWindow with that class by name and so on.
评论 #21633289 未加载
jgtroshover 5 years ago
How does this relate to nested functions in C? (And resulting “infectious executable stacks”?)<p><a href="https:&#x2F;&#x2F;nullprogram.com&#x2F;blog&#x2F;2019&#x2F;11&#x2F;15&#x2F;" rel="nofollow">https:&#x2F;&#x2F;nullprogram.com&#x2F;blog&#x2F;2019&#x2F;11&#x2F;15&#x2F;</a>
评论 #21633029 未加载
评论 #21633069 未加载
评论 #21633073 未加载
评论 #21633025 未加载
评论 #21633109 未加载
评论 #21635433 未加载
tedunangstover 5 years ago
Now call qsort with a closure.
评论 #21634764 未加载
评论 #21633478 未加载
评论 #21634431 未加载
评论 #21636536 未加载
评论 #21635429 未加载
评论 #21633438 未加载
评论 #21633679 未加载
评论 #21645877 未加载
评论 #21633459 未加载
dmitrygrover 5 years ago
&gt; If you’re not familiar with C’s syntax, here’s the equivalent signature in Rust<p>Author is hilarious. Who is familiar with that but not c?
评论 #21636580 未加载
jonny383over 5 years ago
Rust is already doomed. The amount of literature being published about either comparisons or compatibility with C is a strong indicator C is here to stay.
评论 #21634519 未加载
评论 #21636592 未加载
评论 #21637735 未加载