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

Replace side-effecting unwrap_or_else by if let in constellation. #10345

Merged
merged 1 commit into from Apr 15, 2016

Conversation

@asajeffrey
Copy link
Member

asajeffrey commented Apr 1, 2016

This addresses @nox's comments in #10295.


This change is Reviewable

@nox
Copy link
Member

nox commented Apr 3, 2016

-S-awaiting-review +S-needs-code-changes


Reviewed 1 of 1 files at r1.
Review status: all files reviewed at latest revision, 14 unresolved discussions.


components/compositing/constellation.rs, line 717 [r1] (raw file):
This code isn't the same, now you don't call anymore handle_send_error when the pipeline couldn't be found at all. I guess that's intended though?

In that case, I would do:

if let Some(pipeline) = self.pipelines.get(&pipeline_id) {
    if let Err(e) = pipeline.script_chan.send(msg) {
        self.handle_send_error(pipeline_id, e);
    }
} else {
    debug!(...);
    return true;
}

components/compositing/constellation.rs, line 729 [r1] (raw file):
I don't hate all combinators in all circumstances, the map_or is fine here. :)


components/compositing/constellation.rs, line 908 [r1] (raw file):
Just rewrite the whole function:

if let Some(pipeline) = self.pipelines.get_mut(&pipeline_id) {
    pipeline.size = Some(*size);
    if let Err(e) = pipeline.script_chan.send(msg) {
        self.handle_send_error(pipeline_id, e);
    }
}

components/compositing/constellation.rs, line 930 [r1] (raw file):

if let Some(pipeline) = self.pipelines.get(&subframe_parent_id) {
    if let Err(e) = pipeline.script_chan.send(msg) {
        self.handle_send_error(subframe_parent_id, e);
    }
} else {
    debug!("Pipeline {:?} subframe loaded after closure.", subframe_parent_id)
    return;
}

components/compositing/constellation.rs, line 1030 [r1] (raw file):
Again, it seems to me that you can just rewrite the whole function:

let pipeline = match self.pipelines.get(&pipeline_id) {
    Some(pipeline) => pipeline,
    None => {
        debug!(...);
        return;
    }
};

let result = match tick_type {
    AnimationTickType::Script => {
        pipeline.script_chan.0.send(ConstellationControlMsg::TickAllAnimations(pipeline_id))
    },
    AnimationTickType::Layout => {
        pipeline.layout_chan.0.send(LayoutControlMsg::TickAnimations)
    }
};

if let Err(e) = result {
    self.handle_send_error(pipeline_id, e);
}

I really think we should have a specific function on pipelines, which would take a channel and a message and would call handle_send_error itself. We don't even distinguish whether it's script_chan or layout_chan that encountered an error in this case.


components/compositing/constellation.rs, line 1042 [r1] (raw file):
Why not just:

let (parent_info, window_size) =
    self.pipelines.get(&source_id)
        .map(|source| (source.parent_info, source.size))
        .unwrap_or((None, None));

components/compositing/constellation.rs, line 1058 [r1] (raw file):
As usual, I think this idiom is better conveyed as two nested if let without intermediate results.


components/compositing/constellation.rs, line 1083 [r1] (raw file):
I preferred the original way, where both parent_info and window_size were bound at the same moment.


components/compositing/constellation.rs, line 1235 [r1] (raw file):
As usual, I think this idiom is better conveyed as two nested if let without intermediate results.


components/compositing/constellation.rs, line 1253 [r1] (raw file):
An if let expression here would decrease the indentation, and thus look nicer.


components/compositing/constellation.rs, line 1262 [r1] (raw file):
As usual, I think this idiom is better conveyed as two nested if let without intermediate results.


components/compositing/constellation.rs, line 1338 [r1] (raw file):
As usual, I think this idiom is better conveyed as two nested if let without intermediate results.


components/compositing/constellation.rs, line 1411 [r1] (raw file):
As usual, I think this idiom is better conveyed as two nested if let without intermediate results.


components/compositing/constellation.rs, line 1431 [r1] (raw file):
What happens if that one send call fails, btw?


Comments from Reviewable

@asajeffrey
Copy link
Member Author

asajeffrey commented Apr 4, 2016

We have different opinions about coding style, quel surprise.


Review status: all files reviewed at latest revision, 14 unresolved discussions.


components/compositing/constellation.rs, line 717 [r1] (raw file):
Done.


components/compositing/constellation.rs, line 729 [r1] (raw file):
OK, I don't see why, doesn't the debug! count as a side-effect?


components/compositing/constellation.rs, line 908 [r1] (raw file):
Does your version pass the borrow checker?


components/compositing/constellation.rs, line 930 [r1] (raw file):
Ditto.


components/compositing/constellation.rs, line 1030 [r1] (raw file):
Done.


components/compositing/constellation.rs, line 1042 [r1] (raw file):
Done.


components/compositing/constellation.rs, line 1058 [r1] (raw file):
The problem with the nested-if idiom is that the code gradually drifts right, due to indentation. You also end up with spurious diffs whenever the indentation changes, which makes code maintenance annoying.

Also in this case, borowchk.


components/compositing/constellation.rs, line 1083 [r1] (raw file):
Done.


components/compositing/constellation.rs, line 1235 [r1] (raw file):
Ditto.


components/compositing/constellation.rs, line 1253 [r1] (raw file):
I prefer match to if let in the case that there's an else branch. The extra indentation makes the code easier to read, IMO. YMMV.


components/compositing/constellation.rs, line 1262 [r1] (raw file):
Ditto.


components/compositing/constellation.rs, line 1338 [r1] (raw file):
Ditto.


components/compositing/constellation.rs, line 1411 [r1] (raw file):
Ditto.


components/compositing/constellation.rs, line 1431 [r1] (raw file):
The API for the compositor proxy doesn't return a Result.


Comments from Reviewable

@asajeffrey
Copy link
Member Author

asajeffrey commented Apr 4, 2016

@nox: I reverted some of your suggested changes, since I couldn't get them to pass borrowchk. Serves me right for checking in changes without compiling them!

@nox
Copy link
Member

nox commented Apr 5, 2016

Review status: 0 of 1 files reviewed at latest revision, 14 unresolved discussions.


components/compositing/constellation.rs, line 717 [r1] (raw file):
So was there a borrowck problem here if you remove the intermediate result value?


components/compositing/constellation.rs, line 729 [r1] (raw file):
Because that's not the main point of that snippet, we do need the result value, which when the clipboard is broken is an empty string.


components/compositing/constellation.rs, line 908 [r1] (raw file):
I don't see what can make it fail. :/


Comments from Reviewable

@asajeffrey
Copy link
Member Author

asajeffrey commented Apr 5, 2016

Review status: 0 of 1 files reviewed at latest revision, 14 unresolved discussions.


components/compositing/constellation.rs, line 717 [r1] (raw file):
Yes, the pipeline is borrowed from self.pipelines, which means self.handle_send_error can't be called whole pipeline is in scope.


components/compositing/constellation.rs, line 729 [r1] (raw file):
Done.


components/compositing/constellation.rs, line 908 [r1] (raw file):
Calling self.handle_send_error while pipeline is still in scope. Pipeline is borrowed from self.pipelines, and self.handle_send_error needs &mut self.


Comments from Reviewable

@asajeffrey
Copy link
Member Author

asajeffrey commented Apr 11, 2016

Rebased and squashed.

@bors-servo
Copy link
Contributor

bors-servo commented Apr 12, 2016

The latest upstream changes (presumably #10224) made this pull request unmergeable. Please resolve the merge conflicts.

@asajeffrey asajeffrey force-pushed the asajeffrey:make-nox-happy branch from ef84e1f to 0afb803 Apr 12, 2016
@asajeffrey
Copy link
Member Author

asajeffrey commented Apr 12, 2016

Rebased.

@bors-servo
Copy link
Contributor

bors-servo commented Apr 13, 2016

The latest upstream changes (presumably #10489) made this pull request unmergeable. Please resolve the merge conflicts.

@asajeffrey asajeffrey force-pushed the asajeffrey:make-nox-happy branch from 0afb803 to 05f1655 Apr 13, 2016
@asajeffrey
Copy link
Member Author

asajeffrey commented Apr 13, 2016

Rebased. @nox: this is bitrotting.

@jdm jdm removed the S-needs-rebase label Apr 14, 2016
@bors-servo
Copy link
Contributor

bors-servo commented Apr 14, 2016

The latest upstream changes (presumably #10587) made this pull request unmergeable. Please resolve the merge conflicts.

This addresses nox's comments in #10295.
@asajeffrey asajeffrey force-pushed the asajeffrey:make-nox-happy branch from 05f1655 to 0cca4a9 Apr 14, 2016
@asajeffrey
Copy link
Member Author

asajeffrey commented Apr 14, 2016

Another rebase. Serious bitrotting here :(

@nox
Copy link
Member

nox commented Apr 15, 2016

Sorry about this @asajeffrey. Looks good to me with your last remarks and changes.

@bors-servo r+

@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

📌 Commit 0cca4a9 has been approved by nox

@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

Testing commit 0cca4a9 with merge 6c5cfb7...

bors-servo added a commit that referenced this pull request Apr 15, 2016
Replace side-effecting unwrap_or_else by if let in constellation.

This addresses @nox's comments in #10295.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10345)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Apr 15, 2016

@bors-servo bors-servo merged commit 0cca4a9 into servo:master Apr 15, 2016
3 checks passed
3 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
@asajeffrey asajeffrey deleted the asajeffrey:make-nox-happy branch Apr 15, 2016
@asajeffrey
Copy link
Member Author

asajeffrey commented Apr 15, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

8 participants
You can’t perform that action at this time.