All Projects → felipenoris → JuliaPackageWithRustDep.jl

felipenoris / JuliaPackageWithRustDep.jl

Licence: MIT license
Example of a Julia Package with Rust dependency.

Programming Languages

julia
2034 projects

Projects that are alternatives of or similar to JuliaPackageWithRustDep.jl

Oracle.jl
Oracle Database driver for the Julia language.
Stars: ✭ 32 (-50.77%)
Mutual labels:  julia-language
Suppressor.jl
Julia macros for suppressing and/or capturing output (STDOUT), warnings (STDERR) or both streams at the same time.
Stars: ✭ 94 (+44.62%)
Mutual labels:  julia-language
DICOM.jl
Julia package for reading and writing DICOM (Digital Imaging and Communications in Medicine) files
Stars: ✭ 45 (-30.77%)
Mutual labels:  julia-language
Acorn.jl
A pure julia text editor
Stars: ✭ 41 (-36.92%)
Mutual labels:  julia-language
YOLO.jl
YOLO Object Detection in Julia
Stars: ✭ 41 (-36.92%)
Mutual labels:  julia-language
NaquadahBrowser
A web browser built in Julia Language from the ground up.
Stars: ✭ 31 (-52.31%)
Mutual labels:  julia-language
DataScienceTutorials.jl
A set of tutorials to show how to use Julia for data science (DataFrames, MLJ, ...)
Stars: ✭ 94 (+44.62%)
Mutual labels:  julia-language
XUnit.jl
XUnit.jl is a unit-testing framework for Julia.
Stars: ✭ 32 (-50.77%)
Mutual labels:  julia-language
Scientific-Programming-in-Julia
Repository for B0M36SPJ
Stars: ✭ 32 (-50.77%)
Mutual labels:  julia-language
C3T
C3T: Crash Course Category Theory - A friendly non-mathematician's approach to beginners of Category Theory. 🐱
Stars: ✭ 26 (-60%)
Mutual labels:  julia-language
Julia-data-science
Data science and numerical computing with Julia
Stars: ✭ 54 (-16.92%)
Mutual labels:  julia-language
jill.py
A cross-platform installer for the Julia programming language
Stars: ✭ 202 (+210.77%)
Mutual labels:  julia-language
KissABC.jl
Pure julia implementation of Multiple Affine Invariant Sampling for efficient Approximate Bayesian Computation
Stars: ✭ 28 (-56.92%)
Mutual labels:  julia-language
crfsuite-rs
Rust binding to crfsuite
Stars: ✭ 19 (-70.77%)
Mutual labels:  rust-ffi
RobustModels.jl
A Julia package for robust regressions using M-estimators and quantile regressions
Stars: ✭ 18 (-72.31%)
Mutual labels:  julia-language
Expronicon.jl
Collective tools for metaprogramming on Julia Expr
Stars: ✭ 36 (-44.62%)
Mutual labels:  julia-language
OMETIFF.jl
I/O operations for OME-TIFF files in Julia
Stars: ✭ 18 (-72.31%)
Mutual labels:  julia-language
openBF
1D blood flow model
Stars: ✭ 16 (-75.38%)
Mutual labels:  julia-language
CausalityTools.jl
Algorithms for causal inference and the detection of dynamical coupling from time series, and for approximation of the transfer operator and invariant measures.
Stars: ✭ 45 (-30.77%)
Mutual labels:  julia-language
LatticeQCD.jl
A native Julia code for lattice QCD with dynamical fermions in 4 dimension.
Stars: ✭ 85 (+30.77%)
Mutual labels:  julia-language

JuliaPackageWithRustDep.jl

License travis

This is a set of examples on how to embed a Rust library in a Julia package. Interfacing between Julia and Rust library is done using Rust's FFI: the Rust library is exposed as a C dynamic library, and Julia will call Rust functions using ccall.

The build script deps/build.jl uses cargo to build the Rust library deps/RustDylib. Julia bindings to the Rust API are implemented in src/api.jl file.

If the Rust library build is successful during Pkg.build, the file deps/deps.jl is generated, and the package __init__ function will call check_deps to check if the Rust dynamic library is callable. This follows the same convention used by BinaryProvider.jl.

Requirements

  • Julia v1.0

  • Rust Stable

Installation

julia> using Pkg

julia> pkg"add https://github.com/felipenoris/JuliaPackageWithRustDep.jl.git"

julia> Pkg.test("JuliaPackageWithRustDep")

Primitive Type Correspondences

Julia Rust
Int32 i32
Int64 i64
Int64 i64
Float32 f32
Float64 f64
Bool bool

Passing a Julia Owned String to Rust

A Julia String is converted to a Cstring and passed to Rust, which will receive it as a pointer to char.

function rustdylib_inspect_string(s::String)
    ccall((:rustdylib_inspect_string, librustdylib), Cvoid, (Cstring,), s)
end

In Rust, the pointer to thar *const c_char is converted to a CStr, which is a reference to a C String. From a CStr, you can convert it to a regular &str.

use std::ffi::CStr;
use std::os::raw::c_char;

#[no_mangle]
pub extern fn rustdylib_inspect_string(cstring: *const c_char) {
    let cstr = unsafe { CStr::from_ptr(cstring) };

    match cstr.to_str() {
        Ok(s) => {
            // `s` is a regular `&str`
            println!("Rust read `{:?}`.", s);
        }
        Err(_) => {
            panic!("Couldn't convert foreign Cstring to &str.");
        }
    }
}

Returning a Rust Owned String to Julia

In this example, the Rust generates a owned string with rustdylib_generate_rust_owned_string and the ownership is transfered to the Julia process.

After being consumed, the Julia process must transfer the ownership back to Rust by calling rustdylib_free_rust_owned_string, to let the memory be freed.

use std::ffi::CString;
use std::os::raw::c_char;

#[no_mangle]
pub extern fn rustdylib_generate_rust_owned_string() -> *mut c_char {
    let rust_string = String::from("The bomb: 💣");
    let cstring = CString::new(rust_string).unwrap();
    cstring.into_raw() // transfers ownership to the Julia process
}

#[no_mangle]
pub unsafe extern fn rustdylib_free_rust_owned_string(s: *mut c_char) {
    if !s.is_null() {
        drop(CString::from_raw(s)) // retakes ownership of the CString and drop
    }
}

In the Julia process, the pointer to string is copied to a new String instance using unsafe_string function. Then, Julia asks Rust to free the string.

function rustdylib_free_rust_owned_string(s::Cstring)
    ccall((:rustdylib_free_rust_owned_string, librustdylib), Cvoid, (Cstring,), s)
end

function rustdylib_generate_rust_owned_string()
    ccall((:rustdylib_generate_rust_owned_string, librustdylib), Cstring, ())
end

function read_rust_owned_string() :: String
    cstring = rustdylib_generate_rust_owned_string()
    result = unsafe_string(cstring) # copies the contents of the string
    rustdylib_free_rust_owned_string(cstring) # ask Rust to free the memory
    return result
end

Resources

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].