Skip to content

Commit

Permalink
feat: Implement Deref for PublicInput
Browse files Browse the repository at this point in the history
This allows treating a `PublicInput` basically identically to the type
it wraps, `Vec<BFieldElement>`.

fix #311
  • Loading branch information
jan-ferdinand committed Sep 12, 2024
1 parent 3e488bf commit 5a52154
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion triton-vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, "╰─{:─<total_width$}─╯", "");
Expand Down Expand Up @@ -1345,6 +1346,12 @@ impl From<Vec<BFieldElement>> for PublicInput {
}
}

impl From<PublicInput> for Vec<BFieldElement> {
fn from(value: PublicInput) -> Self {
value.individual_tokens
}
}

impl From<&Vec<BFieldElement>> for PublicInput {
fn from(tokens: &Vec<BFieldElement>) -> Self {
Self::new(tokens.to_owned())
Expand All @@ -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<BFieldElement>) -> Self {
Self { individual_tokens }
Expand Down Expand Up @@ -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 == <Vec<_>>::from(public_input));

assert!(PublicInput::new(vec![]) == [].into());
}
Expand Down Expand Up @@ -3385,4 +3401,11 @@ pub(crate) mod tests {
.sum::<XFieldElement>();
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);
}
}

0 comments on commit 5a52154

Please sign in to comment.