toasty_core/stmt/expr_cast.rs
1use super::{Expr, Type};
2
3/// A type cast expression.
4///
5/// Converts an expression's value to a different type.
6///
7/// # Examples
8///
9/// ```text
10/// cast(x, i64) // cast `x` to `i64`
11/// cast(y, string) // cast `y` to `string`
12/// ```
13#[derive(Debug, Clone, PartialEq)]
14pub struct ExprCast {
15 /// The expression to cast.
16 pub expr: Box<Expr>,
17
18 /// The target type.
19 pub ty: Type,
20}
21
22impl Expr {
23 /// Creates a type cast expression that converts `expr` to the target type.
24 pub fn cast(expr: impl Into<Self>, ty: impl Into<Type>) -> Self {
25 ExprCast {
26 expr: Box::new(expr.into()),
27 ty: ty.into(),
28 }
29 .into()
30 }
31
32 /// Returns `true` if this expression is a type cast.
33 pub fn is_cast(&self) -> bool {
34 matches!(self, Self::Cast(_))
35 }
36}
37
38impl From<ExprCast> for Expr {
39 fn from(value: ExprCast) -> Self {
40 Self::Cast(value)
41 }
42}