<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Rustler使用 Rust 編寫 Erlang NIF 庫

          聯(lián)合創(chuàng)作 · 2023-09-28 11:16

          Rustler 實現(xiàn)了用安全的 Rust 代碼編寫 Erlang NIF 的庫。 這意味著應該沒有辦法讓BEAM(Erlang VM)崩潰。 該庫提供了用于生成樣板的工具,用于與BEAM交互,處理Erlang術語的編碼和解碼,并在它們展開到C之前捕獲 Rust  錯誤。

          Erlang 的 NIF(Native Implemented Function)被用來擴展erlang的某些功能,一般用來實現(xiàn)一些erlang很難實現(xiàn)的,或者一些erlang實現(xiàn)效率不高的功能。

          NIF使用C開發(fā),效率和C接近,比純erlang實現(xiàn)要高。NIF會編譯成動態(tài)庫,直接動態(tài)加載到erlang進程空間調(diào)用,也是erlang擴展新方法最高效的做法。調(diào)用NIF不用上下文的切換開銷,但是也有代價,NIF的crash會導致整個Erlang進程crash。

          該庫提供了 Erlang 和 Elixir 的功能,但目前 Elixir 更受歡迎。

          特性:

          • 安全 - The code you write in a Rust NIF should never be able to crash the BEAM.
          • 互操作 - Decoding and encoding rust values into Erlang terms is as easy as a function call.
          • 類型組合 - Making a Rust struct encodable and decodable to Erlang or Elixir can be done with a single attribute.
          • 資源對象 - Enables you to safely pass a reference to a Rust struct into Erlang code. The struct will be automatically dropped when it's no longer referenced.

          開始使用

          最簡單的入門是使用 rustler elixir library.

          注意: 如果你以前用過 Rustler, 你需要先運行 mix archive.uninstall rustler_installer.ez 來移除之前生成的 NIF。

          示例代碼:

          use rustler::{Encoder, Env, Error, Term};
          
          mod atoms {
              rustler::rustler_atoms! {
                  atom ok;
              }
          }
          
          rustler::rustler_export_nifs!(
              "Elixir.Math",
              [
                  ("add", 2, add)
              ],
              None
          );
          
          fn add<'a>(env: Env<'a>, args: &[Term<'a>]) -> Result<Term<'a>, Error> {
              let a: i64 = args[0].decode()?;
              let b: i64 = args[1].decode()?;
          
              Ok((atoms::ok(), a + b).encode(env))
          }
          瀏覽 30
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          編輯 分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  人妻一卡二卡 | 欧美成人视频在线免费观看 | 黄色1级片a | 京熱大亂交无碼大亂交在线 | 国产精品天干天干 |