idanarye / rust-smart-default

Rust macro for automatically generating default
MIT License
133 stars 7 forks source link

[FR] Support const expressions #16

Closed behnam-oneschema closed 2 weeks ago

behnam-oneschema commented 2 weeks ago

Would be great to be able to use const expressions, even as simple as numeric operators, such as:

#[default = 20 * 1024]
num_bytes: u32
idanarye commented 2 weeks ago

I've afraid this syntax is rejected by either syn or the Rust language itself (I'm not sure where the attribute value must be a literal error comes from). Even if it's just syn and can by bypassed with manual parsing, it'd still create problems for other derive macros on the same type (which won't even be able to get the attribute name) so I'd rather not do it.

SmartDefault does have a workaround for this though:

#[default(20 * 1024)]
num_bytes: u32
behnam-oneschema commented 2 weeks ago

Interesting! The workaround is perfect, then. Thanks!