Hi, I had the bright idea to integrate Crystal with Rust, which is more less worked out, but there is one thing which does not seem to work, Arrays.
Since Rust can compile to C compatible code, like this
#[no_mangle]
pub extern "C" fn rust_function() {
println!("Hello from Rust");
}
#[no_mangle]
pub extern "C" fn rust_add(a: i32, b: i32) -> i32 {
a + b
}
#[no_mangle]
pub extern "C" fn rust_sum(a: Vec<i32>) -> i32 {
let mut sum = 0;
for item in a {
sum += item;
}
sum
}
and Crystal can load C libs like this
module Rshost
VERSION = "0.1.0"
@[Link(ldflags: "#{__DIR__}/../../embed/target/release/libembed.so")]
lib MyDylib
fun rust_function : Void
fun rust_add(a: Int32, b: Int32) : Int32
fun rust_sum(a: Pointer(Int32)) : Int32
end
MyDylib.rust_function
puts MyDylib.rust_add(2, 3)
puts MyDylib.rust_sum([1,2])
end
the only problem is with the last function the rust_sum is not working as expected, if I mean expected to work at all :D The error what I’m getting is the following
It won’t work this way. Vector in Rust is a much more complex construct than just pointer. Code on a Rust side should take not a vector, but a raw pointer and number of elements, then (unsafely) construct a vector from them.
Well, I don’t know Rust. Maybe you should construct slice not vector, and maybe it’s not unsafe as long as calling contract holds.
But general idea is that you should pass pointer and size.
yes, after some digging around, you are probably right, I came to the same conclusion too. I will try it out, when I have some free time, and probably try to include in a Python code, I’m more proficient with that, thanks for the help
The answer is in my previous message and in your question.
It would have been practical and fun to develop in Crystal some time-consuming parts in Rust
…
higher abstraction to extend a lower one
Also, because I want learn Crystal not Lua…
Crystal is statically typed, compiled and not a scripting langage like Lua. Using Crystal to save time on certain features (ORM, data processing, …) would have been a good entry point (without too much performance penalty) .
Because you have some time to loss. You arg, I answer you only by politeness…
It was not for Rust that I intended to do that, just for the convenience of developing certain features in Crystal and to learn Crystal in an existing project in Rust.
anykeyh already answered my question, so I dropped this idea.
On the higher safety provided by rust I agree. But performance wise, there should not be much difference between Crystal and Rust. They both heavily rely an optimizations provided by LLVM. Do you have any supporting evidence that Rust could provide a relevant speed improvement?
Anyways, regarding the original question: Technically, you can build C libraries with Crystal and link them from other languages. From the language itself, this is not an issue. The practical problems come from the standard library, which is built around a global runtime providing fibers, GC etc. which might conflict with other runtimes when it’s only used as a library.