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

Small API Improvements #102

Merged
merged 5 commits into from
Aug 22, 2018
Merged

Small API Improvements #102

merged 5 commits into from
Aug 22, 2018

Conversation

Hoverbear
Copy link
Contributor

@Hoverbear Hoverbear commented Aug 8, 2018

Some small improvements to the API.

  • Use impl Trait for returning iterators in ProgressSet.
  • ProgressSet functions should not panic on bad actions, but instead report an error.
  • Callers now explicitly panic to preserve behavior.

@Hoverbear Hoverbear mentioned this pull request Aug 8, 2018
@Hoverbear Hoverbear changed the title Small improvements Small API Improvements Aug 8, 2018
@Hoverbear Hoverbear force-pushed the small-improvements branch 4 times, most recently from 53e77aa to bafcd69 Compare August 8, 2018 21:23
@Hoverbear Hoverbear self-assigned this Aug 8, 2018
@Hoverbear Hoverbear added this to the 0.4.0 milestone Aug 8, 2018
@Hoverbear Hoverbear added the Enhancement An improvement to existing code. label Aug 8, 2018
if self.voters.insert(id, pr).is_some() {
panic!("insert voter {} twice", id);
if self.learners.contains_key(&id) {
Err(Error::Exists(id, "learners"))?;
Copy link
Contributor

Choose a reason for hiding this comment

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

any different to panic outside?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The error message will be different now. Otherwise this is just moving the error up a level so these functions can be called without worrying about panic.

siddontang
siddontang previously approved these changes Aug 9, 2018
Copy link
Contributor

@siddontang siddontang left a comment

Choose a reason for hiding this comment

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

LGTM

@Hoverbear
Copy link
Contributor Author

I rebased to fix a conflict. PTAL @breeswish

breezewish
breezewish previously approved these changes Aug 14, 2018
Copy link
Member

@breezewish breezewish left a comment

Choose a reason for hiding this comment

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

LGTM!

@breezewish
Copy link
Member

@BusyJay PTAL

@@ -55,7 +55,14 @@ quick_error! {
description(err.description())
display("protobuf error {:?}", err)
}

/// The node exists, but should not.
Exists(id: u64, set: &'static str) {
Copy link
Member

Choose a reason for hiding this comment

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

Can these two errors be merged?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hm, maybe? I think Exists(u64, &'static str, bool) could work? I worry that there might be some semantic issues. I was already not sure about combining things like VoterCannotBecomeLearner, VoterExists, LearnerExists, and LearnerAlreadyVoter.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think distinguishing them may be more clear.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think so. People usually don't care about what the exact case are they, what they do is just panic. More errors lead to longer match or repeated duplicated error1 | error 2.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@BusyJay In the case of this particular error there are definitely recoverable cases, and they are different between the errors.

If the user is lazy and wants to panic, that's fine, but we should allow our users to write good code.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am fine with this, like Rust IO error, it has NotFound or AlreadyExists error for a file.

LGTM

src/progress.rs Outdated
if !self.learners.contains_key(&id) {
Err(Error::NotExists(id, "learners"))?
}
if let Some(mut learner) = self.learners.remove(&id) {
Copy link
Member

Choose a reason for hiding this comment

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

Seems check in L154 is duplicated.

Hoverbear added 5 commits August 14, 2018 19:41
Moves failable functions to returning `Result<(), Error>` and adds
useful error types for determining the problem.

Adds an `panic` to all calling code to preserve existing behavior.
@Hoverbear Hoverbear merged commit 8b547c5 into master Aug 22, 2018
@Hoverbear Hoverbear deleted the small-improvements branch August 22, 2018 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement An improvement to existing code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants