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    pub fn cast(expr: impl Into<Self>, ty: impl Into<Type>) -> Self {
24        ExprCast {
25            expr: Box::new(expr.into()),
26            ty: ty.into(),
27        }
28        .into()
29    }
30
31    pub fn is_cast(&self) -> bool {
32        matches!(self, Self::Cast(_))
33    }
34}
35
36impl From<ExprCast> for Expr {
37    fn from(value: ExprCast) -> Self {
38        Self::Cast(value)
39    }
40}