From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Zhu Zihao Newsgroups: gmane.emacs.devel 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 Message-ID: <86czyrlubu.fsf@163.com> References: <87o8ivumn5.fsf@telefonica.net> <83sg7xrgr5.fsf@gnu.org> <83h7odrdwy.fsf@gnu.org> <86sg7w39fh.fsf@163.com> <83pn30pku5.fsf@gnu.org> <86wnx8otoj.fsf@163.com> <834kkbp9vr.fsf@gnu.org> <87czyxuxw6.fsf@db48x.net> <87v9cosv0b.fsf@db48x.net> <877dp3skh8.fsf@db48x.net> <86h7o68iev.fsf@163.com> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="=-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10324"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.4.13; emacs 27.1 Cc: db48x@db48x.net, dimech@gmx.com, abrochard@gmx.com, bugs@gnu.support, emacs-devel@gnu.org To: rms@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Dec 30 06:47:59 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kuUL1-0002YY-Dp for ged-emacs-devel@m.gmane-mx.org; Wed, 30 Dec 2020 06:47:59 +0100 Original-Received: from localhost ([::1]:51642 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kuUL0-00036p-FW for ged-emacs-devel@m.gmane-mx.org; Wed, 30 Dec 2020 00:47:58 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50274) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kuUK9-0002fa-Na for emacs-devel@gnu.org; Wed, 30 Dec 2020 00:47:05 -0500 Original-Received: from mail-m972.mail.163.com ([123.126.97.2]:41066) by eggs.gnu.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1kuUK2-0006CR-LS; Wed, 30 Dec 2020 00:47:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version; bh=TQJ8G pdd9AkGNV3kTFaUCSM3P48PWDsGKPeE5FA17bo=; b=AI9hzkhW0hxlhr9eKCVDB Hgt2qAktI5m8Ox9tbV7iMBL9LxPfitd0DACTm3NhO6jxSlMBbsF+xdlqxHDMhvtD P4fXFmZmfOpTU2dI6QGrQ1tbvi4tfnp1ClrQdGUr06VGYmXmlBOO22leqoAih1u4 6O7TLHk+WaIlI7TVO1yoUo= Original-Received: from asus-laptop (unknown [27.39.88.125]) by smtp2 (Coremail) with SMTP id GtxpCgAH7XY5FOxfx86UHA--.857S2; Wed, 30 Dec 2020 13:46:33 +0800 (CST) In-reply-to: X-CM-TRANSID: GtxpCgAH7XY5FOxfx86UHA--.857S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxXrWrZF18CFyDJr18Zw4fXwb_yoW5urW7pr 4rGrWj9r18Z3yxArZ3Jay09FyfKas5GFWUJay8K34qyw1qgr9Igr4akFW3C3yDCrn3Ar12 qrn09rWUGF40yFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jD3kZUUUUU= X-Originating-IP: [27.39.88.125] X-CM-SenderInfo: pdoosuxxwbztlvw6il2tof0z/xtbCCgsLr12MZakU7AAAsA Received-SPF: pass client-ip=123.126.97.2; envelope-from=all_but_last@163.com; helo=mail-m972.mail.163.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:262106 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 =3D ["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 Ma= kefile to automate these steps. ``` chino@asus-laptop:/archive/gitrepos/jieba-rs-c-binding-example$ LANG=3Den_U= S.utf8 make make -C jieba_rustlib libjieba_rustlib.so make[1]: Entering directory '/archive/gitrepos/jieba-rs-c-binding-example/j= ieba_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/ji= eba_rustlib' gcc main.c -Ijieba_rustlib -Ljieba_rustlib -Wl,-rpath=3Djieba_rustlib -ljie= ba_rustlib -o main chino@asus-laptop:/archive/gitrepos/jieba-rs-c-binding-example$ ./main=20 =E6=88=91 =E5=8F=AF=E4=BB=A5 =E5=90=9E=E4=B8=8B =E7=8E=BB=E7=92=83 =E8=80=8C =E4=B8=8D=E4=BC=A4 =E8=BA=AB=E4=BD=93 ``` > 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). =2D-=20 Retrieve my PGP public key: gpg --recv-keys D47A9C8B2AE3905B563D9135BE42B352A9F6821F Zihao --=-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iIsEARYIADMWIQTUepyLKuOQW1Y9kTW+QrNSqfaCHwUCX+wUNRUcYWxsX2J1dF9s YXN0QDE2My5jb20ACgkQvkKzUqn2gh+iKAEApZ7AuHFiD3GgwXWAFwm7Zs4lJBE+ C6Ygw0jJWVrMJT8A/j8f0OjrnQ2XJUZNnKT27ffbMrF8gP14aFJb0jzVgIYF =nrNc -----END PGP SIGNATURE----- --=-=-=--