Sha256: 0924ee019f1f9dd4fb05639e1f4a67e25ad635fb3acf9b9cad89010fbab03590

Contents?: true

Size: 866 Bytes

Versions: 1

Compression:

Stored size: 866 Bytes

Contents

#include<stdio.h>
#include<ruby.h>

extern char *hello_rust(void);
extern void  fill_slice(char *);
extern int   num(void);

VALUE hello(void) {
    char *hello = hello_rust();
    printf("%s\n", hello);

    return Qnil;
}

VALUE make_hello() {
    // "Hello, world!".length == 14, with the null
    char *hello = (char *)malloc(sizeof(char) * 14);

    fill_slice(hello);

    printf("%s\n", hello);

    free(hello);

    return Qnil;
}

VALUE number() {
    int x = num();

    return INT2FIX(x);
}

// https://github.com/ruby/ruby/blob/trunk/README.EXT#L682
void Init_rust_example(void) {
    VALUE rust_example = rb_define_module("RustExample");
    
    rb_define_singleton_method(rust_example, "hello", hello, 0);
    rb_define_singleton_method(rust_example, "make_hello", make_hello, 0);
    rb_define_singleton_method(rust_example, "number", number, 0);
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rust_example-0.2.0-x86_64-linux ext/rust_example/rust_example.c