-
Notifications
You must be signed in to change notification settings - Fork 9
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
Remove to_headers
#184
Remove to_headers
#184
Conversation
@@ -250,43 +251,40 @@ impl Program { | |||
Ok(expanded_program) | |||
} | |||
|
|||
pub fn to_instructions(&self, include_headers: bool) -> Vec<Instruction> { | |||
let mut result = vec![]; | |||
pub fn to_instructions(&self) -> Vec<Instruction> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As long as we're breaking here ... should this take self
? That's my understanding of the convention with to_x
, @Shadow53 wdyt
(For later consideration: I suppose if we want a borrowed equivalent there could be an iter_instructions
which chains these all together and thus doesn't need a clone)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to_
is actually the correct prefix here, according to the Rust API naming guidelines
(Summary: as_
== cheap borrowed, to_
== expensive operation, likely with cloning, into_
== owned-to-owned conversion)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do like the idea of a method returning an iterator. Per convention, it would probably just be called instructions()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my own learning, I tried implementing an instructions()
method that returns an iterator but ran into trouble finding a way to do it without cloning. As a slimmed down example, consider:
pub fn instructions(&'a self) -> impl Iterator<Item = &Instruction> + 'a {
self.memory_regions
.iter()
.map(|(name, descriptor)| {
Instruction::Declaration(Declaration {
name: *name,
size: descriptor.size,
sharing: descriptor.sharing,
})
}) // => Returns an Iterator with `Item = Instruction`
.chain(self.instructions.iter()) // Returns an Iterator with `Item = &Instruction`
}
Because the types of the iterators are mismatched, Rust won't compile this code. To avoid cloning self.instructions
, we have to create an iterator of references to the Declaration instructions, which can't be done with out collecting them into something like a vector first. We'd have to do the same for frames
and calibrations
as well, since we also build new Instruction
s out of their data. Maybe there is a workaround for this, but I couldn't find one after playing with it for a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only workaround I can think of is to use Cow
inside Instruction
, which is more refactoring than I think we want to do here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically just responded to @kalzoo's comments.
@@ -250,43 +251,40 @@ impl Program { | |||
Ok(expanded_program) | |||
} | |||
|
|||
pub fn to_instructions(&self, include_headers: bool) -> Vec<Instruction> { | |||
let mut result = vec![]; | |||
pub fn to_instructions(&self) -> Vec<Instruction> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do like the idea of a method returning an iterator. Per convention, it would probably just be called instructions()
.
Co-authored-by: Michael Bryant <mbryant@rigetti.com>
Cargo.lock
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
knope
typically generates and commits this, so I had to bypass the .gitignore
to push this update. This updates the crossbeam
dependency of criterion
to a newer version that hasn't been yanked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -250,43 +251,40 @@ impl Program { | |||
Ok(expanded_program) | |||
} | |||
|
|||
pub fn to_instructions(&self, include_headers: bool) -> Vec<Instruction> { | |||
let mut result = vec![]; | |||
pub fn to_instructions(&self) -> Vec<Instruction> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only workaround I can think of is to use Cow
inside Instruction
, which is more refactoring than I think we want to do here.
Closes #151
Deleted a bunch of now unreferenced snapshots too