Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StackOutputs: get_stack_{item, word}() methods #1155

Merged
merged 8 commits into from
Nov 27, 2023

Conversation

plafer
Copy link
Contributor

@plafer plafer commented Nov 21, 2023

Followup to discussions in #1146

Copy link
Contributor

@frisitano frisitano left a comment

Choose a reason for hiding this comment

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

Looks good, thanks! Added a question in regards to whether we should modify the api to return errors when accessing data which is out of bounds of the stack? What are your thoughts? I'm not strongly opinionated but wonder if you have any thoughts on this?

Comment on lines 91 to 94
/// Returns the value located at the specified position on the stack
pub fn get_stack_item(&self, idx: usize) -> Option<Felt> {
self.stack.get(idx).map(|&felt| felt.into())
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if this function should be fallible. If we try and access an element with idx > self.stack.len() - 1 then we should return an error?

Copy link
Contributor Author

@plafer plafer Nov 22, 2023

Choose a reason for hiding this comment

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

I prefer to return an Option when there's only one error possible (here: out of bounds). If we returned OutputError for example, and a caller wanted to match on the error, then they would need to handle error cases that could never happen. Then, if we create a new error type with one variant, I consider that "type bloat" because it doesn't convey any more information than an Option would, but introduces a new name.

I believe this is a convention widely used in the standard library, such as with slice::get.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated the comments to document what None means here

Comment on lines +97 to +110
pub fn get_stack_word(&self, idx: usize) -> Option<Word> {
let word_elements: Word = {
let word_elements: Vec<Felt> = range(idx, 4)
.map(|idx| self.get_stack_item(idx))
// Elements need to be reversed, since a word `[a, b, c, d]` will be stored on the
// stack as `[d, c, b, a]`
.rev()
.collect::<Option<_>>()?;

word_elements.try_into().expect("a Word contains 4 elements")
};

Some(word_elements)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Similar to above, I wonder if this function should be fallible in the case we try and access a word which is out of bounds?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Above comment applies

@@ -1,3 +1,7 @@
use miden_crypto::Word;

Copy link
Contributor

Choose a reason for hiding this comment

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

typically we try and avoid spacing between imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This can be solved by the group_imports section of rustfmt.toml. I see that it's been commented out, and the reason seems to be that the CI complained that the features were on nightly only. This is solved by configuring the CI to use nightly rust for the cargo fmt command. I will open another PR for it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

core/src/stack/outputs.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@bobbinth bobbinth left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you! I left a couple of minor nits inline. After these, let's squash the commits and merge.

core/src/stack/outputs.rs Outdated Show resolved Hide resolved
core/src/stack/outputs.rs Outdated Show resolved Hide resolved
@bobbinth
Copy link
Contributor

Also, let's add an entry to the changelog for this.

@plafer
Copy link
Contributor Author

plafer commented Nov 27, 2023

Added changelog entry under "VM Internals" header

@plafer plafer merged commit fc81592 into next Nov 27, 2023
15 checks passed
@plafer plafer deleted the plafer-stackoutputs-get-felt-at branch November 27, 2023 13:56
bobbinth pushed a commit that referenced this pull request Dec 13, 2023
* get_stack_item()

* get_stack_word

* adjust comment

* adjust comments

* fix comment

* fix comments
bobbinth pushed a commit that referenced this pull request Feb 26, 2024
* get_stack_item()

* get_stack_word

* adjust comment

* adjust comments

* fix comment

* fix comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants