mrustymruby 对 Rust 的安全约束
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".
评论
Gopher的Rust第一课:Rust的依赖管理
在上一章《Gopher的Rust第一课:Rust代码组织》中,我们了解了Rust的代码组织形式,知道了基于Cargo构建项目以及Rust代码组织是目前的标准方式,同时Cargo也是管理项目外部依赖的标准方法,而项目内部的代码组织则由Rust module来完成。在这一章中,我们将聚焦Rust的依赖管
GoCN
0