Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}
}