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 impl From<Ordering> for i32 #89491

Closed
wants to merge 1 commit into from
Closed

Add impl From<Ordering> for i32 #89491

wants to merge 1 commit into from

Conversation

lopopolo
Copy link
Contributor

@lopopolo lopopolo commented Oct 3, 2021

This commit adds a first-class From conversion of Ordering into i32.

Ordering is defined to be compatible with the return types of the various ...cmp... routines in the C standard library, but the only way to convert an Ordering to an i32 is with a non-obvious as cast.

This commit adds a proper API for doing this and hides a "scary" as cast inside the core library.

This commit adds a first-class `From` conversion of `Ordering` into
`i32`.

`Ordering` is defined to be compatible with the return types of the
various `...cmp...` routines in the C standard library, but the only way
to convert an `Ordering` to an `i32` is with a non-obvious `as` cast.

This commit adds a proper API for doing this and hides a "scary" `as`
cast inside the `core` library.
@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 3, 2021
@joshtriplett joshtriplett added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label Oct 4, 2021
@joshtriplett
Copy link
Member

Tagging libs-api.

This would be insta-stable, so it needs team review.

@lopopolo Where is Ordering documented to be compatible with the return value of strcmp/memcmp/etc? Converting an Ordering to such a value doesn't seem like a particularly common use case.

@lopopolo
Copy link
Contributor Author

lopopolo commented Oct 4, 2021

Where is Ordering documented to be compatible with the return value of strcmp/memcmp/etc?

😅 I didn't say this was documented. I meant to say that Ordering is defined in the source to have the same semantics as the int return value/sign (-1/0/1) of these functions.

I'm implementing several "cmp" style routines as part of an oxidation project and being able to turn a nicely typed Ordering into the int expected at the FFI boundary would be something I'd like to have.

@lopopolo
Copy link
Contributor Author

lopopolo commented Oct 4, 2021

Converting an Ordering to such a value doesn't seem like a particularly common use case.

I was of the impression that such a conversion being common was not a requirement for adding From impls to core and std. I think I have a fairly clear use case and such a From impl would remove a little bit of friction here for little cost.

This behavior is already possible with an as cast and is already relied upon for the implementations of Ord and PartialEq for Ordering. This change just adds a discoverable API and makes this conversion less scary than an as cast.

@joshtriplett
Copy link
Member

It's not that it needs to be common, it's that the usefulness needs to outweigh the potential for an error-prone conversion. .into() is non-specific, and can sometimes result in an unexpected conversion.

There's a proposal in #81642 to add derive(FromRepr) and automatically derive AsRepr, which would serve the same purpose, and I think that'd be more self-explanatory.

I would propose that we merge #81642 (in some form) and then Ordering will gain an as_repr() method for this.

@lopopolo
Copy link
Contributor Author

lopopolo commented Oct 4, 2021

There's a proposal in #81642 to add derive(FromRepr) and automatically derive AsRepr, which would serve the same purpose, and I think that'd be more self-explanatory.

ooooooh I didn't know about this proposal. That looks like it would do the trick. One sticking point is it doesn't look like Ordering has a #[repr(...)] attribute:

#[derive(Clone, Copy, PartialEq, Debug, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
pub enum Ordering {
/// An ordering where a compared value is less than another.
#[stable(feature = "rust1", since = "1.0.0")]
Less = -1,
/// An ordering where a compared value is equal to another.
#[stable(feature = "rust1", since = "1.0.0")]
Equal = 0,
/// An ordering where a compared value is greater than another.
#[stable(feature = "rust1", since = "1.0.0")]
Greater = 1,
}

I would propose that we merge #81642 (in some form) and then Ordering will gain an as_repr() method for this.

This sounds good to me. Do we need to cut another ticket to add a repr to ordering?

@joshtriplett
Copy link
Member

@lopopolo Yes, please do submit a PR adding repr(i8) to Ordering, and I'd be happy to FCP it with libs.

Also, I submitted rust-lang/nomicon#315 to clarify that adding such a repr should not cause a problem with the actual size of the type.

@lopopolo
Copy link
Contributor Author

lopopolo commented Oct 4, 2021

thanks for the help @joshtriplett. The new PR is here:

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Oct 16, 2021
… r=joshtriplett

Add `#[repr(i8)]` to `Ordering`

Followup to rust-lang#89491 to allow `Ordering` to auto-derive `AsRepr` once the proposal to add `AsRepr` (rust-lang#81642) lands.

cc `@joshtriplett`
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Oct 16, 2021
… r=joshtriplett

Add `#[repr(i8)]` to `Ordering`

Followup to rust-lang#89491 to allow `Ordering` to auto-derive `AsRepr` once the proposal to add `AsRepr` (rust-lang#81642) lands.

cc ``@joshtriplett``
@lopopolo lopopolo deleted the lopopolo/from-ordering-for-i32 branch October 9, 2022 00:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants