Skip to content

Instantly share code, notes, and snippets.

@mfpiccolo
Created July 21, 2015 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mfpiccolo/593cf64cac3f62c4ca94 to your computer and use it in GitHub Desktop.
Save mfpiccolo/593cf64cac3f62c4ca94 to your computer and use it in GitHub Desktop.
#![feature(libc)]
#![feature(cstr_memory)]
extern crate libc;
use std::ffi::CString;
use std::mem;
#[repr(C)]
pub struct RubyArray {
len: libc::size_t,
data: *const libc::c_void,
}
impl RubyArray {
fn from_vec<T>(vec: Vec<T>) -> RubyArray {
let array = RubyArray {
data: vec.as_ptr() as *const libc::c_void,
len: vec.len() as libc::size_t };
mem::forget(vec);
array
}
}
#[no_mangle]
pub extern fn number_to_char_array() -> RubyArray {
let mut utf_chars: Vec<*const libc::c_char> = vec![];
for i in 33..126 {
let maybe_char: Option<char> = std::char::from_u32(i);
utf_chars.push(CString::new(maybe_char.unwrap().to_string()).unwrap().into_ptr());
}
RubyArray::from_vec(utf_chars)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment