Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rust + Ruby/mruby

Rust + Ruby/mruby

Gosuke Miyashita

November 25, 2017
Tweet

More Decks by Gosuke Miyashita

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • ٶԼ ߶ี • ߹ಉձࣾServerspec Operations୅ද • ϑϦʔϥϯεͷιϑτ΢ΣΞΤϯδχΞ •

    http://mizzy.org/ • mizzy@github, gosukenator@twitter ෱ԬRubyձٞ02 2
  2. ΞδΣϯμ • RustͱFFI • ೖ໳Rust + Ruby/mruby • ࣮ફRust +

    Ruby/mruby • libspecinfraϓϩδΣΫτͷ֓ཁʢ͕࣌ؒ͋Ε͹ʣ • ※Rust౳ͷݴޠ࢓༷తͳ࿩͸͠·ͤΜ ෱ԬRubyձٞ02 6
  3. ଞݴޠͰॻ͔ΕͨϥΠϒϥϦؔ਺ΛݺͿ #[link(name = "snappy")] extern { fn snappy_max_compressed_length(source_length: size_t) ->

    size_t; } fn main() { let x = unsafe { snappy_max_compressed_length(100) }; println!("max compressed length of a 100 byte buffer: {}", x); } https://doc.rust-lang.org/book/first-edition/ffi.html#calling- foreign-functions ෱ԬRubyձٞ02 10
  4. ଞݴޠ͔Βؔ਺Λݺ΂ΔΑ͏ʹ͢Δ #[no_mangle] pub extern "C" fn hello_rust() -> *const u8

    { "Hello, world!\0".as_ptr() } https://doc.rust-lang.org/book/first-edition/ffi.html#calling-rust- code-from-c ෱ԬRubyձٞ02 11
  5. hello_rust()ΛC͔Βݺͼग़͢ #include <stdio.h> extern char *hello_rust(void); int main() { printf("%s\n",

    hello_rust()); } cc hello.c -L. -lhello LD_LIBRARY_PATH=. ./a.out Hello, world! ෱ԬRubyձٞ02 12
  6. RustͱFFI·ͱΊ • FFI = Foreign Function Interface • ͋Δϓϩάϥϛϯάݴޠ͔ΒଞͷϓϩάϥϛϯάݴޠͰఆٛ ͞Εͨؔ਺ͳͲΛར༻͢ΔͨΊͷػߏ

    • Rustʹ͸FFI͕૊Έࠐ·Ε͍ͯͯɺଞͷݴޠͰॻ͔ΕͨϥΠϒ ϥϦͷؔ਺Λݺͼग़ͨ͠Γɺଞͷݴޠ͔ΒRustͰॻ͍ͨϥΠ ϒϥϦͷؔ਺Λݺͼग़ͨ͠ΓͰ͖Δ ෱ԬRubyձٞ02 13
  7. ೖ໳Rust + Ruby/mruby • RustͱRuby/mrubyΛͲ͏ܨ͙͔۩ମతͳίʔυͰࣔ͠·͢ • ࣗ෼͕libspecinfraͷόΠϯσΟϯά։ൃͷͨΊʹֶΜͩͷͱಉ͡εςοϓΛḷ ͬͯΈ·͢ • จࣈΛେ͖ͯ͘͠εϥΠυΛݟ΍͘͢͢ΔͨΊίʔυΛলུͨ͠Γਪ঑͞Εͳ

    ͍ॻ͖ํΛͯ͠Δ෦෼΋͋Γ·͢ • ׬શͳίʔυ͸GitHubʹஔ͍ͯ͋Γ·͢ • https://github.com/mizzy/fukuokark02_code_examples • εϥΠυͱඍົʹҧ͏෦෼΋͋Γ·͕͢ ෱ԬRubyձٞ02 16
  8. ੔਺ͷ଍͠ࢉΛߦ͏Rustίʔυ extern crate libc; use libc::uint32_t; #[no_mangle] pub extern "C"

    fn addition(a: uint32_t, b: uint32_t) -> uint32_t { a + b } ෱ԬRubyձٞ02 18
  9. Rustͷؔ਺Λݺͼग़͢Rubyίʔυ require 'ffi' module Integers extend FFI::Library ffi_lib 'addition' attach_function

    :addition, [:uint32, :uint32], :uint32 end puts Integers.addition(1, 2) ෱ԬRubyձٞ02 19
  10. Rustͷؔ਺Λݺͼग़͢mrbgemίʔυ extern uint32_t addition(uint32_t, uint32_t); static mrb_value addition_(mrb_state *mrb, mrb_value

    self) { mrb_int a, b; mrb_get_args(mrb, "ii", &a, &b); return mrb_fixnum_value(addition(a, b)); } void mrb_mruby_addition_gem_init(mrb_state *mrb) { struct RClass *i = mrb_define_module(mrb, "Integers"); mrb_define_class_method( mrb, i, "addition", addition_, MRB_ARGS_REQ(2) ); } ෱ԬRubyձٞ02 20
  11. จࣈྻʹ!!!Λ෇Ճ͢ΔRustίʔυ pub extern "C" fn emphasize(ptr: *const c_char) -> *const

    c_char { let s = unsafe { assert!(!ptr.is_null()); CStr::from_ptr(ptr) }.to_str().unwrap(); let str = s.to_owned() + "!!!"; CString::new(str).unwrap().into_raw() } ෱ԬRubyձٞ02 23
  12. Rustͷؔ਺Λݺͼग़͢mrbgemίʔυ extern char *emphasize(char *); static mrb_value emphasize_(mrb_state *mrb, mrb_value

    self) { char *arg, *ret; mrb_int len; mrb_value str; mrb_get_args(mrb, "s", &arg, &len); ret = emphasize(arg); str = mrb_str_buf_new(mrb, sizeof(ret)); return mrb_str_cat_cstr(mrb, str, ret); } ෱ԬRubyձٞ02 25
  13. ߏ଄ମ • ͜Ε͸লུ • ҰԠॻ͖͔͚ͷίʔυ͸αϯϓϧϦϙδτϦʹ͋Γ·͢ • ҎԼͷهࣄ͕ࢀߟʹͳΓ·͢ • Ruby-FFIʹ͍ͭͯௐ΂ͯΈͨɻʢͦͷ2ʣ -

    ͍΋ͷ΍·ɻ • http://yamaimo.hatenablog.jp/entry/2015/05/22/200000 • mruby Ͱ C ݴޠͷߏ଄ମΛϥοϓͨ͠ΦϒδΣΫτΛ࡞Δਖ਼͍͠ํ๏ - Qiita • https://qiita.com/tsahara@github/items/86610a696f8ca792db45 ෱ԬRubyձٞ02 28
  14. SpecinfraΦϒδΣΫτͷఆٛ(Rust) pub struct Specinfra; impl Specinfra { pub fn new()

    -> Specinfra { Specinfra } pub fn file(self, name: &str) -> File { File { name: name } } } ෱ԬRubyձٞ02 35
  15. FileΦϒδΣΫτͷఆٛ(Rust) pub struct File<'a> { name: &'a str, } impl<'a>

    File<'a> { pub fn mode(self) -> i32 { // ύʔϛογϣϯΛऔಘͯ͠ฦ͢ॲཧ } } ෱ԬRubyձٞ02 36
  16. SpecinfraΦϒδΣΫτͷ֎෦༻ఆٛ(Rust) pub extern "C" fn specinfra_new() -> *const Specinfra {

    let s = Specinfra::new(); Box::into_raw(Box::new(s)) } pub extern "C" fn specinfra_file<'a>(ptr: *const Specinfra, name: *const c_char) -> *const File<'a> { let s = unsafe { &*ptr }; let name = unsafe { CStr::from_ptr(name) }; Box::into_raw(Box::new(s.file(name.to_str().unwrap()))) } ෱ԬRubyձٞ02 37
  17. FileΦϒδΣΫτͷ֎෦༻ఆٛ(Rust) pub extern "C" fn file_mode(ptr: *const File) -> int32_t

    { let f = unsafe { assert!(!ptr.is_null()); &*ptr }; f.mode() } ෱ԬRubyձٞ02 38
  18. ΦϒδΣΫτ։์༻ͷؔ਺(Rust) pub extern "C" fn specinfra_free(ptr: *mut Specinfra) { unsafe

    { Box::from_raw(ptr); } } pub extern "C" fn file_free(ptr: *mut File) { unsafe { Box::from_raw(ptr); } } ෱ԬRubyձٞ02 39
  19. SpecinfraΦϒδΣΫτ(Ruby) class Specinfra < FFI::AutoPointer def self.release(ptr) Binding.free(ptr) end def

    file(name) Binding.file(self, name) end module Binding attach_function :new, :specinfra_new, [], Specinfra attach_function :free, :specinfra_free, [Specinfra], :void attach_function :file, :specinfra_file, [Specinfra, :string], File end end ෱ԬRubyձٞ02 41
  20. FileΦϒδΣΫτ(Ruby) class File < FFI::AutoPointer def self.release(ptr) file_free(ptr) end def

    mode() file_mode(self) end attach_function :file_free, [File], :void attach_function :file_mode, [File], :int end ෱ԬRubyձٞ02 42
  21. FileΦϒδΣΫτؔ࿈ఆٛ(mrbgem) typedef int file; extern int file_mode(file *); extern void

    file_free(file *); struct mrb_data_type mrb_file_type = { "File", mrb_file_free }; ෱ԬRubyձٞ02 44
  22. SpecinfraΦϒδΣΫτؔ࿈ఆٛ(mrbgem) typedef int specinfra; extern specinfra *specinfra_new(void); extern file *specinfra_file(specinfra

    *, char *); extern void specinfra_free(specinfra *); struct mrb_data_type mrb_specinfra_type = { "Specinfra", mrb_specinfra_free }; ෱ԬRubyձٞ02 45
  23. Specinfra.new(mrbgem) mrb_value specinfra_new_(mrb_state *mrb, mrb_value self) { specinfra *s =

    specinfra_new(); DATA_TYPE(self) = &mrb_specinfra_type; DATA_PTR(self) = s; return self; } ෱ԬRubyձٞ02 46
  24. Specinfra#file(mrbgem) mrb_value specinfra_file_(mrb_state *mrb, mrb_value self) { mrb_value v; mrb_get_args(mrb,

    "S", &v); char *name = mrb_str_to_cstr(mrb, v); file *f = specinfra_file(DATA_PTR(self), name); struct RClass file_class = mrb_class_get(mrb, "File"); mrb_value file_object = mrb_obj_new(mrb, file_class, 0, NULL); DATA_TYPE(file_object) = &mrb_file_type; DATA_PTR(file_object) = f; return file_object; } ෱ԬRubyձٞ02 47
  25. File#mode(mrbgem) mrb_value file_mode_(mrb_state *mrb, mrb_value self) { file *f; int

    m; f = DATA_PTR(self); m = file_mode(f); return mrb_fixnum_value(m); } ෱ԬRubyձٞ02 49
  26. ॳظԽ(mrbgem) void mrb_mruby_object_gem_init(mrb_state *mrb) { struct RClass *s = mrb_define_class(mrb,

    "Specinfra", mrb->object_class); mrb_define_method(mrb, s, "initialize", specinfra_new_, MRB_ARGS_NONE()); mrb_define_method(mrb, s, "file", specinfra_file_, MRB_ARGS_REQ(1)); struct RClass *f = mrb_define_class(mrb, "File", mrb->object_class); mrb_define_method(mrb, f, "mode", file_mode_, MRB_ARGS_NONE()); } ෱ԬRubyձٞ02 51
  27. SpecinfraΦϒδΣΫτͷमਖ਼(Rust) pub struct Specinfra<'a> { backend: &'a Backend, } impl<'a>

    Specinfra<'a> { pub fn new(b: &Backend) -> Specinfra { b.detect_platform(); Specinfra { backend: b } } ... ෱ԬRubyձٞ02 54
  28. BackendτϨΠτͷఆٛͱ࣮૷(Rust) pub trait Backend { fn detect_platform(&self) -> &str; }

    pub struct Direct; impl Backend for Direct { fn detect_platform(&self) -> &str { // ϓϥοτϑΥʔϜ൑ผॲཧ } } ෱ԬRubyձٞ02 55
  29. SpecinfraΦϒδΣΫτͷ֎෦༻ఆٛ(Rust) Ҿ਺ͰτϨΠτΛड͚औΔΑ͏ʹ͍ͯ͠Δͱ… pub extern "C" fn specinfra_new<'a>(ptr: *const Backend) ->

    *const Specinfra<'a> { let b = unsafe { &*ptr }; let s = Specinfra::new(b); // ͜ͷॲཧதͷ… Box::into_raw(Box::new(s)) } ෱ԬRubyձٞ02 56
  30. ηάϑΥ…(Rust) impl<'a> Specinfra<'a> { pub fn new(b: &Backend) -> Specinfra

    { b.detect_platform(); // ͜͜ͰηάϑΥ͢Δ Specinfra { backend: b } } ෱ԬRubyձٞ02 57
  31. BackendWrapper structͷಋೖ(Rust) τϨΠτ͸௚઀Ҿ਺Ͱ౉ͣ͞BackendWrapperͰ͘ΔΉ pub struct BackendWrapper(pub Box<Backend>); pub extern "C"

    fn specinfra_new<'a>(ptr: *const BackendWrapper) -> *const Specinfra<'a> { let b = unsafe { &*ptr }; let s = Specinfra::new(&*b.0); Box::into_raw(Box::new(s)) } ෱ԬRubyձٞ02 58
  32. DirectΦϒδΣΫτͷ֎෦༻ఆٛ(Rust) pub extern "C" fn direct_new() -> *const Direct {

    let d = Direct::new(); Box::into_raw(Box::new(d)) } ΛҎԼͷΑ͏ʹ͢Δ pub extern "C" fn direct_new() -> *const BackendWrapper { let d = Direct::new(); let b = BackendWrapper(Box::new(d)); Box::into_raw(Box::new(b)) } ෱ԬRubyձٞ02 59
  33. τϨΠτΛѻ͏ίʔυ(Ruby) ͜͏ॻ͖͍ͨ b = Direct::Binding.new s = Specinfra::Binding.new(b) f =

    s.file("/etc/passwd") printf("%#o\n", f.mode) ෱ԬRubyձٞ02 60
  34. DirectΦϒδΣΫτͷఆٛ(Ruby) class Direct < FFI::AutoPointer def self.release(ptr) Binding.free(ptr) end module

    Binding attach_function :free, :direct_free, [Direct], :void attach_function :new, :direct_new, [], Direct end end ෱ԬRubyձٞ02 61
  35. SpecinfraΦϒδΣΫτͷमਖ਼(Ruby) class Specinfra < FFI::AutoPointer ... module Binding ... attach_function

    :new, :specinfra_new, [:pointer], Specinfra ... end end ෱ԬRubyձٞ02 62
  36. τϨΠτΛѻ͏ίʔυ(mruby) ͜͏ॻ͖͍ͨ b = Direct.new s = Specinfra.new(b) f =

    s.file("/etc/passwd") printf("%#o\n", f.mode) ෱ԬRubyձٞ02 63
  37. τϨΠτؔ࿈ఆٛ(mrbgem) typedef int backend; extern specinfra *specinfra_new(backend *); extern backend

    *direct_new(void); struct mrb_data_type mrb_direct_type = { "Direct", mrb_free }; ෱ԬRubyձٞ02 64
  38. Direct.new(mrbgem) mrb_value direct_new_(mrb_state *mrb, mrb_value self) { backend *b; b

    = direct_new(); DATA_TYPE(self) = &mrb_direct_type; DATA_PTR(self) = b; return self; } ෱ԬRubyձٞ02 65
  39. Specinfra.newͷमਖ਼(mrbgem) mrb_value specinfra_new_(mrb_state *mrb, mrb_value self) { mrb_value b; mrb_get_args(mrb,

    "o", &b); specinfra *s = specinfra_new(DATA_PTR(b)); DATA_TYPE(self) = &mrb_specinfra_type; DATA_PTR(self) = s; return self; } ෱ԬRubyձٞ02 66
  40. ॳظԽ(mrbgem) void mrb_mruby_trait_gem_init(mrb_state *mrb) { ... s = mrb_define_class(mrb, "Specinfra",

    mrb->object_class); mrb_define_method(mrb, s, "initialize", new, MRB_ARGS_REQ(1)); ... struct RClass *d = mrb_define_class(mrb, "Direct", mrb->object_class); mrb_define_method(mrb, d, "initialize", direct_new_, MRB_ARGS_NONE()); } ෱ԬRubyձٞ02 67
  41. ΤϥʔΛൃੜͤ͞Δίʔυ(Rust) ύʔϛογϣϯऔಘ࣌ʹΤϥʔ͕ൃੜ͢Δ͜ͱΛ૝ఆ pub struct File<'a> { name: &'a str, error:

    &'a str, } impl<'a> File<'a> { pub fn mode(self) -> Result<i32, &'a str> { // ύʔϛογϣϯΛऔಘͯ͠ฦ͢ॲཧΛೖΕΔ } } ෱ԬRubyձٞ02 70
  42. ύʔϛογϣϯऔಘؔ਺ͷ֎෦ఆٛΛमਖ਼(Rust) pub extern "C" fn file_mode(ptr: *mut File) -> int32_t

    { let f = unsafe { &mut *ptr }; match f.mode() { Ok(mode) => mode, Err(e) => { f.error = e; -1 } } } ෱ԬRubyձٞ02 71
  43. ΤϥʔϝοηʔδऔಘͷͨΊͷ֎෦༻ؔ਺ఆٛ(Rust) pub extern "C" fn file_error(ptr: *const File) -> *const

    c_char { let f = unsafe { &*ptr }; CString::new(f.error).unwrap().into_raw() } ෱ԬRubyձٞ02 72
  44. Τϥʔॲཧ(Ruby) ͜͏ॲཧ͍ͨ͠ b = Direct::Binding.new s = Specinfra::Binding.new(b) f =

    s.file("/etc/passwd") begin printf("%#o\n", f.mode) rescue => e puts e.message end ෱ԬRubyձٞ02 73
  45. File#modeΛमਖ਼(Ruby) class File < FFI::AutoPointer ... def mode() mode =

    file_mode(self) if mode == -1 raise file_error(self) end mode end ... attach_function :file_error, [File], :string end ෱ԬRubyձٞ02 74
  46. Τϥʔॲཧ(mruby) ͜͏ॲཧ͍ͨ͠ b = Direct.new s = Specinfra.new(b) f =

    s.file("/etc/passwd") begin printf("%#o\n", f.mode) rescue => e puts e.message end ෱ԬRubyձٞ02 75
  47. File#modeͷमਖ਼(mrbgem) mrb_value mode(mrb_state *mrb, mrb_value self) { file *f =

    DATA_PTR(self); int m = file_mode(f); if (m < 0) { mrb_raise(mrb, E_RUNTIME_ERROR, file_error(f)); } else { return mrb_fixnum_value(m); } } ෱ԬRubyձٞ02 76
  48. SpecinfraΛ༻͍ͨϓϩμΫτͷίʔυྫ ServerspecʹΑΔςετίʔυ set :backend, :exec describe package('nginx') do it {

    should be_installed } end describe service('nginx') do it { should be_enabled } it { should be_running } end ෱ԬRubyձٞ02 81
  49. libspecinfraࢀߟࢿྉ • libspecinfra ϓϩδΣΫτͷ֓ཁͱࠓޙʹ͍ͭͯ | Advanced Technology Lab • http://atl.recruit-tech.co.jp/blog/4339/

    • libspecinfra ։ൃऀ޲͚νϡʔτϦΞϧ | Advanced Technology Lab • http://atl.recruit-tech.co.jp/blog/4349/ • libspecinfraͷ֓ཁͱݱঢ়ͱࠓޙ • https://speakerdeck.com/mizzy/overview-of-libspecinfra-project ෱ԬRubyձٞ02 83