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

Tracking issue for floats ↔ bytes conversions #60446

Closed
SimonSapin opened this issue May 1, 2019 · 8 comments · Fixed by #66002
Closed

Tracking issue for floats ↔ bytes conversions #60446

SimonSapin opened this issue May 1, 2019 · 8 comments · Fixed by #66002
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@SimonSapin
Copy link
Contributor

SimonSapin commented May 1, 2019

#58756 #62366 adds:

impl f32 {
    pub const fn to_be_bytes(self) -> [u8; 4] { self.to_bits().to_be_bytes() }
    pub const fn to_le_bytes(self) -> [u8; 4] { self.to_bits().to_le_bytes() }
    pub const fn to_ne_bytes(self) -> [u8; 4] { self.to_bits().to_ne_bytes() }
    pub const fn from_be_bytes(bytes: [u8; 4]) -> Self { Self::from_bits(u32::from_be_bytes(bytes)) }
    pub const fn from_le_bytes(bytes: [u8; 4]) -> Self { Self::from_bits(u32::from_le_bytes(bytes)) }
    pub const fn from_ne_bytes(bytes: [u8; 4]) -> Self { Self::from_bits(u32::from_ne_bytes(bytes)) }
}

impl f64 {
    pub const fn to_be_bytes(self) -> [u8; 8] { self.to_bits().to_be_bytes() }
    pub const fn to_le_bytes(self) -> [u8; 8] { self.to_bits().to_le_bytes() }
    pub const fn to_ne_bytes(self) -> [u8; 8] { self.to_bits().to_ne_bytes() }
    pub const fn from_be_bytes(bytes: [u8; 8]) -> Self { Self::from_bits(u64::from_be_bytes(bytes)) }
    pub const fn from_le_bytes(bytes: [u8; 8]) -> Self { Self::from_bits(u64::from_le_bytes(bytes)) }
    pub const fn from_ne_bytes(bytes: [u8; 8]) -> Self { Self::from_bits(u64::from_ne_bytes(bytes)) }
}

PR discussion summary: unlike corresponding integer methods, these can be implemented without unsafe code. So the need for having them in the standard library feels less pressing. At the same time, it might not be obvious to everyone that to_bits and from_bits can be combined with integer methods in this way. So having these float methods helps discoverability.

@SimonSapin SimonSapin added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. labels May 1, 2019
@tesuji
Copy link
Contributor

tesuji commented Jul 5, 2019

Those methods above are not const anymore. We have to make f*::from_bits const
to mark above methods as const.
Also see #58756 (comment).

@That3Percent
Copy link

There is a typo in the documentation. f64::from_le_bytes reads "Create a floating point value from its representation as a byte array in big endian." but it should say "... little endian". There may be other such typos, I only checked this function.

@tesuji
Copy link
Contributor

tesuji commented Aug 8, 2019

You're right. Could you send a pull request to fix that typo?

@That3Percent
Copy link

Looks like you already did. Linking to it here just in case anyone is reading the thread: #63366 To be honest, the suggestion kind of blew my mind that I, too, could contribute to rust-lang. I'll submit a PR next time. Thanks.

@SimonSapin
Copy link
Contributor Author

Any remaining issue?

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Oct 18, 2019

Team member @SimonSapin has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Oct 18, 2019
@rfcbot
Copy link

rfcbot commented Oct 21, 2019

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Oct 31, 2019
@rfcbot
Copy link

rfcbot commented Oct 31, 2019

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants