about summary refs log tree commit diff
path: root/tvix/eval/src/chunk.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-09-04T13·49+0300
committertazjin <tazjin@tvl.su>2022-09-09T21·10+0000
commit1fe6cfe5a279cd19dbb2586f30db6a8790db7a4d (patch)
tree36f23709803af2112a7bc5a205ce68bb44f33d49 /tvix/eval/src/chunk.rs
parent8c64ebe074c9fc99c2174769f5e3c13755b82c55 (diff)
refactor(tvix/eval): index into Chunk with ConstantIdx/CodeIdx r/4773
This is a step towards hiding the internal fields of thunk, and making
the interface of the type more predictable.

Part of the preparation for implementing observers.

Change-Id: I1a88a96419c72eb9e2332b56a2dd94afa47e6f88
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6447
Reviewed-by: sterni <sternenseemann@systemli.org>
Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/chunk.rs')
-rw-r--r--tvix/eval/src/chunk.rs22
1 files changed, 18 insertions, 4 deletions
diff --git a/tvix/eval/src/chunk.rs b/tvix/eval/src/chunk.rs
index c89e6ef84810..a4d6be752b91 100644
--- a/tvix/eval/src/chunk.rs
+++ b/tvix/eval/src/chunk.rs
@@ -1,3 +1,5 @@
+use std::ops::Index;
+
 use crate::opcode::{CodeIdx, ConstantIdx, OpCode};
 use crate::value::Value;
 
@@ -31,6 +33,22 @@ pub struct Chunk {
     pub codemap: std::rc::Rc<codemap::CodeMap>,
 }
 
+impl Index<ConstantIdx> for Chunk {
+    type Output = Value;
+
+    fn index(&self, index: ConstantIdx) -> &Self::Output {
+        &self.constants[index.0]
+    }
+}
+
+impl Index<CodeIdx> for Chunk {
+    type Output = OpCode;
+
+    fn index(&self, index: CodeIdx) -> &Self::Output {
+        &self.code[index.0]
+    }
+}
+
 impl Chunk {
     pub fn push_op(&mut self, data: OpCode, span: codemap::Span) -> CodeIdx {
         let idx = self.code.len();
@@ -45,10 +63,6 @@ impl Chunk {
         ConstantIdx(idx)
     }
 
-    pub fn constant(&self, idx: ConstantIdx) -> &Value {
-        &self.constants[idx.0]
-    }
-
     // Span tracking implementation
 
     fn push_span(&mut self, span: codemap::Span) {