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 Hash trait on FixedOffset #254

Merged
merged 1 commit into from
Jun 24, 2018
Merged

Conversation

LuoZijun
Copy link

No description provided.

@quodlibetor
Copy link
Contributor

This PR doesn't actually add a Hash impl, as far as I can tell.

What is Ord supposed to mean with timezones? They form a loop, you get back to the beginning by going past the end. Can you explain your use case?

@Enet4
Copy link

Enet4 commented Jun 19, 2018

I guess that this is an attempt to conform with the C-COMMON-TRAITS guideline. This is so that users can rely on these traits (regardless of semantics, e.g. as BTreeMap key) without having to create newtypes.

But you're right, since the fixed offset timezone can be interpreted as having a cyclic ordering, deriving Ord might be confusing here, and should at least be very well documented to mean a (non-cyclic) total ordering.

@quodlibetor
Copy link
Contributor

Thanks that's a good point. I would like to have Ord and if we can't have it we should document why it isn't there. I'd like to have a solid understanding of what the use-cases are -- as is, sticking a TZ into a BTreeMap would probably lead to surprising behavior?

If we're going to add it we should not derive it, we should do a separate impl so that we can add doc-comments about the actual behavior.

Also, I forgot to say: thanks for the PR @LuoZijun!

@LuoZijun
Copy link
Author

@quodlibetor

I am using chrono to implement a built-in object Date of an ECMAScript language. This object looks like this:

#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct Datetime {
     Utc: NaiveDateTime,
     Offset: FixedOffset,
}

In the interpreter implementation, I need to store this structure in a HashMap (@Enet4 is right), so I need to implement the Hash and Eq and Ord features.

But currently our FixedOffset structure does not seem to implement these features automatically, so I need to implement it manually.

For some language reasons (I do not speak english), I'm sorry I didn't specify this on the PR

From Google Translate

@quodlibetor
Copy link
Contributor

Thanks for the explanation! Google translate is fantastic.

For just a HashMap you shouldn't need Ord, the docs clearly state that just PartialEq + Eq + Hash is required. Ord is only required for a BTreeMap. Adding Hash is a trivial thing to do, and if you just add a test that verifies that we can insert a FixedOffset into a HashMap I'm happy to accept a PR that adds that derive.

If you still want Ord then I think we'd need a variety of tests that show what the behavior actually is so that we can discuss what including it means for users. If you still want this then I think you should probably split out the Ord into a separate PR so that we can unblock your usage of Hash more quickly.

@LuoZijun LuoZijun changed the title Add Ord, Hash trait Add Hash trait on FixedOffset Jun 23, 2018
@LuoZijun
Copy link
Author

Updated.

@quodlibetor quodlibetor merged commit e165a93 into chronotope:master Jun 24, 2018
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.

3 participants