From 5a521542cd98475200662a3d1410247d2d59a9cf Mon Sep 17 00:00:00 2001 From: Jan Ferdinand Sauer Date: Thu, 12 Sep 2024 12:17:47 +0200 Subject: [PATCH] feat: Implement `Deref` for `PublicInput` This allows treating a `PublicInput` basically identically to the type it wraps, `Vec`. fix #311 --- triton-vm/src/vm.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/triton-vm/src/vm.rs b/triton-vm/src/vm.rs index 1b1c1cfb..eccd41d8 100644 --- a/triton-vm/src/vm.rs +++ b/triton-vm/src/vm.rs @@ -3,6 +3,7 @@ use std::collections::VecDeque; use std::fmt::Display; use std::fmt::Formatter; use std::fmt::Result as FmtResult; +use std::ops::Deref; use std::ops::Range; use air::table::hash::PermutationTrace; @@ -1307,7 +1308,7 @@ impl Display for VMState { .map(|reg| row[reg.main_index()]) .map(|bfe| format!("{bfe:>2}")) .join(" | "); - print_row(f, format!("ib6-0: [ {ib_registers} ]",))?; + print_row(f, format!("ib6-0: [ {ib_registers} ]"))?; let Some(ref sponge) = self.sponge else { return writeln!(f, "╰─{:─> for PublicInput { } } +impl From for Vec { + fn from(value: PublicInput) -> Self { + value.individual_tokens + } +} + impl From<&Vec> for PublicInput { fn from(tokens: &Vec) -> Self { Self::new(tokens.to_owned()) @@ -1363,6 +1370,14 @@ impl From<&[BFieldElement]> for PublicInput { } } +impl Deref for PublicInput { + type Target = [BFieldElement]; + + fn deref(&self) -> &Self::Target { + &self.individual_tokens + } +} + impl PublicInput { pub fn new(individual_tokens: Vec) -> Self { Self { individual_tokens } @@ -1555,6 +1570,7 @@ pub(crate) mod tests { assert!(public_input == (&tokens).into()); assert!(public_input == tokens[..].into()); assert!(public_input == (&tokens[..]).into()); + assert!(tokens == >::from(public_input)); assert!(PublicInput::new(vec![]) == [].into()); } @@ -3385,4 +3401,11 @@ pub(crate) mod tests { .sum::(); prop_assert_eq!(expected_dot_product, observed_dot_product); } + + #[test] + fn iterating_over_public_inputs_individual_tokens_is_easy() { + let public_input = PublicInput::from(bfe_vec![1, 2, 3]); + let actual = public_input.iter().join(", "); + assert_eq!("1, 2, 3", actual); + } }