Created
July 21, 2015 08:19
-
-
Save mfpiccolo/593cf64cac3f62c4ca94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![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