]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_ty/src/consteval.rs
Merge #8882
[rust.git] / crates / hir_ty / src / consteval.rs
index a4a430f30fef0935e93f48ea694cf85bdda3eacd..e3ceb3d626996ac70cf023a26fbbb7675a2faf08 100644 (file)
 use crate::{Const, ConstData, ConstValue, Interner, TyKind};
 
 /// Extension trait for [`Const`]
-pub trait ConstExtension {
+pub trait ConstExt {
     /// Is a [`Const`] unknown?
     fn is_unknown(&self) -> bool;
 }
 
-impl ConstExtension for Const {
+impl ConstExt for Const {
     fn is_unknown(&self) -> bool {
         match self.data(&Interner).value {
             // interned Unknown
@@ -35,20 +35,12 @@ fn is_unknown(&self) -> bool {
     }
 }
 
-/// Extension trait for [`Expr`]
-pub trait ExprEval {
-    /// Attempts to evaluate the expression as a target usize.
-    fn eval_usize(&self) -> Option<u64>;
-}
-
-impl ExprEval for Expr {
-    // FIXME: support more than just evaluating literals
-    fn eval_usize(&self) -> Option<u64> {
-        match self {
-            Expr::Literal(Literal::Uint(v, None))
-            | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(),
-            _ => None,
-        }
+// FIXME: support more than just evaluating literals
+pub fn eval_usize(expr: &Expr) -> Option<u64> {
+    match expr {
+        Expr::Literal(Literal::Uint(v, None))
+        | Expr::Literal(Literal::Uint(v, Some(BuiltinUint::Usize))) => (*v).try_into().ok(),
+        _ => None,
     }
 }