oxidize.rb
Building Ruby extensions with Rust using rb-sys
Why rb-sys?
Leverage Rust's performance, safety, and modern tooling without leaving the Ruby ecosystem.
Performance
Write CPU-intensive code in Rust to speed up bottlenecks in your Ruby application. Ideal for parsing, image processing, and complex computations.
Memory Safety
Eliminate a whole class of bugs with Rust's compile-time memory safety guarantees. Say goodbye to segfaults and memory leaks from native extensions.
Modern Tooling
Get access to the entire Rust ecosystem, including Cargo, a first-class package manager, and a rich library of existing crates.
Key Features
Easy to Use
rb-sys integrates into Ruby projects with minimal setup, combining Ruby's flexibility with Rust's performance and safety.
Focus on What Matters
rb-sys handles the complex FFI integration between Ruby and Rust, letting you concentrate on your application code.
Powered by Rust
Add Rust capabilities to Ruby applications with simplified bindings to the Ruby C API.
High-Performance JSON Parsing
A real-world example of replacing a pure Ruby method with a much faster Rust implementation.
require 'json'
json_string = '{"name": "John Doe", "age": 30, "is_student": false}'
# Parse with standard library
parsed_data = JSON.parse(json_string)
puts parsed_data["name"] # => "John Doe"
use magnus::{function, prelude::*, Error, Ruby, Value};
// Helper to convert serde_json::Value to Magnus's Value
fn json_to_ruby(ruby: &Ruby, value: serde_json::Value) -> Result<Value, Error> {
// ... implementation details ...
magnus::serde::to_value(ruby, &value)
}
// The high-performance parsing function
fn parse_json(ruby: &Ruby, json_string: String) -> Result<Value, Error> {
let value: serde_json::Value = serde_json::from_str(&json_string)
.map_err(|e| Error::new(magnus::exception::runtime_error(), e.to_string()))?;
json_to_ruby(ruby, value)
}
#[magnus::init]
fn init(ruby: &Ruby) -> Result<(), Error> {
let module = ruby.define_module("FastJson")?;
module.define_singleton_method("parse", function!(parse_json, 1))?;
Ok(())
}
Used By
`rb-sys` and the oxidize-rb toolchain are trusted in production by these and other great projects.