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

Add PartialEq/Eq, Add TimeUnitSpecEq #78

Merged
merged 4 commits into from
Feb 18, 2021

Conversation

koenichiwa
Copy link
Contributor

Fixes #61

Not sure about the name TimeIUnitSpecEq, but that can be changed.

src/schedule.rs Outdated

impl PartialEq for TimeUnitSpecEq {
fn eq(&self, other: &TimeUnitSpecEq) -> bool{
self.0.fields == other.0.fields
Copy link
Owner

Choose a reason for hiding this comment

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

Rather than vending a new type that achieves this, how would you feel about having something like:

impl Schedule {
  // ... 
  pub fn ordinals_eq(other: &Schedule) -> bool {
    self.fields == other.fields
  }
  // ...
}

and allowing users to define their own wrapper type that implements PartialEq in terms of ordinals_eq? That alleviates the pressure to provide a type with a perfect name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure no problem

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nitpick: Maybe we should call it timeunitspec_eq? Because Ordinals is not exposed?

Copy link
Owner

Choose a reason for hiding this comment

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

Nitpick: Maybe we should call it timeunitspec_eq? Because Ordinals is not exposed?

Oh, good point! I wish I liked the type name TimeUnitSpec more, but we'll still have time to revise that before 1.0 if I can think of something better. Let's go with timeunitspec_eq.

@@ -5,7 +5,7 @@ use once_cell::sync::Lazy;

static ALL: Lazy<OrdinalSet> = Lazy::new(|| { DaysOfMonth::supported_ordinals() });

#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
Copy link
Owner

Choose a reason for hiding this comment

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

Deriving PartialEq means that some equivalent representations of "all" may not be found to be equal. (e.g. * and ? would be considered equal, but * and 1-7 for DayOfWeek would not.)

We could address this by either:

  • Manually implementing PartialEq and checking for the is_all() case OR
  • Adding code to the parsing logic that checks to see if we're about to call from_ordinal_set() using an ordinal set that's effectively "all" and using all() instead, guaranteeing that the internal representation is the same.

I think I'd prefer the former as it consolidates the definition of PartialEq into a single function while the latter relies on knowing about an invariant in the parser. It seems like we could do a single blanket impl of PartialEq?

impl <T> PartialEq for T where T: TimeUnitField { /*....*/ }

With that in place, ScheduleFields could use derive(PartialEq, Eq).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, good catch! I can do with both ideas, so I'll work on the former

Copy link
Contributor Author

@koenichiwa koenichiwa Feb 18, 2021

Choose a reason for hiding this comment

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

Sadly it seems that

impl <T> PartialEq for T where T: TimeUnitField { /*....*/ }

runs into some conflicts. I can go forward and implement it individually though
see: https://stackoverflow.com/questions/62904267/conflicting-implementation-error-with-single-implementation

Copy link
Owner

Choose a reason for hiding this comment

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

Oof, I've stubbed my toe on that more than once and still forgot about it.

Copy link
Owner

@zslayton zslayton left a comment

Choose a reason for hiding this comment

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

Nice!

@koenichiwa
Copy link
Contributor Author

koenichiwa commented Feb 18, 2021

Great, I think that'll be my last PR this cycle. Maybe I can work on #77 , but otherwise I'm done.

This was a great way of getting to know rust

@zslayton zslayton merged commit 7752df7 into zslayton:master Feb 18, 2021
@zslayton
Copy link
Owner

Great, I think that'll be my last PR this cycle. Maybe I can work on #77 , but otherwise I'm done.

I'll take a look at #77 in closer detail later this evening. Thanks for all of your contributions, I appreciate it!

@koenichiwa
Copy link
Contributor Author

No problem, it was fun!

I can open a draft pull request with the code I have up until now if you want 🙂🤔

@koenichiwa koenichiwa deleted the feature/partialeq-eq branch February 23, 2021 16:10
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.

PartialEq and Eq for the Schedule type
2 participants