mrusty mruby 对 Rust 的安全约束开源项目

我要开发同款
诺克萨斯2016年03月04日
45阅读
开发技术Rust
所属分类其他开发相关、程序开发
授权协议MPL-2.0

作品详情

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".
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论