jroesch commented on a change in pull request #5526:
URL: https://github.com/apache/incubator-tvm/pull/5526#discussion_r420936324



##########
File path: rust/tvm-sys/src/packed_func.rs
##########
@@ -0,0 +1,380 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use std::{
+    convert::TryFrom,
+    ffi::{CStr, CString},
+    os::raw::c_void,
+};
+
+pub use crate::ffi::TVMValue;
+use crate::{errors::ValueDowncastError, ffi::*};
+
+pub trait PackedFunc:
+    Fn(&[ArgValue]) -> Result<RetValue, crate::errors::FuncCallError> + Send + 
Sync
+{
+}
+
+impl<T> PackedFunc for T where
+    T: Fn(&[ArgValue]) -> Result<RetValue, crate::errors::FuncCallError> + 
Send + Sync
+{
+}
+
+/// Calls a packed function and returns a `RetValue`.
+///
+/// # Example
+///
+/// `call_packed!(my_tvm_func, &mut arg1, &mut arg2)`
+#[macro_export]
+macro_rules! call_packed {
+    ($fn:expr, $($args:expr),+) => {
+        $fn(&[$($args.into(),)+])
+    };
+    ($fn:expr) => {
+        $fn(&Vec::new())
+    };
+}
+
+/// Constructs a derivative of a TVMPodValue.
+macro_rules! TVMPODValue {
+    {
+        $(#[$m:meta])+
+        $name:ident $(<$a:lifetime>)? {
+            $($extra_variant:ident ( $variant_type:ty ) ),+ $(,)?
+        },
+        match $value:ident {
+            $($tvm_type:ident => { $from_tvm_type:expr })+
+        },
+        match &self {
+            $($self_type:ident ( $val:ident ) => { $from_self_type:expr })+
+        }
+        $(,)?
+    } => {
+        $(#[$m])+
+        #[derive(Clone, Debug)]
+        pub enum $name $(<$a>)? {
+            Int(i64),
+            UInt(i64),
+            Float(f64),
+            Null,
+            DataType(DLDataType),
+            String(CString),
+            Context(TVMContext),
+            Handle(*mut c_void),
+            ArrayHandle(TVMArrayHandle),
+            ObjectHandle(*mut c_void),
+            ModuleHandle(TVMModuleHandle),
+            FuncHandle(TVMFunctionHandle),
+            NDArrayHandle(*mut c_void),
+            $($extra_variant($variant_type)),+
+        }
+
+        impl $(<$a>)? $name $(<$a>)? {
+            pub fn from_tvm_value($value: TVMValue, type_code: u32) -> Self {
+                use $name::*;
+                #[allow(non_upper_case_globals)]
+                unsafe {
+                    match type_code as _ {
+                        DLDataTypeCode_kDLInt => Int($value.v_int64),
+                        DLDataTypeCode_kDLUInt => UInt($value.v_int64),
+                        DLDataTypeCode_kDLFloat => Float($value.v_float64),
+                        TVMTypeCode_kTVMNullptr => Null,
+                        TVMTypeCode_kTVMDataType => DataType($value.v_type),
+                        TVMTypeCode_kTVMContext => Context($value.v_ctx),
+                        TVMTypeCode_kTVMOpaqueHandle => 
Handle($value.v_handle),
+                        TVMTypeCode_kTVMDLTensorHandle => 
ArrayHandle($value.v_handle as TVMArrayHandle),
+                        TVMTypeCode_kTVMObjectHandle => 
ObjectHandle($value.v_handle),
+                        TVMTypeCode_kTVMModuleHandle => 
ModuleHandle($value.v_handle),
+                        TVMTypeCode_kTVMPackedFuncHandle => 
FuncHandle($value.v_handle),
+                        TVMTypeCode_kTVMNDArrayHandle => 
NDArrayHandle($value.v_handle),
+                        $( $tvm_type => { $from_tvm_type } ),+
+                        _ => unimplemented!("{}", type_code),
+                    }
+                }
+            }
+
+            pub fn to_tvm_value(&self) -> (TVMValue, TVMTypeCode) {
+                use $name::*;
+                match self {
+                    Int(val) => (TVMValue { v_int64: *val }, 
DLDataTypeCode_kDLInt),
+                    UInt(val) => (TVMValue { v_int64: *val as i64 }, 
DLDataTypeCode_kDLUInt),
+                    Float(val) => (TVMValue { v_float64: *val }, 
DLDataTypeCode_kDLFloat),
+                    Null => (TVMValue{ v_int64: 0 },TVMTypeCode_kTVMNullptr),
+                    DataType(val) => (TVMValue { v_type: *val }, 
TVMTypeCode_kTVMDataType),
+                    Context(val) => (TVMValue { v_ctx: val.clone() }, 
TVMTypeCode_kTVMContext),
+                    String(val) => {
+                        (
+                            TVMValue { v_handle: val.as_ptr() as *mut c_void },
+                            TVMTypeCode_kTVMStr,
+                        )
+                    }
+                    Handle(val) => (TVMValue { v_handle: *val }, 
TVMTypeCode_kTVMOpaqueHandle),
+                    ArrayHandle(val) => {
+                        (
+                            TVMValue { v_handle: *val as *const _ as *mut 
c_void },
+                            TVMTypeCode_kTVMNDArrayHandle,
+                        )
+                    },
+                    ObjectHandle(val) => (TVMValue { v_handle: *val }, 
TVMTypeCode_kTVMObjectHandle),
+                    ModuleHandle(val) =>
+                        (TVMValue { v_handle: *val }, 
TVMTypeCode_kTVMModuleHandle),
+                    FuncHandle(val) => (
+                        TVMValue { v_handle: *val },
+                        TVMTypeCode_kTVMPackedFuncHandle
+                    ),
+                    NDArrayHandle(val) =>
+                        (TVMValue { v_handle: *val }, 
TVMTypeCode_kTVMNDArrayHandle),
+                    $( $self_type($val) => { $from_self_type } ),+
+                }
+            }
+        }
+    }
+}
+
+TVMPODValue! {
+    /// A borrowed TVMPODValue. Can be constructed using `into()` but the 
preferred way
+    /// to obtain a `ArgValue` is automatically via `call_packed!`.
+    ArgValue<'a> {
+        Bytes(&'a TVMByteArray),
+        Str(&'a CStr),
+    },
+    match value {
+        TVMTypeCode_kTVMBytes => { Bytes(&*(value.v_handle as *const 
TVMByteArray)) }
+        TVMTypeCode_kTVMStr => { Str(CStr::from_ptr(value.v_handle as *const 
i8)) }
+    },
+    match &self {
+        Bytes(val) => {
+            (TVMValue { v_handle: val as *const _ as *mut c_void }, 
TVMTypeCode_kTVMBytes)
+        }
+        Str(val) => { (TVMValue { v_handle: val.as_ptr() as *mut c_void }, 
TVMTypeCode_kTVMStr) }
+    }
+}
+
+TVMPODValue! {
+    /// An owned TVMPODValue. Can be converted from a variety of primitive and 
object types.
+    /// Can be downcasted using `try_from` if it contains the desired type.
+    ///
+    /// # Example
+    ///
+    /// ```
+    /// use std::convert::{TryFrom, TryInto};
+    /// use tvm_sys::RetValue;
+    ///
+    /// let a = 42u32;
+    /// let b: u32 = tvm_sys::RetValue::from(a).try_into().unwrap();
+    ///
+    /// let s = "hello, world!";
+    /// let t: RetValue = s.to_string().into();
+    /// assert_eq!(String::try_from(t).unwrap(), s);
+    /// ```
+    RetValue {
+        Bytes(TVMByteArray),
+        Str(&'static CStr),
+    },
+    match value {
+        TVMTypeCode_kTVMBytes => { Bytes(*(value.v_handle as *const 
TVMByteArray)) }

Review comment:
       Yeah I think this code is from the previous iteration, there are a 
couple ownership issues we probably need to clean up. I've been trying to 
radically simplify the interface at the higher-level.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to