Skip to main content

IntoExpr

Trait IntoExpr 

Source
pub trait IntoExpr<T = Expr> {
    type Output: Borrow<T> + Into<T>;

    // Required method
    fn into_expr(self) -> Result<Self::Output>;

    // Provided method
    fn into_owned_expr(self) -> Result<T>
       where Self: Sized { ... }
}
Expand description

A value that converts into the expression type T, possibly failing: a T or a subtype of it, owned or borrowed, a Rust literal, or an ExprResult under construction.

T is Expr by default, and PrimExpr for an operand that must be primitive. The subtypes of T are the types with an upcast to it, for which tvm-ffi generates an IntoArg<T>; every IntoArg<T> is an IntoExpr<T>, so a new expression type converts into each type it upcasts to without a list to maintain. The conversion never checks a type: an Expr or a Var is not a PrimExpr until it is checked, by downcast_ref, which borrows it in place, or downcast, which takes it. Its one error is the first error an ExprResult operand raised.

§Examples

use tvm_tirx::ir::prim::Add;
use tvm_tirx::ir::{PrimExpr, PrimType, PrimVar, Var, UNKNOWN_LOC};
use tvm_tirx::script::script;
use tvm_tirx::tvm_ffi::ObjectRefCore;

let x = PrimVar::new("x", PrimType::int(32), UNKNOWN_LOC);
let sum = Add::new(&x + 1, 2, UNKNOWN_LOC)?;
assert_eq!(script(&sum)?, "x + 1 + 2");

let f = PrimVar::new("f", PrimType::float(32), UNKNOWN_LOC);
assert!(Add::new(&f << 1, 2, UNKNOWN_LOC).is_err());

// A variable of any type is checked first, here borrowed in place.
let var = Var::new("y", PrimType::int(32), UNKNOWN_LOC);
let y = var.downcast_ref::<PrimVar>().expect("an int32 variable");
assert_eq!(script(&Add::new(y, &x, UNKNOWN_LOC)?)?, "y + x");

An Expr is not an IntoExpr<PrimExpr>:

ⓘ
fn add(a: &Expr, b: &Expr) -> tvm_tirx::tvm_ffi::Result<Add> {
    Add::new(a, b, UNKNOWN_LOC)
}

Required Associated Types§

Source

type Output: Borrow<T> + Into<T>

The expression: an owned T for an owned value or a literal, or a borrowed &T for a reference, which converts without a reference-count change.

A function that only reads the expression borrows it with .borrow(); one that keeps it takes ownership with into_owned_expr.

Required Methods§

Source

fn into_expr(self) -> Result<Self::Output>

Convert into an expression, or return the first error an ExprResult raised.

Provided Methods§

Source

fn into_owned_expr(self) -> Result<T>
where Self: Sized,

Convert into an owned expression, as Cow::into_owned does: an owned value moves, and only a borrowed expression is cloned.

Implementors§