Skip to main content

Crate ring_lang_codegen

Crate ring_lang_codegen 

Source
Expand description

§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";  // Optional prefix for all functions

    // Standalone functions - generates mylib_add(a, b)
    pub fn add(a: i32, b: i32) -> i32 {
        a + b
    }

    pub fn greet(name: &str) -> String {
        format!("Hello, {}!", name)
    }

    // Structs with auto-generated accessors
    #[derive(Default)]
    pub struct Counter {
        pub value: i64,
        pub name: String,
    }

    // Impl blocks with methods
    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

SourceGenerated Ring Functions
pub fn add(a, b)mylib_add(a, b)
pub struct Countermylib_counter_new(), mylib_counter_delete(ptr)
pub value: i64 fieldmylib_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

Rust TypeRing Type
i8, i16, i32, i64, u8, u16, u32, u64, f32, f64Number
boolNumber (0/1)
String, &strString
Structs (via pointer)C Pointer

Macros§

ring_extension
Define a Ring module with auto-generated bindings and ring_libinit!