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

Remove to_headers #184

Merged

Conversation

MarquessV
Copy link
Contributor

@MarquessV MarquessV commented Apr 6, 2023

Closes #151

Deleted a bunch of now unreferenced snapshots too

@MarquessV MarquessV changed the base branch from main to 1455-python-support-for-program-and-beyond April 6, 2023 17:42
quil-rs/src/instruction/mod.rs Outdated Show resolved Hide resolved
@@ -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> {
Copy link
Contributor

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)

Copy link
Contributor

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)

Copy link
Contributor

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().

Copy link
Contributor Author

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 Instructions out of their data. Maybe there is a workaround for this, but I couldn't find one after playing with it for a bit.

Copy link
Contributor

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.

Copy link
Contributor

@Shadow53 Shadow53 left a 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.

quil-rs/src/instruction/mod.rs Outdated Show resolved Hide resolved
@@ -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> {
Copy link
Contributor

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().

Cargo.lock Outdated
Copy link
Contributor Author

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.

Copy link
Contributor

@Shadow53 Shadow53 left a 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> {
Copy link
Contributor

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.

@MarquessV MarquessV merged commit 846b203 into 1455-python-support-for-program-and-beyond Apr 10, 2023
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.

to_headers should be removed
3 participants