|
1 | 1 | #[macro_use] |
2 | 2 | extern crate helix; |
3 | 3 |
|
4 | | -pub struct RubyArray<'a>(&'a [usize]); |
| 4 | +use helix::{Error, FromRuby}; |
5 | 5 |
|
6 | 6 | ruby! { |
7 | 7 | reopen class Array { |
8 | | - def is_superset_of(&self, needle: RubyArray) -> bool { |
9 | | - let needle = needle.0; |
| 8 | + def is_superset_of(&self, needle: Vec<u64>) -> Result<bool, Error> { |
| 9 | + if needle.is_empty() { return Ok(true) } |
10 | 10 |
|
11 | | - if needle.is_empty() { return true } |
| 11 | + let haystack = try!(self.as_vec()); |
12 | 12 |
|
13 | | - let haystack = self.as_ref(); |
| 13 | + if haystack.is_empty() { return Ok(false) } |
14 | 14 |
|
15 | | - if haystack.is_empty() { return false } |
16 | | - |
17 | | - let mut needle = needle.iter(); |
| 15 | + let mut needle = needle.into_iter(); |
18 | 16 | let mut needle_item = needle.next().unwrap(); |
19 | 17 |
|
20 | 18 | for item in haystack { |
21 | 19 | if item == needle_item { |
22 | 20 | match needle.next() { |
23 | | - None => return true, |
| 21 | + None => return Ok(true), |
24 | 22 | Some(next_item) => needle_item = next_item |
25 | 23 | } |
26 | 24 | } |
27 | 25 | } |
28 | 26 |
|
29 | | - false |
| 27 | + Ok(false) |
30 | 28 | } |
31 | 29 | } |
32 | 30 | } |
33 | 31 |
|
34 | | -use helix::{FromRuby, CheckedValue, CheckResult, sys}; |
35 | | - |
36 | | -impl<'a> FromRuby for RubyArray<'a> { |
37 | | - type Checked = CheckedValue<RubyArray<'a>>; |
38 | | - |
39 | | - fn from_ruby(value: sys::VALUE) -> CheckResult<Self::Checked> { |
40 | | - if unsafe { sys::RB_TYPE_P(value, sys::T_ARRAY) } { |
41 | | - Ok(unsafe { CheckedValue::new(value) }) |
42 | | - } else { |
43 | | - type_error!(value, "an Array of unsigned pointer-sized integers") |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - fn from_checked(checked: Self::Checked) -> RubyArray<'a> { |
48 | | - let value = checked.to_value(); |
49 | | - let size = unsafe { sys::RARRAY_LEN(value) }; |
50 | | - let ptr = unsafe { sys::RARRAY_PTR(value) }; |
51 | | - RubyArray(unsafe { std::slice::from_raw_parts(ptr as *const usize, size as usize) }) |
52 | | - } |
53 | | -} |
54 | | - |
55 | | -impl AsRef<[usize]> for Array { |
56 | | - fn as_ref<'a>(&'a self) -> &'a [usize] { |
57 | | - RubyArray::from_ruby_unwrap(self.helix).0 |
| 32 | +impl Array { |
| 33 | + fn as_vec(&self) -> Result<Vec<u64>, Error> { |
| 34 | + Vec::<u64>::from_ruby(self.helix).map(Vec::<u64>::from_checked) |
58 | 35 | } |
59 | 36 | } |
0 commit comments