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