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

hex-literal: remove support for comments inside literals and migrate to CTFE #816

Merged
merged 9 commits into from
Apr 2, 2023

Conversation

newpavlov
Copy link
Member

Potential breaking change for v1.0, see #814.

@newpavlov newpavlov changed the title hex-literal: remove support for comments inside literals hex-literal: remove support for comments inside literals and migrate to CTFE Apr 2, 2023
@newpavlov
Copy link
Member Author

newpavlov commented Apr 2, 2023

As suggested by @Nugine, I've migrated this crate from proc macro to CTFE. Initially I thought it would require Nightly, but looking closer at the const_str crate, it can work on stable as well. It results in a lighter crate, which likely should compile significantly faster.

@newpavlov newpavlov marked this pull request as ready for review April 2, 2023 01:40
@newpavlov newpavlov requested review from tarcieri and removed request for tarcieri April 2, 2023 01:41
@newpavlov
Copy link
Member Author

@tarcieri
I think we can release these changes as v0.4. WDYT?

@tarcieri
Copy link
Member

tarcieri commented Apr 2, 2023

@newpavlov sure!

@newpavlov newpavlov merged commit f3dfe51 into master Apr 2, 2023
@newpavlov newpavlov deleted the hex-literal/rm_comments branch April 2, 2023 02:21
@triamazikamno
Copy link

Hi, this change seem to have broken the usage of hex! in the match patterns, i.e.

match buf {
hex!("0a0b") => {}

throws now:

error: expected pattern, found `{`
   |
60 |         hex!("0a0b") => {
   |         ^^^^^^^^^^^^^^^^
   |         |
   |         expected pattern
   |         in this macro invocation
   |         this macro call doesn't expand to a pattern

@newpavlov
Copy link
Member Author

Unfortunately, I think it's inevitable with the CTFE approach until implementation of RFC 2920 will be stabilized. You can either continue to use v0.3, or introduce named constants and use them in patterns.

@triamazikamno
Copy link

I see, thank you. Will stick to the v0.3 for now

@tarcieri
Copy link
Member

tarcieri commented Apr 3, 2023

Another small regression (which is easy enough to work around, but was convenient before):

https://github.com/RustCrypto/SSH/actions/runs/4599840083/jobs/8125721544?pr=92#step:5:13

error[E0716]: temporary value dropped while borrowed
 --> ssh-encoding/tests/decode.rs:8:21
  |
8 |     let mut bytes = hex!("42").as_slice();
  |                     ^^^^^^^^^^           - temporary value is freed at the end of this statement
  |                     |
  |                     creates a temporary value which is freed while still in use
9 |     let ret = u8::decode(&mut bytes).unwrap();
  |                          ---------- borrow later used here

@newpavlov
Copy link
Member Author

Yes, it's unfortunate. Though in such cases I usually use shorter let mut bytes = &hex!("..."); with hinted type, if necessary.

macro_rules! hex {
($($s:literal)*) => {{
const STRINGS: &[&'static [u8]] = &[$($s.as_bytes(),)*];
$crate::decode::<{ $crate::len(STRINGS) }>(STRINGS)
Copy link
Contributor

@Nugine Nugine Apr 4, 2023

Choose a reason for hiding this comment

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

This line may not be const-promoted (or const-evaluated), which may cause performance regression.

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.

4 participants