Skip to content

Commit

Permalink
reviews: Docs and avoid allocation when formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
igamigo committed Jan 20, 2025
1 parent 07c5e27 commit b7672e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
11 changes: 7 additions & 4 deletions objects/src/accounts/component/template/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ impl Deserializable for AccountComponentTemplate {
/// index 0.
/// - Storage slots are laid out in a contiguous manner.
/// - Storage placeholders can appear multiple times, but only if the expected [StorageValue] is of
/// the same type in all instances.
/// the same type in all instances. The expected placeholders can be retrieved with
/// [AccountComponentMetadata::get_unique_storage_placeholders()], which returns a map from
/// [StoragePlaceholder] to [PlaceholderType] (which, in turn, indicates the expected value type
/// for the placeholder).
///
/// # Example
///
Expand Down Expand Up @@ -193,7 +196,7 @@ impl AccountComponentMetadata {

/// Retrieves a map of unique storage placeholders mapped to their expected type that require
/// a value at the moment of component instantiation. These values will be used for
/// initializing storage slot values, or storage map entries.For a full example on how a
/// initializing storage slot values, or storage map entries. For a full example on how a
/// placeholder may be utilized, please refer to the docs for [AccountComponentMetadata].
///
/// Types for the returned storage placeholders are inferred based on their location in the
Expand Down Expand Up @@ -240,7 +243,7 @@ impl AccountComponentMetadata {
/// # Errors
///
/// - If the specified storage slots contain duplicates.
/// - If the template contains multiple storage placeholders with of different type.
/// - If the template contains multiple storage placeholders of different type.
/// - If the slot numbers do not start at zero.
/// - If the slots are not contiguous.
fn validate(&self) -> Result<(), AccountComponentTemplateError> {
Expand Down Expand Up @@ -299,7 +302,7 @@ impl AccountComponentMetadata {
Ok(())
}

/// Validates map keys.
/// Validates map keys by checking for duplicates.
/// Because keys can be represented in a variety of ways, the `to_string()` implementation is
/// used to check for duplicates.
fn validate_map_keys(&self) -> Result<(), AccountComponentTemplateError> {
Expand Down
22 changes: 13 additions & 9 deletions objects/src/accounts/component/template/storage/value_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use alloc::{boxed::Box, collections::BTreeSet, string::ToString, vec::Vec};
use alloc::{boxed::Box, collections::BTreeSet, vec::Vec};

use vm_core::{
utils::{ByteReader, ByteWriter, Deserializable, Serializable},
Expand Down Expand Up @@ -138,15 +138,15 @@ impl core::fmt::Display for WordRepresentation {
WordRepresentation::Value(hex) => f.write_str(&Digest::from(hex).to_hex()),
WordRepresentation::Array(array) => {
f.write_str("[")?;
f.write_str(&format!("{}, ", array[0]))?;
f.write_str(&format!("{}, ", array[1]))?;
f.write_str(&format!("{}, ", array[2]))?;
f.write_str(&format!("{}, ", array[3]))?;
f.write_fmt(format_args!("{}, ", array[0]))?;
f.write_fmt(format_args!("{}, ", array[1]))?;
f.write_fmt(format_args!("{}, ", array[2]))?;
f.write_fmt(format_args!("{}, ", array[3]))?;

f.write_str("]")
},
WordRepresentation::Template(storage_placeholder) => {
f.write_str(&storage_placeholder.to_string())
f.write_fmt(format_args!("{}", storage_placeholder))
},
}
}
Expand Down Expand Up @@ -259,10 +259,14 @@ impl Deserializable for FeltRepresentation {
impl core::fmt::Display for FeltRepresentation {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
FeltRepresentation::Hexadecimal(base_element) => f.write_str(&base_element.to_string()),
FeltRepresentation::Decimal(base_element) => f.write_str(&base_element.to_string()),
FeltRepresentation::Hexadecimal(base_element) => {
f.write_fmt(format_args!("{}", base_element))
},
FeltRepresentation::Decimal(base_element) => {
f.write_fmt(format_args!("{}", base_element))
},
FeltRepresentation::Template(storage_placeholder) => {
f.write_str(&storage_placeholder.to_string())
f.write_fmt(format_args!("{}", storage_placeholder))
},
}
}
Expand Down

0 comments on commit b7672e4

Please sign in to comment.