summaryrefslogtreecommitdiff
path: root/src/eval/value.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-04-30 14:12:28 +0200
committerLaurenz <laurmaedje@gmail.com>2022-04-30 14:12:28 +0200
commitf9e115daf54c29358f890b137f50a33a781af680 (patch)
tree496de52246629ea8039db6beea94eb779ed2851d /src/eval/value.rs
parentf7c67cde72e6a67f45180856b332bae9863243bd (diff)
New block spacing model
Diffstat (limited to 'src/eval/value.rs')
-rw-r--r--src/eval/value.rs23
1 files changed, 20 insertions, 3 deletions
diff --git a/src/eval/value.rs b/src/eval/value.rs
index 31287fa8..6ce815a4 100644
--- a/src/eval/value.rs
+++ b/src/eval/value.rs
@@ -408,12 +408,25 @@ macro_rules! dynamic {
/// Make a type castable from a value.
macro_rules! castable {
+ ($type:ty: $inner:ty) => {
+ impl $crate::eval::Cast<$crate::eval::Value> for $type {
+ fn is(value: &$crate::eval::Value) -> bool {
+ <$inner>::is(value)
+ }
+
+ fn cast(value: $crate::eval::Value) -> $crate::diag::StrResult<Self> {
+ <$inner>::cast(value).map(Self)
+ }
+ }
+ };
+
(
$type:ty,
Expected: $expected:expr,
$($pattern:pat => $out:expr,)*
$(@$dyn_in:ident: $dyn_type:ty => $dyn_out:expr,)*
) => {
+ #[allow(unreachable_patterns)]
impl $crate::eval::Cast<$crate::eval::Value> for $type {
fn is(value: &$crate::eval::Value) -> bool {
#[allow(unused_variables)]
@@ -602,10 +615,14 @@ castable! {
castable! {
NonZeroUsize,
Expected: "positive integer",
- Value::Int(int) => Value::Int(int)
- .cast::<usize>()?
+ Value::Int(int) => int
.try_into()
- .map_err(|_| "must be positive")?,
+ .and_then(|int: usize| int.try_into())
+ .map_err(|_| if int <= 0 {
+ "must be positive"
+ } else {
+ "number too large"
+ })?,
}
castable! {