§ring-lang-codegen
Proc macros to generate Ring programming language
extensions in Rust with zero configuration.
§Features
- Zero config - Just use the
ring_extension! macro, no separate config files
- Auto-generated bindings - Structs, impl blocks, and functions are automatically wrapped
- Auto ring_libinit! - Library registration is generated for you
- Full IDE support - Works with rust-analyzer, autocomplete, and type checking
§Quick Start
Add to your Cargo.toml:
[lib]
crate-type = ["cdylib"]
[dependencies]
ring-lang-rs = "0.1"
ring-lang-codegen = "0.1"
§Usage
ⓘuse ring_lang_codegen::ring_extension;
use ring_lang_rs::*;
ring_extension! {
prefix: "mylib"; pub fn add(a: i32, b: i32) -> i32 {
a + b
}
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
#[derive(Default)]
pub struct Counter {
pub value: i64,
pub name: String,
}
impl Counter {
pub fn new(name: &str, initial: i64) -> Self {
Counter { value: initial, name: name.to_string() }
}
pub fn increment(&mut self) {
self.value += 1;
}
pub fn get_value(&self) -> i64 {
self.value
}
}
}
§What Gets Generated
| Source | Generated Ring Functions |
pub fn add(a, b) | mylib_add(a, b) |
pub struct Counter | mylib_counter_new(), mylib_counter_delete(ptr) |
pub value: i64 field | mylib_counter_get_value(ptr), mylib_counter_set_value(ptr, v) |
impl Counter { pub fn new() } | Replaces default _new with custom constructor |
pub fn increment(&mut self) | mylib_counter_increment(ptr) |
§Ring Usage
loadlib("libmylib.so") # or .dll / .dylib
? mylib_add(10, 20) # 30
? mylib_greet("World") # Hello, World!
obj = mylib_counter_new("test", 0)
mylib_counter_increment(obj)
? mylib_counter_get_value(obj) # 1
mylib_counter_delete(obj)
§Supported Types
§Return Types
| Rust Type | Ring Representation |
i8, i16, i32, i64, u8, u16, u32, u64, f32, f64 | Number |
bool | Number (1 or 0) |
String, &str | String |
Vec<T> | List |
Vec<Vec<T>> | Nested list (2D array) |
Option<T> | Value or empty string for None |
Result<T, E> | Value on Ok, Ring error on Err |
(A, B), (A, B, C) | List (tuple as list) |
Box<T> | Unwrapped inner value |
HashMap<K, V> | List of [key, value] pairs |
| Custom structs | C pointer |
§Parameter Types
| Rust Type | Ring Input |
i8, i16, i32, i64, u8, u16, u32, u64, f32, f64 | Number |
bool | Number (non-zero = true) |
&str, String | String |
&T (struct reference) | C pointer |
&mut T (mutable struct reference) | C pointer |
Vec<T> | List |
&[T] (slice) | List |
Option<T> | Value or empty string for None |
| Custom structs | C pointer |
| Rust Type | Get Returns | Set Accepts |
| Primitives | Number | Number |
String | String | String |
Vec<T> | List | List |
Option<T> | Value or empty string | Value or empty string |
| Struct | C pointer | C pointer |