Skip to content

Commit c241e41

Browse files
committed
Streamline docsite
1 parent 0373fe3 commit c241e41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1497
-5754
lines changed

Rakefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ desc "Pretty the files"
7979
task :fmt do
8080
sh "cargo fmt"
8181
sh "bundle exec standardrb --fix" if RUBY_VERSION >= "2.6.0"
82+
Dir.chdir("docsite") do
83+
sh "npm run fmt"
84+
end
8285
sh "npx prettier --write $(git ls-files '*.yml')"
8386
md_files = `git ls-files '*.md'`.split("\n").select { |f| File.exist?(f) }
8487
sh "npx", "prettier", "--write", "--print-width=120", "--prose-wrap=always", *md_files

docsite/.prettierignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Dependencies
2+
node_modules/
3+
4+
# Build artifacts
5+
build/
6+
.docusaurus/
7+
8+
# Lockfiles
9+
package-lock.json
10+
yarn.lock
11+
12+
# IDE
13+
.idea/
14+
.vscode/
15+
*.swp
16+
*.swo
17+
18+
# OS
19+
.DS_Store

docsite/.prettierrc.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"printWidth": 120,
3+
"semi": true,
4+
"singleQuote": false,
5+
"tabWidth": 2,
6+
"trailingComma": "es5",
7+
"proseWrap": "preserve"
8+
}
Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,52 @@
11
---
22
id: rb-sys-features
3-
title: rb-sys Crate Features
3+
title: Crate `rb-sys`
44
---
55

6-
# rb-sys Crate Features
6+
# Crate `rb-sys`
77

8-
The `rb-sys` crate provides battle-tested Rust bindings for the Ruby C API. It uses the
9-
[`rust-bindgen`](https://github.com/rust-lang/rust-bindgen) crate to generate bindings from the `ruby.h` header.
8+
The `rb-sys` crate provides low-level Rust bindings for the Ruby C API, generated directly from `ruby.h` headers.
109

11-
## Usage Notice
10+
## Constraints
1211

13-
This is a very low-level library. If you are looking to write a gem in Rust, you should probably use the
14-
[Magnus](https://github.com/matsadler/magnus) crate with the `rb-sys-interop` feature, which provides a higher-level,
15-
more ergonomic API.
12+
- **Direct `rb-sys`:** Use for raw, `unsafe` access to `libruby`.
13+
- **Gem Development:** For most gem development, prefer higher-level libraries (e.g., [Magnus](https://github.com/matsadler/magnus) with `rb-sys` feature). See [Development Approaches](../development-approaches.mdx).
14+
- **Embedding Ruby:** Use `rb-sys` with the `link-ruby` feature.
1615

17-
If you need raw/unsafe bindings to libruby, then this crate is for you!
16+
## Crate Features
1817

19-
## Writing a Ruby Gem
18+
The following features can be enabled in your `Cargo.toml`.
2019

21-
Ruby gems require boilerplate to be defined to be usable from Ruby. `rb-sys` makes this process painless by doing the
22-
work for you. Simply enable the `gem` feature:
20+
| Feature | Description | Default |
21+
| :------------------------- | :------------------------------------------------------------------------------- | :------ |
22+
| `gem` | Configures the crate for building a Ruby gem (e.g., does not link `libruby`). ||
23+
| `link-ruby` | Links `libruby` for embedding the Ruby VM in a Rust binary. | |
24+
| `ruby-static` | Statically links `libruby`. Can also be set with the `RUBY_STATIC=true` env var. | |
25+
| `global-allocator` | Reports Rust memory allocations to the Ruby GC. Recommended for gems. | |
26+
| `stable-api` | Uses Ruby's C-level stable API if available for your Ruby version. | |
27+
| `bindgen-rbimpls` | Includes internal Ruby implementation types in the generated bindings. | |
28+
| `bindgen-deprecated-types` | Includes deprecated Ruby functions and types in the bindings. | |
2329

24-
```toml
25-
[dependencies]
26-
rb-sys = "0.9"
27-
```
30+
## Usage Examples
2831

29-
Under the hood, this ensures the crate does not link libruby (unless on Windows) and defines a `ruby_abi_version`
30-
function for Ruby 3.2+.
32+
### Ruby Gem Integration
3133

32-
## Embedding libruby in Your Rust App
33-
34-
:::important If you are authoring a Ruby gem, you do not need to enable this feature. :::
35-
36-
If you need to link libruby (i.e., you are initializing a Ruby VM in your Rust code), you can enable the `link-ruby`
37-
feature:
34+
Enable the `gem` feature (default) and `global-allocator` for memory reporting.
3835

3936
```toml
4037
[dependencies]
41-
rb-sys = { version = "0.9", features = ["link-ruby"] }
38+
rb-sys = { version = "0.9", features = ["global-allocator"] }
4239
```
4340

44-
## Static libruby
41+
### Embedding Ruby VM
4542

46-
You can also force static linking of libruby:
43+
Enable the `link-ruby` feature to embed a Ruby VM.
4744

4845
```toml
4946
[dependencies]
50-
rb-sys = { version = "0.9", features = ["ruby-static"] }
51-
```
52-
53-
Alternatively, you can set the `RUBY_STATIC=true` environment variable.
54-
55-
## Available Features
56-
57-
The `rb-sys` crate provides several features that can be enabled in your `Cargo.toml`:
58-
59-
| Feature | Description |
60-
| -------------------------- | --------------------------------------------------------------- |
61-
| `global-allocator` | Report Rust memory allocations to the Ruby GC (_recommended_) |
62-
| `ruby-static` | Link the static version of libruby |
63-
| `link-ruby` | Link libruby (typically used for embedding, not for extensions) |
64-
| `bindgen-rbimpls` | Include the Ruby impl types in bindings |
65-
| `bindgen-deprecated-types` | Include deprecated Ruby methods in bindings |
66-
| `gem` | Set up the crate for use in a Ruby gem (default feature) |
67-
| `stable-api` | Use the stable API (C level) if available for your Ruby version |
68-
69-
## Example Cargo.toml
70-
71-
```toml
72-
[dependencies]
73-
rb-sys = { version = "0.9", features = ["global-allocator", "stable-api"] }
47+
rb-sys = { version = "0.9", features = ["link-ruby"] }
7448
```
7549

7650
## Ruby Version Compatibility
7751

78-
`rb-sys` is compatible with Ruby 2.6 and later. The crate detects the Ruby version at compile time and adapts the
79-
bindings accordingly.
80-
81-
For Ruby 3.2 and later, `rb-sys` provides a `ruby_abi_version` function that is required for native extensions.
82-
83-
## Integration with Magnus
84-
85-
If you're building a Ruby extension, it's recommended to use the [Magnus](https://github.com/matsadler/magnus) crate on
86-
top of `rb-sys`. Magnus provides a high-level, safe API for interacting with Ruby:
87-
88-
```toml
89-
[dependencies]
90-
magnus = { version = "0.7", features = ["rb-sys"] }
91-
rb-sys = "0.9"
92-
```
52+
`rb-sys` is compatible with Ruby 2.6+. It detects the Ruby version at compile time and generates appropriate bindings. Ruby 3.2+ requires `ruby_abi_version` function, which `rb-sys` provides automatically.

docsite/docs/api-reference/rb-sys-gem-config.mdx

Lines changed: 25 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,63 @@
11
---
22
id: rb-sys-gem-config
3-
title: rb_sys Gem Configuration
3+
title: Gem Configuration
44
---
55

6-
# rb_sys Gem Configuration
6+
# Gem Configuration
77

8-
The `rb_sys` gem makes it easy to build native Ruby extensions in Rust. It interoperates with existing Ruby native
9-
extension toolchains (i.e., `rake-compiler`) to make testing, building, and cross-compilation of gems easy.
8+
The `rb_sys` Ruby gem provides helpers for building Rust-based extensions, integrating with standard tools like `rake-compiler`.
109

11-
## RbSys::ExtensionTask
10+
## `RbSys::ExtensionTask`
1211

13-
This gem provides a `RbSys::ExtensionTask` class that can be used to build a Ruby extension in Rust. It's a thin wrapper
14-
around `Rake::ExtensionTask` that provides sane defaults for building Rust extensions.
12+
Define how your Rust extension is built within your `Rakefile` using `RbSys::ExtensionTask`.
1513

1614
```ruby
1715
# Rakefile
18-
1916
require "rb_sys/extensiontask"
2017

2118
GEMSPEC = Gem::Specification.load("my_gem.gemspec")
2219

2320
RbSys::ExtensionTask.new("my-crate-name", GEMSPEC) do |ext|
2421
ext.lib_dir = "lib/my_gem"
25-
26-
# If you want to use `rb-sys-dock` for cross-compilation:
27-
ext.cross_compile = true
22+
ext.cross_compile = true # Enable cross-compilation with rb-sys-dock.
2823
end
2924
```
3025

31-
## create_rust_makefile
26+
## `create_rust_makefile`
3227

33-
The gem provides a simple helper to build a Ruby-compatible Makefile for your Rust extension:
28+
Generate a Ruby-compatible `Makefile` for your Rust extension in your `extconf.rb`.
3429

3530
```ruby
36-
# ext/rust_reverse/extconf.rb
37-
38-
# We need to require mkmf *first* since `rake-compiler` injects code here for cross compilation
31+
# ext/my_gem/extconf.rb
3932
require "mkmf"
4033
require "rb_sys/mkmf"
4134

42-
create_rust_makefile("rust_reverse") do |r|
43-
# Create debug builds in dev. Make sure that release gems are compiled with
44-
# `RB_SYS_CARGO_PROFILE=release` (optional)
45-
r.profile = ENV.fetch("RB_SYS_CARGO_PROFILE", :dev).to_sym
46-
47-
# Can be overridden with `RB_SYS_CARGO_FEATURES` env var (optional)
48-
r.features = ["test-feature"]
49-
50-
# You can add whatever env vars you want to the env hash (optional)
51-
r.env = {"FOO" => "BAR"}
52-
53-
# If your Cargo.toml is in a different directory, you can specify it here (optional)
54-
r.ext_dir = "."
55-
56-
# Extra flags to pass to the $RUSTFLAGS environment variable (optional)
57-
r.extra_rustflags = ["--cfg=some_nested_config_var_for_crate"]
58-
59-
# Force a rust toolchain to be installed via rustup (optional)
60-
# You can also set the env var `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN=true`
61-
r.force_install_rust_toolchain = "stable"
62-
63-
# Clean up the target/ dir after `gem install` to reduce bloat (optional)
64-
r.clean_after_install = false # default: true if invoked by rubygems
65-
66-
# Auto-install Rust toolchain if not present on "gem install" (optional)
67-
r.auto_install_rust_toolchain = false # default: true if invoked by rubygems
35+
create_rust_makefile("my_gem") do |r|
36+
r.profile = ENV.fetch("RB_SYS_CARGO_PROFILE", :dev).to_sym # Cargo build profile (:dev or :release).
37+
r.features = ["a-feature"] # Comma-separated list of Cargo features.
38+
r.extra_rustflags = ["--cfg=some_config_flag"] # Extra flags for $RUSTFLAGS.
39+
r.ext_dir = "." # Directory containing Cargo.toml, relative to extconf.rb.
6840
end
6941
```
7042

7143
## Environment Variables
7244

73-
The `rb_sys` gem respects several environment variables that can modify its behavior:
74-
75-
| Environment Variable | Description |
76-
| ------------------------------------- | --------------------------------------------------- |
77-
| `RB_SYS_CARGO_PROFILE` | Set the Cargo profile (i.e., `release` or `dev`) |
78-
| `RB_SYS_CARGO_FEATURES` | Comma-separated list of Cargo features to enable |
79-
| `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN` | Force installation of a Rust toolchain |
80-
| `RUBY_STATIC` | Force static linking of libruby if set to `true` |
81-
| `LIBCLANG_PATH` | Path to libclang if it can't be found automatically |
82-
83-
## Tips and Tricks
84-
85-
- When using `rake-compiler` to build your gem, you can use the `RB_SYS_CARGO_PROFILE` environment variable to set the
86-
Cargo profile (i.e., `release` or `dev`).
87-
88-
- You can pass Cargo arguments to `rake-compiler` like so: `rake compile -- --verbose`
45+
These environment variables control the `rb_sys` build process.
8946

90-
- It's possible to force an installation of a Rust toolchain by setting the `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN`
91-
environment variable. This will install [rustup](https://rustup.rs/) and [cargo](https://crates.io/) in the build
92-
directory, so the end user does not have to have Rust pre-installed. Ideally, this should be a last resort, as it's
93-
better to already have the toolchain installed on your system.
47+
| Environment Variable | Description |
48+
| :------------------------------------ | :------------------------------------------------------- |
49+
| `RB_SYS_CARGO_PROFILE` | Set the Cargo profile (e.g., `release` or `dev`). |
50+
| `RB_SYS_CARGO_FEATURES` | Comma-separated list of Cargo features to enable. |
51+
| `RB_SYS_FORCE_INSTALL_RUST_TOOLCHAIN` | Set to `true` to force installation of a Rust toolchain. |
52+
| `RUBY_STATIC` | Set to `true` to force static linking of `libruby`. |
53+
| `LIBCLANG_PATH` | Path to `libclang` if it can't be found automatically. |
54+
| `RB_SYS_VERBOSE_BUILD` | Set to `true` to enable verbose output during the build. |
9455

9556
## Troubleshooting
9657

97-
### Libclang Issues
98-
99-
If you see an error like this:
100-
101-
```
102-
thread 'main' panicked at 'Unable to find libclang: "couldn't find any valid shared libraries matching: ['libclang.so', 'libclang-*.so', 'libclang.so.*', 'libclang-*.so.*'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: [])"'
103-
```
58+
### `libclang` Not Found
10459

105-
This means that bindgen is having issues finding a usable version of libclang. An easy way to fix this is to install the
106-
[`libclang` gem](https://github.com/oxidize-rb/libclang-rb), which will install a pre-built version of libclang for you.
107-
`rb_sys` will automatically detect this gem and use it.
60+
If `libclang` is not found during `bindgen` execution, add the `libclang` gem to your `Gemfile`.
10861

10962
```ruby
11063
# Gemfile

0 commit comments

Comments
 (0)