about summary refs log tree commit diff
path: root/tvix/eval/src/value/mod.rs
diff options
context:
space:
mode:
authorGriffin Smith <grfn@gws.fyi>2022-10-18T10·32-0400
committerclbot <clbot@tvl.fyi>2022-10-24T08·13+0000
commite2f0967d3fd44cac78ac50425bc2dbe65fd4a8c4 (patch)
tree3a025e71afed11681aa19ffb9b57b76b773dc079 /tvix/eval/src/value/mod.rs
parentdfa4c4847c2c597300a5ee4fa80a209d2a6453e0 (diff)
feat(nix/eval): Implement builtins.groupBy r/5189
Change-Id: I3e0aa017a7100cbeb86d2e5747471b36affcc102
Reviewed-on: https://cl.tvl.fyi/c/depot/+/7038
Autosubmit: grfn <grfn@gws.fyi>
Reviewed-by: tazjin <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/value/mod.rs')
-rw-r--r--tvix/eval/src/value/mod.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/tvix/eval/src/value/mod.rs b/tvix/eval/src/value/mod.rs
index c065d5c1bd6d..14b1a5c6122e 100644
--- a/tvix/eval/src/value/mod.rs
+++ b/tvix/eval/src/value/mod.rs
@@ -79,6 +79,19 @@ macro_rules! gen_cast {
     };
 }
 
+/// Generate an `as_*_mut/to_*_mut` accessor method that returns either the
+/// expected type, or a type error.
+macro_rules! gen_cast_mut {
+    ( $name:ident, $type:ty, $expected:expr, $variant:ident) => {
+        pub fn $name(&mut self) -> Result<&mut $type, ErrorKind> {
+            match self {
+                Value::$variant(x) => Ok(x),
+                other => Err(type_error($expected, &other)),
+            }
+        }
+    };
+}
+
 /// Generate an `is_*` type-checking method.
 macro_rules! gen_is {
     ( $name:ident, $variant:pat ) => {
@@ -284,6 +297,8 @@ impl Value {
     gen_cast!(to_list, NixList, "list", Value::List(l), l.clone());
     gen_cast!(to_closure, Closure, "lambda", Value::Closure(c), c.clone());
 
+    gen_cast_mut!(as_list_mut, NixList, "list", List);
+
     gen_is!(is_path, Value::Path(_));
     gen_is!(is_number, Value::Integer(_) | Value::Float(_));
     gen_is!(is_bool, Value::Bool(_));