Skip to content

Commit

Permalink
Rustify PackedFunc & Friends (apache#2969)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhynes authored and wweic committed Apr 8, 2019
1 parent ad1d5d8 commit 1e65e79
Show file tree
Hide file tree
Showing 18 changed files with 341 additions and 452 deletions.
1 change: 1 addition & 0 deletions rust/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name = "tvm-common"
version = "0.1.0"
authors = ["TVM Contributors"]
license = "Apache-2.0"
edition = "2018"

[features]
bindings = []
Expand Down
63 changes: 6 additions & 57 deletions rust/common/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,11 @@
use std::fmt;

static TYPE_CODE_STRS: [&str; 15] = [
"int",
"uint",
"float",
"handle",
"null",
"TVMType",
"TVMContext",
"ArrayHandle",
"NodeHandle",
"ModuleHandle",
"FuncHandle",
"str",
"bytes",
"NDArrayContainer",
"ExtBegin",
];

#[derive(Debug, Fail)]
#[fail(
display = "Could not downcast `{}` into `{}`",
expected_type, actual_type
)]
pub struct ValueDowncastError {
actual_type_code: i64,
expected_type_code: i64,
}

impl ValueDowncastError {
pub fn new(actual_type_code: i64, expected_type_code: i64) -> Self {
Self {
actual_type_code,
expected_type_code,
}
}
}

impl fmt::Display for ValueDowncastError {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(
formatter,
"Could not downcast TVMValue: expected `{}` but was {}",
TYPE_CODE_STRS[self.actual_type_code as usize],
TYPE_CODE_STRS[self.expected_type_code as usize]
)
}
pub actual_type: String,
pub expected_type: &'static str,
}

#[derive(Debug, Fail)]
Expand All @@ -62,18 +26,3 @@ impl FuncCallError {
}
}
}

// error_chain! {
// errors {
// TryFromTVMRetValueError(expected_type: String, actual_type_code: i64) {
// description("mismatched types while downcasting TVMRetValue")
// display("invalid downcast: expected `{}` but was `{}`",
// expected_type, type_code_to_string(actual_type_code))
// }
// }
// foreign_links {
// IntoString(std::ffi::IntoStringError);
// ParseInt(std::num::ParseIntError);
// Utf8(std::str::Utf8Error);
// }
// }
4 changes: 2 additions & 2 deletions rust/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This crate contains the refactored basic components required
//! for `runtime` and `frontend` TVM crates.

#![feature(box_syntax, trait_alias)]
#![feature(box_syntax, type_alias_enum_variants, trait_alias)]

#[macro_use]
extern crate failure;
Expand All @@ -25,5 +25,5 @@ pub mod packed_func;
pub mod value;

pub use errors::*;
pub use ffi::{TVMContext, TVMType};
pub use ffi::{TVMByteArray, TVMContext, TVMType};
pub use packed_func::{TVMArgValue, TVMRetValue};
Loading

0 comments on commit 1e65e79

Please sign in to comment.