Skip to content

Commit 1ca2498

Browse files
committed
value: move iterator methods from Value to ValueRef
These all conceptually work on references to values, so they should be available from a ValueRef.
1 parent 38c5776 commit 1ca2498

1 file changed

Lines changed: 32 additions & 9 deletions

File tree

src/value.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl DagLike for ValueRef<'_> {
101101
}
102102
}
103103

104-
impl ValueRef<'_> {
104+
impl<'v> ValueRef<'v> {
105105
/// Check if the value is a unit.
106106
pub fn is_unit(&self) -> bool {
107107
self.ty.is_unit()
@@ -196,6 +196,34 @@ impl ValueRef<'_> {
196196
n,
197197
})
198198
}
199+
200+
/// Yields an iterator over the "raw bytes" of the value.
201+
///
202+
/// The returned bytes match the padded bit-encoding of the value. You
203+
/// may wish to call [`Self::iter_padded`] instead to obtain the bits,
204+
/// but this method is more efficient in some contexts.
205+
pub fn raw_byte_iter(&self) -> RawByteIter<'v> {
206+
RawByteIter {
207+
value: *self,
208+
yielded_bytes: 0,
209+
}
210+
}
211+
212+
/// Return an iterator over the compact bit encoding of the value.
213+
///
214+
/// This encoding is used for writing witness data and for computing IHRs.
215+
pub fn iter_compact(&self) -> CompactBitsIter<'v> {
216+
CompactBitsIter::new(*self)
217+
}
218+
219+
/// Return an iterator over the padded bit encoding of the value.
220+
///
221+
/// This encoding is used to represent the value in the Bit Machine.
222+
pub fn iter_padded(&self) -> PreOrderIter<'v> {
223+
PreOrderIter {
224+
inner: BitIter::new(self.raw_byte_iter()).take(self.ty.bit_width()),
225+
}
226+
}
199227
}
200228

201229
pub struct RawByteIter<'v> {
@@ -591,26 +619,21 @@ impl Value {
591619
/// may wish to call [`Self::iter_padded`] instead to obtain the bits,
592620
/// but this method is more efficient in some contexts.
593621
pub fn raw_byte_iter(&self) -> RawByteIter<'_> {
594-
RawByteIter {
595-
value: self.as_ref(),
596-
yielded_bytes: 0,
597-
}
622+
self.as_ref().raw_byte_iter()
598623
}
599624

600625
/// Return an iterator over the compact bit encoding of the value.
601626
///
602627
/// This encoding is used for writing witness data and for computing IHRs.
603628
pub fn iter_compact(&self) -> CompactBitsIter<'_> {
604-
CompactBitsIter::new(self.as_ref())
629+
self.as_ref().iter_compact()
605630
}
606631

607632
/// Return an iterator over the padded bit encoding of the value.
608633
///
609634
/// This encoding is used to represent the value in the Bit Machine.
610635
pub fn iter_padded(&self) -> PreOrderIter<'_> {
611-
PreOrderIter {
612-
inner: BitIter::new(self.raw_byte_iter()).take(self.ty.bit_width()),
613-
}
636+
self.as_ref().iter_padded()
614637
}
615638

616639
/// Check if the value is of the given type.

0 commit comments

Comments
 (0)