ruru 0.9.2

Native Ruby extensions in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# Ruru (Rust + Ruby)

## Native Ruby extensions in Rust

[![](http://meritbadge.herokuapp.com/ruru)](https://crates.io/crates/ruru)
[![Build Status](https://travis-ci.org/d-unseductable/ruru.svg?branch=master)](https://travis-ci.org/d-unseductable/ruru)
[![Build status](https://ci.appveyor.com/api/projects/status/2epyqhooimdu6u5l?svg=true)](https://ci.appveyor.com/project/d-unseductable/ruru)
[![Gitter](https://badges.gitter.im/rust-ruru/general.svg)](https://gitter.im/rust-ruru/general?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

<p align="center">
  <img src="http://this-week-in-ruru.org/assets/images/logo.png" width="350" height="350">
  <br>
  <b><a href="http://d-unseductable.github.io/ruru/ruru/index.html">Documentation</a></b>
  <br>
  <b><a href="http://this-week-in-ruru.org">Website</a></b>
  <br>
</p>

Have you ever considered rewriting some parts of your ~~slow~~ Ruby application?

Just replace your Ruby application with Rust, method by method, class by class. It does not require you
to change the interface of your classes or to change any other Ruby code.

As simple as Ruby, as efficient as Rust.

## Contents

* [Examples]#examples
  - [The famous `String#blank?` method]#the-famous-stringblank-method
  - [Simple Sidekiq-compatible server]#simple-sidekiq-compatible-server
  - [Safe conversions]#safe-conversions
  - [Wrapping Rust data to Ruby objects]#wrapping-rust-data-to-ruby-objects
  - [True parallelism]#true-parallelism
  - [Defining a new class]#defining-a-new-class
  - [Replacing only several methods instead of the whole class]#replacing-only-several-methods-instead-of-the-whole-class
  - [Class definition DSL]#class-definition-dsl
  - [Calling Ruby code from Rust]#calling-ruby-code-from-rust
* [... and why is FFI not enough?]#-and-why-is-ffi-not-enough
* [How do I use it?]#how-do-i-use-it
* [Contributors are welcome!]#contributors-are-welcome
* [License]#license

## Examples

### The famous `String#blank?` method

The fast `String#blank?` implementation by Yehuda Katz

```rust,no_run
#[macro_use]
extern crate ruru;

use ruru::{Boolean, Class, Object, RString};

methods!(
   RString,
   itself,

   fn string_is_blank() -> Boolean {
       Boolean::new(itself.to_string().chars().all(|c| c.is_whitespace()))
   }
);

#[no_mangle]
pub extern fn initialize_string() {
    Class::from_existing("String").define(|itself| {
        itself.def("blank?", string_is_blank);
    });
}
```

### Simple Sidekiq-compatible server

[Link to the repository](https://github.com/d-unseductable/rust_sidekiq)

### Safe conversions

Since 0.8.0 safe conversions are available for built-in Ruby types and for custom types.

Let's imagine that we are writing an HTTP server. It should handle requests which are passed from
Ruby side.

Any object which responds to `#body` method is considered as a valid request.

```rust,no_run
#[macro_use]
extern crate ruru;

use std::error::Error;
use ruru::{Class, Object, RString, VerifiedObject, VM};

class!(Request);

impl VerifiedObject for Request {
    fn is_correct_type<T: Object>(object: &T) -> bool {
        object.respond_to("body")
    }

    fn error_message() -> &'static str {
        "Not a valid request"
    }
}

class!(Server);

methods!(
    Server,
    itself,

    fn process_request(request: Request) -> RString {
        let body = request
            .and_then(|request| request.send("body", vec![]).try_convert_to::<RString>())
            .map(|body| body.to_string());

        // Either request does not respond to `body` or `body` is not a String
        if let Err(ref error) = body {
            VM::raise(error.to_exception(), error.description());
        }

        let formatted_body = format!("[BODY] {}", body.unwrap());

        RString::new(&formatted_body)
    }
);

#[no_mangle]
pub extern fn initialize_server() {
    Class::new("Server", None).define(|itself| {
        itself.def("process_request", process_request);
    });
}
```

### Wrapping Rust data to Ruby objects

Wrap `Server`s to `RubyServer` objects

```rust,no_run
#[macro_use] extern crate ruru;
#[macro_use] extern crate lazy_static;

use ruru::{AnyObject, Class, Fixnum, Object, RString, VM};

// The structure which we want to wrap
pub struct Server {
    host: String,
    port: u16,
}

impl Server {
    fn new(host: String, port: u16) -> Self {
        Server {
            host: host,
            port: port,
        }
    }

    fn host(&self) -> &str {
        &self.host
    }

    fn port(&self) -> u16 {
        self.port
    }
}

wrappable_struct!(Server, ServerWrapper, SERVER_WRAPPER);

class!(RubyServer);

methods!(
    RubyServer,
    itself,

    fn ruby_server_new(host: RString, port: Fixnum) -> AnyObject {
        let server = Server::new(host.unwrap().to_string(),
                                 port.unwrap().to_i64() as u16);

        Class::from_existing("RubyServer").wrap_data(server, &*SERVER_WRAPPER)
    }

    fn ruby_server_host() -> RString {
        let host = itself.get_data(&*SERVER_WRAPPER).host();

        RString::new(host)
    }

    fn ruby_server_port() -> Fixnum {
        let port = itself.get_data(&*SERVER_WRAPPER).port();

        Fixnum::new(port as i64)
    }
);

fn main() {
    let data_class = Class::from_existing("Data");

    Class::new("RubyServer", Some(&data_class)).define(|itself| {
        itself.def_self("new", ruby_server_new);

        itself.def("host", ruby_server_host);
        itself.def("port", ruby_server_port);
    });
}
```

### True parallelism

Ruru provides a way to enable true parallelism for Ruby threads by releasing GVL (GIL).

It means that a thread with released GVL runs in parallel with other threads without
being interrupted by GVL.

Current example demonstrates a "heavy" computation (`2 * 2` for simplicity) run in parallel.

```rust,no_run
#[macro_use] extern crate ruru;

use ruru::{Class, Fixnum, Object, VM};

class!(Calculator);

methods!(
    Calculator,
    itself,

    fn heavy_computation() -> Fixnum {
        let computation = || { 2 * 2 };
        let unblocking_function = || {};

        // release GVL for current thread until `computation` is completed
        let result = VM::thread_call_without_gvl(
            computation,
            Some(unblocking_function)
        );

        Fixnum::new(result)
    }
);

fn main() {
    Class::new("Calculator", None).define(|itself| {
        itself.def("heavy_computation", heavy_computation);
    });
}
```

### Defining a new class

Let's say you have a `Calculator` class.

```ruby
class Calculator
  def pow_3(number)
    (1..number).each_with_object({}) do |index, hash|
      hash[index] = index ** 3
    end
  end
end

# ... somewhere in the application code ...
Calculator.new.pow_3(5) #=> { 1 => 1, 2 => 8, 3 => 27, 4 => 64, 5 => 125 }
```

You have found that it's very slow to call `pow_3` for big numbers and decided to replace the whole class
with Rust.

```rust,no_run
#[macro_use]
extern crate ruru;

use std::error::Error;
use ruru::{Class, Fixnum, Hash, Object, VM};

class!(Calculator);

methods!(
    Calculator,
    itself,

    fn pow_3(number: Fixnum) -> Hash {
        let mut result = Hash::new();

        // Raise an exception if `number` is not a Fixnum
        if let Err(ref error) = number {
            VM::raise(error.to_exception(), error.description());
        }

        for i in 1..number.unwrap().to_i64() + 1 {
            result.store(Fixnum::new(i), Fixnum::new(i.pow(3)));
        }

        result
    }
);

#[no_mangle]
pub extern fn initialize_calculator() {
    Class::new("Calculator", None).define(|itself| {
        itself.def("pow_3", pow_3);
    });
}
```

Ruby:

```ruby
# No Calculator class in Ruby anymore

# ... somewhere in the application ...
Calculator.new.pow_3(5) #=> { 1 => 1, 2 => 8, 3 => 27, 4 => 64, 5 => 125 }
```

Nothing has changed in the API of class, thus there is no need to change any code elsewhere in the app.

### Replacing only several methods instead of the whole class

If the `Calculator` class from the example above has more Ruby methods, but we want to
replace only `pow_3`, use `Class::from_existing()`

```rust,ignore
Class::from_existing("Calculator").define(|itself| {
    itself.def("pow_3", pow_3);
});
```

### Class definition DSL

```rust,no_run
Class::new("Hello", None).define(|itself| {
    itself.const_set("GREETING", &RString::new("Hello, World!").freeze());

    itself.attr_reader("reader");

    itself.def_self("greeting", greeting);
    itself.def("many_greetings", many_greetings);

    itself.define_nested_class("Nested", None).define(|itself| {
        itself.def_self("nested_greeting", nested_greeting);
    });
});
```

Which corresponds to the following Ruby code:

```ruby
class Hello
  GREETING = "Hello, World".freeze

  attr_reader :reader

  def self.greeting
    # ...
  end

  def many_greetings
    # ...
  end

  class Nested
    def self.nested_greeting
      # ...
    end
  end
end
```

See documentation for `Class` and `Object` for more information.

### Calling Ruby code from Rust

Getting an account balance of some `User` whose name is John and who is 18 or 19 years old.

```ruby
default_balance = 0

account_balance = User
  .find_by(age: [18, 19], name: 'John')
  .account_balance

account_balance = default_balance unless account_balance.is_a?(Fixnum)
```

```rust,no_run
#[macro_use]
extern crate ruru;

use ruru::{Array, Class, Fixnum, Hash, Object, RString, Symbol};

fn main() {
    let default_balance = 0;
    let mut conditions = Hash::new();

    conditions.store(
        Symbol::new("age"),
        Array::new().push(Fixnum::new(18)).push(Fixnum::new(19))
    );

    conditions.store(
        Symbol::new("name"),
        RString::new("John")
    );

    // Fetch user and his balance
    // and set it to 0 if balance is not a Fixnum (for example `nil`)
    let account_balance =
        Class::from_existing("User")
            .send("find_by", vec![conditions.to_any_object()])
            .send("account_balance", vec![])
            .try_convert_to::<Fixnum>()
            .map(|balance| balance.to_i64())
            .unwrap_or(default_balance);
}
```

**Check out [Documentation](http://d-unseductable.github.io/ruru/ruru/index.html) for many more
examples!**

## ... and why is **FFI** not enough?

 - No support of native Ruby types;

 - No way to create a standalone application to run the Ruby VM separately;

 - No way to call your Ruby code from Rust;

## How do I use it?

Warning! The crate is a WIP.

It is recommended to use [Thermite](https://github.com/malept/thermite) gem,
a Rake-based helper for building and distributing Rust-based Ruby extensions.

To be able to use Ruru, make sure that your Ruby version is 2.3.0 or higher.

1. Your local MRI copy has to be built with the `--enable-shared` option. For
   example, using rbenv:

  ```bash
  CONFIGURE_OPTS=--enable-shared rbenv install 2.3.0
  ```

2. Add Ruru to `Cargo.toml`

  ```toml
  [dependencies]
  ruru = "0.9.0"
  ```

3. Compile your library as a `dylib`

  ```toml
  [lib]
  crate-type = ["dylib"]
  ```

4. Create a function which will initialize the extension

  ```rust,ignore
  #[no_mangle]
  pub extern fn initialize_my_app() {
      Class::new("SomeClass");

      // ... etc
  }
  ```

5. Build extension

  ```bash
  $ cargo build --release
  ```

  or using Thermite

  ```bash
  $ rake thermite:build
  ```

6. On the ruby side, open the compiled `dylib` and call the function to initialize extension

  ```ruby
  require 'fiddle'

  library = Fiddle::dlopen('path_to_dylib/libmy_library.dylib')

  Fiddle::Function.new(library['initialize_my_app'], [], Fiddle::TYPE_VOIDP).call
  ```

7. Ruru is ready :heart:

## Contributors are welcome!

If you have any questions, join Ruru on
[Gitter](https://gitter.im/rust-ruru/general?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)

## License

MIT License

Copyright (c) 2015-2016 Dmitry Gritsay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Icon is designed by [Github](https://github.com).