mruby对Rust的安全约束 mrusty

码农软件 · 软件分类 · 其他开发相关 · 2019-10-18 18:11:25

软件介绍

mrusty 可以让你在 mruby 中使用 Rust 的结构和枚举并运行它们。

示例代码:

// mrfn!
#[macro_use]
extern crate mrusty;

// Needs some undocumented, hidden calls.
use mrusty::*;

let mruby = MRuby::new();

struct Cont {
    value: i32
}

// Cont should not flood the current namespace. We will add it with require.
impl MRubyFile for Cont {
    fn require(mruby: MRubyType) {
        mruby.def_class::<Cont>("Container");

        // Converts mruby types automatically & safely.
        // slf is always Value in initialize().
        mruby.def_method::<Cont, _>("initialize", mrfn!(|_mruby, slf: Value, v: i32| {
            let cont = Cont { value: v };

            slf.init(cont)
        }));
        mruby.def_method::<Cont, _>("value", mrfn!(|mruby, slf: Cont| {
            mruby.fixnum(slf.value)
        }));
    }
}

// Add file to the context, making it requirable.
mruby.def_file::<Cont>("cont");

// Add spec testing.
describe!(Cont, "
  context 'when containing 1' do
    it 'returns 1 when calling #value' do
      expect(Container.new(1).value).to eql 1
    end
  end
");

let result = mruby.run("
    require 'cont'

    Container.new(3).value
").unwrap(); // Returns Value.

println!("{}", result.to_i32().unwrap()); // Prints "3".

本文地址:https://codercto.com/soft/d/17045.html

妙趣横生的算法

妙趣横生的算法

杨峰 / 清华大学出版社 / 2010-4 / 49.00元

《妙趣横生的算法(C语言实现)》理论与实践相结合,旨在帮助读者理解算法,并提高C语言编程能力,培养读者的编程兴趣,并巩固已有的C语言知识。全书分为2个部分共10章,内容涵盖了编程必备的基础知识(如数据结构、常用算法等),编程实例介绍,常见算法和数据结构面试题等。《妙趣横生的算法(C语言实现)》最大的特色在于实例丰富,题材新颖有趣,实用性强,理论寓于实践之中。通过《妙趣横生的算法(C语言实现)》的学......一起来看看 《妙趣横生的算法》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具