Skip to content

Commit

Permalink
[Rust] Impl IsObjectRef for Array (apache#7138)
Browse files Browse the repository at this point in the history
* impl isobjectref for array

* array test

* cargo fmt
  • Loading branch information
hypercubestart authored and trevor-m committed Jan 21, 2021
1 parent 950f21a commit 435c3e1
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions rust/tvm-rt/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ external! {
fn array_size(array: ObjectRef) -> i64;
}

impl<T: IsObjectRef> IsObjectRef for Array<T> {
type Object = Object;
fn as_ptr(&self) -> Option<&ObjectPtr<Self::Object>> {
self.object.as_ptr()
}
fn into_ptr(self) -> Option<ObjectPtr<Self::Object>> {
self.object.into_ptr()
}
fn from_ptr(object_ptr: Option<ObjectPtr<Self::Object>>) -> Self {
let object_ref = match object_ptr {
Some(o) => o.into(),
_ => panic!(),
};
Array {
object: object_ref,
_data: PhantomData,
}
}
}

impl<T: IsObjectRef> Array<T> {
pub fn from_vec(data: Vec<T>) -> Result<Array<T>> {
let iter = data.into_iter().map(T::into_arg_value).collect();
Expand Down Expand Up @@ -131,8 +151,8 @@ impl<T: IsObjectRef> FromIterator<T> for Array<T> {
}
}

impl<T: IsObjectRef> From<Array<T>> for ArgValue<'static> {
fn from(array: Array<T>) -> ArgValue<'static> {
impl<'a, T: IsObjectRef> From<Array<T>> for ArgValue<'a> {
fn from(array: Array<T>) -> ArgValue<'a> {
array.object.into()
}
}
Expand Down Expand Up @@ -172,6 +192,7 @@ impl<'a, T: IsObjectRef> TryFrom<RetValue> for Array<T> {
mod tests {
use super::Array;
use crate::function::Result;
use crate::object::{IsObjectRef, ObjectRef};
use crate::string::String;

#[test]
Expand All @@ -183,4 +204,13 @@ mod tests {
assert_eq!(array.get(2)?.to_string(), "baz");
Ok(())
}

#[test]
fn downcast() -> Result<()> {
let vec: Vec<String> = vec!["foo".into(), "bar".into(), "baz".into()];
let array: ObjectRef = ObjectRef::from_ptr(Array::from_vec(vec)?.into_ptr());
let array: Array<ObjectRef> = array.downcast::<Array<ObjectRef>>().unwrap();
assert_eq!(array.get(1)?.downcast::<String>().unwrap(), "bar");
Ok(())
}
}

0 comments on commit 435c3e1

Please sign in to comment.