Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions crates/rust-client/src/rpc/domain/digest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt::{self, Debug, Display, Formatter};
use core::fmt::{self, Debug, Display, Formatter, Write};

use hex::ToHex;
use miden_objects::note::NoteId;
Expand Down Expand Up @@ -36,21 +35,17 @@ impl ToHex for &proto::primitives::Digest {

impl ToHex for proto::primitives::Digest {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
let mut data: Vec<char> = Vec::with_capacity(Word::SERIALIZED_SIZE);
data.extend(format!("{:016x}", self.d0).chars());
data.extend(format!("{:016x}", self.d1).chars());
data.extend(format!("{:016x}", self.d2).chars());
data.extend(format!("{:016x}", self.d3).chars());
data.into_iter().collect()
let mut data = String::with_capacity(Word::SERIALIZED_SIZE * 2);
write!(&mut data, "{:016x}{:016x}{:016x}{:016x}", self.d0, self.d1, self.d2, self.d3)
.unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try and avoid this unwrap here? I was not needed before

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we try and avoid this unwrap here? I was not needed before

Yes, did it!

data.chars().collect()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
let mut data: Vec<char> = Vec::with_capacity(Word::SERIALIZED_SIZE);
data.extend(format!("{:016X}", self.d0).chars());
data.extend(format!("{:016X}", self.d1).chars());
data.extend(format!("{:016X}", self.d2).chars());
data.extend(format!("{:016X}", self.d3).chars());
data.into_iter().collect()
let mut data = String::with_capacity(Word::SERIALIZED_SIZE * 2);
write!(&mut data, "{:016X}{:016X}{:016X}{:016X}", self.d0, self.d1, self.d2, self.d3)
.unwrap();
data.chars().collect()
}
}

Expand Down
40 changes: 25 additions & 15 deletions crates/rust-client/src/rpc/domain/word.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::{string::String, vec::Vec};
use core::fmt::{self, Display, Formatter};
use alloc::string::String;
use core::fmt::{self, Display, Formatter, Write};

use hex::ToHex;
use miden_objects::{Felt, StarkField, Word, note::NoteId};
Expand All @@ -9,7 +9,7 @@ use crate::rpc::{errors::RpcConversionError, generated::word};
// CONSTANTS
// ================================================================================================

pub const WORD_DATA_SIZE: usize = 32;
pub const WORD_DATA_SIZE: usize = Word::SERIALIZED_SIZE * 2;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any correlation between WORD_DATA_SIZE (hex representation size) and Word::SERIALIZED_SIZE? Or does it just coincidentally occupy twice the size?


// FORMATTING
// ================================================================================================
Expand All @@ -32,21 +32,31 @@ impl ToHex for &word::Word {

impl ToHex for word::Word {
fn encode_hex<T: FromIterator<char>>(&self) -> T {
let mut data: Vec<char> = Vec::with_capacity(WORD_DATA_SIZE);
data.extend(format!("{:016x}", self.w0).chars());
data.extend(format!("{:016x}", self.w1).chars());
data.extend(format!("{:016x}", self.w2).chars());
data.extend(format!("{:016x}", self.w3).chars());
data.into_iter().collect()
let mut data = String::with_capacity(WORD_DATA_SIZE);
write!(
&mut data,
"{:016x}{:016x}{:016x}{:016x}",
self.w0,
self.w1,
self.w2,
self.w3
)
.unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a safety comment explaining why this is OK to unwrap

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a safety comment explaining why this is OK to unwrap

added comments

data.chars().collect()
}

fn encode_hex_upper<T: FromIterator<char>>(&self) -> T {
let mut data: Vec<char> = Vec::with_capacity(WORD_DATA_SIZE);
data.extend(format!("{:016X}", self.w0).chars());
data.extend(format!("{:016X}", self.w1).chars());
data.extend(format!("{:016X}", self.w2).chars());
data.extend(format!("{:016X}", self.w3).chars());
data.into_iter().collect()
let mut data = String::with_capacity(WORD_DATA_SIZE);
write!(
&mut data,
"{:016X}{:016X}{:016X}{:016X}",
self.w0,
self.w1,
self.w2,
self.w3
)
.unwrap();
data.chars().collect()
}
}

Expand Down
Loading