dragonlooki.blogg.se

Rust any downcast
Rust any downcast













rust any downcast
  1. RUST ANY DOWNCAST HOW TO
  2. RUST ANY DOWNCAST CODE

Thankfully, there is sbt-jni, a suite of sbt plugins, libraries and macros, originally developed by Jakob Odersky, that make using JNI from Scala easier (helps with boilerplate on Scala side, boilerplate on the native side is still there). Scala, as such, has support for using JNI, using the annotation, but it requires a lot of boilerplate code.

rust any downcast rust any downcast

I wanted to change that – this very post is part of the effort. But it seemed like calling Rust from Scala hadn’t been done before. There are also few resources and tools available for calling Rust from Java, or C from Scala.

RUST ANY DOWNCAST CODE

But the vast majority of articles or tutorials about JNI focus on calling C code from Java. But it is also in the experimental phase of its life and, more importantly, by its nature the code doesn’t run on JVM, so it’s not relevant for our use case.īecause JNI is the official and most widely used option, with the benefits like most resources available online, easy googlability of issues, etc, I decided to explore it further. Of honorable mention is Scala Native, which compiles Scala to native code (in the same manner as is typical for Go or Haskell or Rust for that matter), allowing for direct use of native libraries.(Check out SLinC to use it from Scala: GitHub repository and an introductory blogpost.) The OpenJDK project Panama attempts to make it significantly easier to interoperate with native code, but is still in the experimental stage.JNA ( Java Native Access) is relatively easier on programmers, but is slower, because it relies on dynamic behavior.It is fast, but a bit clumsy, it requires programmers to write a lot of boilerplate interoperability code. JNI ( Java Native Interface) is the official way.

RUST ANY DOWNCAST HOW TO

On the Java Virtual Machine – since we run Scala on JVM – we have a couple of options of how to call native code. But can we have one application written in both languages? Using native code from JVM We already have applications written in Scala, and some written in Rust, depending on which technology was better suited for the job. Thus, these two languages complement each other very well. Whereas Scala is very high level, with a powerful type system and sits on the battle-tested JVM, Rust allows you to control low level details, enabling great CPU and/or memory efficiency without sacrificing safety. Both are very powerful languages in their own regard, each with unique strengths (and also weaknesses). If you want to dive straight into example code, here’s the self contained example project. It explains how we can use Rust code from Scala (on JVM) via JNI with the help of sbt-jni. It’s based on work I did and a presentation I gave at our Scala guild meeting a few months ago. This is a post that I’ve been meaning to write already for some time.















Rust any downcast