Skip to content

Macro Renames

Peter Glotfelty edited this page Mar 17, 2019 · 3 revisions

Not Deployed yet

In versions of Rust prior to 1.31.0, Rust didn't support importing macros by path. They could only be brought into scope by #[macro_use]. This can create collisions if two packages export the same macros. See issue #30 for an example. To workaround this, Strum supports renaming specific macros using feature flags. The table below shows for each macro, the corresponding feature flag, and the name of the macro after the feature is enabled.

Original Macro Name Feature Flag New Macro Name
EnumString verbose-enumstring-name StrumEnumString
AsRefStr verbose-asrefstr-name StrumAsRefStr
AsStaticStr verbose-asstaticstr-name StrumAsStaticStr
IntoStaticStr verbose-intostaticstr-name StrumIntoStaticStr
ToString verbose-tostring-name StrumToString
Display verbose-display-name StrumDisplay
EnumIter verbose-enumiter-name StrumEnumIter
EnumMessage verbose-enummessage-name StrumEnumMessage
EnumProperty verbose-enumproperty-name StrumEnumProperty
EnumDiscriminants verbose-enumdiscriminants-name StrumEnumDiscriminants
EnumCount verbose-enumcount-name StrumEnumCount

Example Cargo.toml:

[dependencies]
strum = { version = "0.14" }
strum_macros = { version = "0.14", features = [ "verbose-tostring-name" ] }

Now instead of writing #[derive(ToString)], a library would instead write #[derive(StrumToString)]. This way Strum can still be used in older versions of Rust without worrying about name collisions.

You do not need this in versions of rust >= 1.31. You can and should prefer importing macros by path use strum::ToString

Clone this wiki locally