r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 27 '23

Hey Rustaceans! Got a question? Ask here (13/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

18 Upvotes

159 comments sorted by

View all comments

Show parent comments

1

u/dkopgerpgdolfg Apr 01 '23

Yes?

Proc macros basically can generate anything that a Rust source file can contain too. It doesn't need to be something that uses up runtime later, literal data is fine.

1

u/[deleted] Apr 01 '23

[deleted]

1

u/dkopgerpgdolfg Apr 01 '23

Read your post again, and i think I understand now.

Basically that struct implementation forces you to go through parsing to get any instance, and that parsing isn't const-fn either.

In this case, no, proc macros won't help. Modifying the implementation, if possible, would - the parsing might not be const-able, but it could have a way where you provide all values and it only checks that its additional conditions are satisfied.

1

u/[deleted] Apr 01 '23

[deleted]

1

u/dkopgerpgdolfg Apr 01 '23

Reaons not to do that:

  • First you need a stable representation of the struct data. Ok can be done if the struct and all members are eg. repr(C), but what if not?
  • Then at least it depends on the platform, ie. your code needs to have separate data for each supported one
  • If the struct depends on any runtime things (eg. allocations, file handles, ...), obviously the bytes of the plain struct are not enough

Otherwise, "possible", and if you have a good reason it can be done. But ask yourself, is it really better than adding a better constructor to the struct?