[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
The posibility to use Rust libraries with GNU Project softwares(e.g. lin
From: |
Zhu Zihao |
Subject: |
The posibility to use Rust libraries with GNU Project softwares(e.g. link with Rust library) |
Date: |
Wed, 30 Dec 2020 13:46:29 +0800 |
User-agent: |
mu4e 1.4.13; emacs 27.1 |
Richard Stallman writes:
> I am interested in understanding what that means. Could you describe
> in 10-20 lines what it means? What is the input, what is the output,
> and what software does the conversion?
Just create a small example to help you understand how Rust interact
with C. link here: https://github.com/cireu/jieba-cbinding-example
jieba-rs is a Chinese segmentation crate(crate means library). CJK
languages usually don't use space to separate words so somebody create a
library to do it.
Please see jieba_rustlib/Cargo.toml, we have `crate-type = ["cdylib"]` in
lib section, so cargo(the builder of Rust code, like make) will ask (rustc)rust
compiler to generate C dynamic libraries.
I write a simple interface in jieba_rustlib/src/lib.rs. See the function
with `unsafe extern "C"` and `#[no_magle]` mark, it's marked for FFI to
C.
There're several ways to expose Rust API to C. Some simple types(int,
uint) will have corresponding mappings. For complex type like struct, we
can use #[repr(C)], make the struct layout compatible with C, so C can
access struct directly. Or just use pointer, let C treat Rust struct as
opaque object and use exported function to use it.
I use both in this example, for Jieba struct(introduced by jieba-rs
crate), I use pointer, for hand-craft compat layer of Rust dynamic array
and C arrays, I use C-compatiable struct to expose extra
information(length and capacity) of array to C.
And we use cbindgen(https://github.com/eqrion/cbindgen) to generate C
header, and result in jieba_rustlib/jieba_rustlib.h.
Finally we have dynamic library and header, we just include header in c
source(see main.c) and link with library to craft our application. I use
Makefile to
automate these steps.
```
chino@asus-laptop:/archive/gitrepos/jieba-rs-c-binding-example$ LANG=en_US.utf8
make
make -C jieba_rustlib libjieba_rustlib.so
make[1]: Entering directory
'/archive/gitrepos/jieba-rs-c-binding-example/jieba_rustlib'
cargo build --release
Finished release [optimized] target(s) in 0.01s
cp target/release/libjieba_rustlib.so ./
make[1]: Leaving directory
'/archive/gitrepos/jieba-rs-c-binding-example/jieba_rustlib'
gcc main.c -Ijieba_rustlib -Ljieba_rustlib -Wl,-rpath=jieba_rustlib
-ljieba_rustlib -o main
chino@asus-laptop:/archive/gitrepos/jieba-rs-c-binding-example$ ./main
我
可以
吞下
玻璃
而
不伤
身体
```
> From what I hear, Rust has a fundamental practical flaw: it is not
> intended to be stable. The developers want to keep changing it.
I think Rust will keep evolving. But not aggressively.
The latest Rust stable version is 1.48. When a software/library
released it's 1.0 version, usually means it's production ready.
Cargo(package manager and build system for Rust) have lock. Users can
lock their crates to ensure a reproducible build.
And Rust also introduce "Edition" for breaking
changes(https://doc.rust-lang.org/edition-guide/editions/index.html).
It's stable if user stick to a specific edition. Any updates in same
edition should not break your code failed to compile/failed to run(if
so, it's probably a compiler bug).
--
Retrieve my PGP public key:
gpg --recv-keys D47A9C8B2AE3905B563D9135BE42B352A9F6821F
Zihao
signature.asc
Description: PGP signature
- Re: Internationalize Emacs's messages (swahili), (continued)
- Re: Internationalize Emacs's messages (swahili), Daniel Brooks, 2020/12/27
- Re: Internationalize Emacs's messages (swahili), Richard Stallman, 2020/12/28
- Re: making gettext more like fluent, Daniel Brooks, 2020/12/28
- Re: making gettext more like fluent, Richard Stallman, 2020/12/29
- Re: making gettext more like fluent, Daniel Brooks, 2020/12/29
- Re: making gettext more like fluent, Richard Stallman, 2020/12/30
- Re: Internationalize Emacs's messages (swahili), Zhu Zihao, 2020/12/28
- Re: Internationalize Emacs's messages (swahili), Richard Stallman, 2020/12/29
- Re: Internationalize Emacs's messages (swahili), Daniel Brooks, 2020/12/29
- Re: Internationalize Emacs's messages (swahili), Zhu Zihao, 2020/12/29
- The posibility to use Rust libraries with GNU Project softwares(e.g. link with Rust library),
Zhu Zihao <=
- Re: Internationalize Emacs's messages (swahili), Andy Moreton, 2020/12/30
- Rust trademark problems - Re: Internationalize Emacs's messages (swahili), Jean Louis, 2020/12/30
- Re: Emacs Survey: Toolbars, Eli Zaretskii, 2020/12/21
- Re: Emacs Survey: Toolbars, Christopher Dimech, 2020/12/21
- Re: Emacs Survey: Toolbars, Gregory Heytings, 2020/12/22
- Re: Emacs Survey: Toolbars, Daniel Martín, 2020/12/22
- Re: Emacs Survey: Toolbars, Tomas Hlavaty, 2020/12/23
- Re: Emacs Survey: Toolbars, Gregory Heytings, 2020/12/23
- Re: Emacs Survey: Toolbars, Tomas Hlavaty, 2020/12/23
- Re: Emacs Survey: Toolbars, Eli Zaretskii, 2020/12/23