magnus-bindings
Magnus conventions for building Ruby native extensions from a Rust core: define_class, TryConvert/IntoValue, exception mapping, GVL handling, TypedData GC integration, and rake-compiler distribution.
SKILL.mdinternal/builtins/bindings/magnus/skills/magnus-bindings/SKILL.md
GitHub- Use
magnuscrate for Ruby native extensions. Define classes withmagnus::define_class. - Map Rust types to Ruby using
TryConvertandIntoValuetraits. ReturnResult<T, magnus::Error>. - Use
magnus::function!macro for defining Ruby methods. Usemagnus::method!for instance methods. - Map Rust errors to specific Ruby exceptions:
magnus::Error::new(exception::runtime_error(), msg). - Build with
rb_syscrate andmagnus-buildfor compilation. Userake-compilerfor gem distribution. - Support Ruby 3.2+ with multiple ABI versions.
- Test bindings from Ruby using RSpec or Minitest. Test error handling and edge cases.
- Keep Ruby wrapper gem thin — business logic in Rust, Ruby provides idiomatic API.
- Handle Ruby's GVL (Global VM Lock) — release for CPU-intensive Rust code.
- Use
TypedDatafor wrapping Rust structs as Ruby objects with proper GC integration. - Anti-patterns: panics in Ruby methods,
unsafewithoutSAFETYcomments, manual memory management.