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.

Compiling Rust for .NET, using only tea and stubbornness

175 pointsby tbillingtonover 1 year ago

12 comments

akavelover 1 year ago
Tangentially related, I&#x27;ve written a barebones assembler for Android .apk files once (strictly speaking, the assembler is for .dex files, but it also comes with a set of tools to package and sign .apk files). It showed up to be surprisingly easy. I expected to stumble upon some blocker issue any time that would make it impossible for me to continue — but one just never materialized! It&#x27;s written mainly in Nim and provides enough primitives to allow creating Java &quot;stubs&quot; for native .so libraries, so that .apk-s can be built in Nim WITHOUT JDK AT ALL. The Android NDK is still kinda needed&#x2F;useful, though IIRC mainly for access to <i>adb</i>, and especially <i>adb logcat</i> (which you&#x27;ll need A LOT for debugging if you try to use this contraption).<p>I&#x27;d love to One Day™ <i>Rewrite It In Rust</i>, so that we could write .apk-s purely using the Rust toolchain, just using a JNI library as appropriate, sprinkling the code with some proc-macro annotations where needed by the assembler (for stubs), and possibly adding some lines in a <i>build.rs</i> (for .apk packaging).<p>The .dex assembler itself is at: <a href="https:&#x2F;&#x2F;github.com&#x2F;akavel&#x2F;dali">https:&#x2F;&#x2F;github.com&#x2F;akavel&#x2F;dali</a> — you may like to check out the tests at: <a href="https:&#x2F;&#x2F;github.com&#x2F;akavel&#x2F;dali&#x2F;tree&#x2F;master&#x2F;tests">https:&#x2F;&#x2F;github.com&#x2F;akavel&#x2F;dali&#x2F;tree&#x2F;master&#x2F;tests</a> to see how using it looks like.<p>An example project with a simple .apk written purely in Nim (NO JDK) is at: <a href="https:&#x2F;&#x2F;github.com&#x2F;akavel&#x2F;hellomello&#x2F;tree&#x2F;flappy">https:&#x2F;&#x2F;github.com&#x2F;akavel&#x2F;hellomello&#x2F;tree&#x2F;flappy</a> (unfortunately, given Nim&#x27;s poor packaging story, it&#x27;s most probably already bitrotten to the extent that it can&#x27;t be quickly and easily built &amp; used out of the box). I recorded a presentation about this for an online Nim conference — see: <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=wr9X5NCwPlI&amp;list=PLxLdEZg8DRwTIEzUpfaIcBqhsj09mLWHx&amp;index=11&amp;t=0s">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=wr9X5NCwPlI&amp;list=PLxLdEZg8DR...</a>
评论 #37306190 未加载
pharmakomover 1 year ago
Article is a little wrong about the current state-of-the-art in writing Rust bindings for .NET.<p>One can use Uniffi with the C# generator to get fairly automatic bindings. You still need to package it up, which is a bit of a pain.<p>Uniffi really is an awesome idea. I expect more and more Rust code for foundational shared libraries as a result.
评论 #37304363 未加载
评论 #37305024 未加载
评论 #37310523 未加载
athrunover 1 year ago
Looks like the OP is still in high school. Kudos to them for pushing through, and having fun with compiler internals.<p>I wish I had this level of dedication at that age...
评论 #37307375 未加载
mrweaselover 1 year ago
If one was to do this as something other than a &quot;fun project&quot;, wouldn&#x27;t it make more sense to do a CIL backend for LLVM? That way any language utilizing LLVM would get be able to target .NET, or am I completely misunderstanding how rustc and LLVM works?
评论 #37305819 未加载
评论 #37311586 未加载
评论 #37317124 未加载
leowbattleover 1 year ago
I wonder if it could run on this Rust implementation of the CLR I wrote a few years ago: <a href="https:&#x2F;&#x2F;github.com&#x2F;Leowbattle&#x2F;clr_lite">https:&#x2F;&#x2F;github.com&#x2F;Leowbattle&#x2F;clr_lite</a>
评论 #37310542 未加载
Nelkinsover 1 year ago
Cool project. I remember someone else taking a crack at this a few years ago.<p><a href="https:&#x2F;&#x2F;ericsink.com&#x2F;entries&#x2F;dotnet_rust.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;ericsink.com&#x2F;entries&#x2F;dotnet_rust.html</a><p><a href="https:&#x2F;&#x2F;ericsink.com&#x2F;entries&#x2F;sg_rust_dotnet_preview.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;ericsink.com&#x2F;entries&#x2F;sg_rust_dotnet_preview.html</a><p><a href="https:&#x2F;&#x2F;ericsink.com&#x2F;entries&#x2F;lousygrep.html" rel="nofollow noreferrer">https:&#x2F;&#x2F;ericsink.com&#x2F;entries&#x2F;lousygrep.html</a>
ComputerGuruover 1 year ago
If it’s any consolation, Microsoft itself has shipped production assemblies generated with bad IL (for the auto-generated bindings&#x2F;interop between the Windows 10 SDKs and C#)! Code appeared fine and would run OK until you tried to either R2R or AOT a project depending on that DLL (and it was just a single entry point that was mangled, iirc).
lafar6503over 1 year ago
Beware, the rustification has broken thru .net defenses :) Not sure if I ever find use for Rust in .net runtime (C# has more or less same capabilities), but congrats anyway. However I&#x27;d gladly welcome some lightweight compiled language with easy and powerful meta-programming and AST transformation capabilities.
评论 #37306053 未加载
评论 #37306066 未加载
评论 #37310585 未加载
franksterover 1 year ago
I thougth to myself that surely there would be a CIL backend for LLVM, and why didn&#x27;t the author just use it? But amazingly there doesn&#x27;t seem to be.
评论 #37310782 未加载
评论 #37320464 未加载
e4m2over 1 year ago
From the linked GitHub repo:<p>&gt; As for the heap allocated objects, they will be allocated from unmanged(non-GC) memory, and will be allocated&#x2F;freed exactly like in Rust.<p>I understand this decision, but it would also be interesting to see a version of this that hijacks the global allocator and the alloc types to use the GC instead (while still allowing you to opt-out and use unmanaged memory).<p>Good work nonetheless!
评论 #37306009 未加载
nurettinover 1 year ago
TL;DR<p>&gt; Besides, we don&#x27;t want to be worse than the C++ people, do we ;)?
SkillColossusover 1 year ago
Rust for .NET, I did not believe it ha