From f36220212f2a80bedf485149f8babcf607922137 Mon Sep 17 00:00:00 2001 From: Andrew Liu Date: Sun, 20 Dec 2020 11:04:42 -0800 Subject: [PATCH 1/3] impl isobjectref for array --- rust/tvm-rt/src/array.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/rust/tvm-rt/src/array.rs b/rust/tvm-rt/src/array.rs index 1b0ce8399d1f..33dbed4a89e0 100644 --- a/rust/tvm-rt/src/array.rs +++ b/rust/tvm-rt/src/array.rs @@ -45,6 +45,26 @@ external! { fn array_size(array: ObjectRef) -> i64; } +impl IsObjectRef for Array { + type Object = Object; + fn as_ptr(&self) -> Option<&ObjectPtr> { + self.object.as_ptr() + } + fn into_ptr(self) -> Option> { + self.object.into_ptr() + } + fn from_ptr(object_ptr: Option>) -> Self { + let object_ref = match object_ptr { + Some(o) => o.into(), + _ => panic!() + }; + Array { + object: object_ref, + _data: PhantomData, + } + } +} + impl Array { pub fn from_vec(data: Vec) -> Result> { let iter = data.into_iter().map(T::into_arg_value).collect(); @@ -131,8 +151,8 @@ impl FromIterator for Array { } } -impl From> for ArgValue<'static> { - fn from(array: Array) -> ArgValue<'static> { +impl<'a, T: IsObjectRef> From> for ArgValue<'a> { + fn from(array: Array) -> ArgValue<'a> { array.object.into() } } From eeaed3f3e5ae45fc25d95a184221918c9e3ec591 Mon Sep 17 00:00:00 2001 From: Andrew Liu Date: Sun, 20 Dec 2020 11:37:22 -0800 Subject: [PATCH 2/3] array test --- rust/tvm-rt/src/array.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/tvm-rt/src/array.rs b/rust/tvm-rt/src/array.rs index 33dbed4a89e0..f3fbb7558347 100644 --- a/rust/tvm-rt/src/array.rs +++ b/rust/tvm-rt/src/array.rs @@ -193,6 +193,7 @@ mod tests { use super::Array; use crate::function::Result; use crate::string::String; + use crate::object::{IsObjectRef, ObjectRef}; #[test] fn create_array_and_get() -> Result<()> { @@ -203,4 +204,13 @@ mod tests { assert_eq!(array.get(2)?.to_string(), "baz"); Ok(()) } + + #[test] + fn downcast() -> Result<()> { + let vec: Vec = vec!["foo".into(), "bar".into(), "baz".into()]; + let array: ObjectRef = ObjectRef::from_ptr(Array::from_vec(vec)?.into_ptr()); + let array: Array = array.downcast::>().unwrap(); + assert_eq!(array.get(1)?.downcast::().unwrap(), "bar"); + Ok(()) + } } From a9689aad3fc3481bac6aa4acc0ec456f2e568d98 Mon Sep 17 00:00:00 2001 From: Andrew Liu Date: Mon, 21 Dec 2020 15:01:05 -0800 Subject: [PATCH 3/3] cargo fmt --- rust/tvm-rt/src/array.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rust/tvm-rt/src/array.rs b/rust/tvm-rt/src/array.rs index f3fbb7558347..5abf66708f45 100644 --- a/rust/tvm-rt/src/array.rs +++ b/rust/tvm-rt/src/array.rs @@ -55,8 +55,8 @@ impl IsObjectRef for Array { } fn from_ptr(object_ptr: Option>) -> Self { let object_ref = match object_ptr { - Some(o) => o.into(), - _ => panic!() + Some(o) => o.into(), + _ => panic!(), }; Array { object: object_ref, @@ -192,8 +192,8 @@ impl<'a, T: IsObjectRef> TryFrom for Array { mod tests { use super::Array; use crate::function::Result; - use crate::string::String; use crate::object::{IsObjectRef, ObjectRef}; + use crate::string::String; #[test] fn create_array_and_get() -> Result<()> {