Fix membership test

This commit is contained in:
Godfrey Chan 2017-09-26 16:08:23 -04:00
parent 6d3675c0f8
commit 44c790244a
1 changed files with 28 additions and 8 deletions

View File

@ -1,9 +1,13 @@
#[macro_use]
extern crate helix;
pub struct RubyArray<'a>(&'a [usize]);
ruby! {
reopen class Array {
def is_superset_of(&self, needle: &[usize]) -> bool {
def is_superset_of(&self, needle: RubyArray) -> bool {
let needle = needle.0;
if needle.is_empty() { return true }
let haystack = self.as_ref();
@ -27,13 +31,29 @@ ruby! {
}
}
// Delete me:
use helix::{FromRuby, CheckedValue, CheckResult, ToRust, sys};
use helix::{FromRuby, ToRust};
impl AsRef<[usize]> for Array {
fn as_ref(&self) -> &[usize] {
let checked = FromRuby::from_ruby(self.helix);
checked.unwrap().to_rust()
impl<'a> FromRuby for RubyArray<'a> {
fn from_ruby(value: sys::VALUE) -> CheckResult<RubyArray<'a>> {
if unsafe { sys::RB_TYPE_P(value, sys::T_ARRAY) } {
Ok(unsafe { CheckedValue::new(value) })
} else {
type_error!(value, "an Array of unsigned pointer-sized integers")
}
}
}
impl<'a> ToRust<RubyArray<'a>> for CheckedValue<RubyArray<'a>> {
fn to_rust(self) -> RubyArray<'a> {
let size = unsafe { sys::RARRAY_LEN(self.inner) };
let ptr = unsafe { sys::RARRAY_PTR(self.inner) };
RubyArray(unsafe { std::slice::from_raw_parts(ptr as *const usize, size as usize) })
}
}
impl AsRef<[usize]> for Array {
fn as_ref<'a>(&'a self) -> &'a [usize] {
let checked = RubyArray::from_ruby(self.helix);
checked.unwrap().to_rust().0
}
}