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

Comments

@SimonSapin
Copy link
Contributor

@SimonSapin 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.

@lzutao

This comment has been minimized.

Copy link
Contributor

@lzutao lzutao 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

This comment has been minimized.

Copy link

@That3Percent That3Percent commented Aug 7, 2019

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.

@lzutao

This comment has been minimized.

Copy link
Contributor

@lzutao lzutao commented Aug 8, 2019

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

@That3Percent

This comment has been minimized.

Copy link

@That3Percent That3Percent commented Aug 8, 2019

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

This comment has been minimized.

Copy link
Contributor Author

@SimonSapin SimonSapin commented Oct 18, 2019

Any remaining issue?

@rfcbot fcp merge

@rfcbot

This comment has been minimized.

Copy link

@rfcbot 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

This comment has been minimized.

Copy link

@rfcbot rfcbot commented Oct 21, 2019

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

@rfcbot

This comment has been minimized.

Copy link

@rfcbot 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
Projects
None yet
4 participants
You can’t perform that action at this time.