#associated #constant #const

guilt-by-association

Macro for declaring/implementing traits with fake associated consts (in stable Rust)

8 releases (4 breaking)

Uses old Rust 2015

0.5.0 Mar 13, 2018
0.4.1 Jul 25, 2017
0.4.0 Feb 13, 2017
0.3.1 Jan 20, 2017
0.1.0 Jul 2, 2015

#2634 in Rust patterns

24 downloads per month

MIT license

15KB
165 lines

This package defines a macro that approximates the syntax for associated consts that is used in nightly Rust builds, but it turns the consts into functions so no nightly features are required. Therefore, we can have the appearance (but not the advantages) of associated consts in stable Rust.

Requires rust 1.3 or higher. This package will be irrelevant whenever associated consts get stabilized.

For documentation, run cargo doc.


lib.rs:

Macro for declaring/implementing traits with fake associated consts (in stable Rust)

Currently very fragile in terms of syntax: does not support traits/impls with any kind of generic parameters (either lifetimes or types).

The same macro is used for declaring a trait with associated consts, implementing such a trait, and accessing the consts.

The syntax is the same as that proposed for associated consts, except that:

  • all consts must be at the beginning of the trait/impl, before any functions

See the tests for example usage.

At the moment they are not consts at all -- they simply expand to static functions with the same name as the declared const. You may therefore access the const by calling Trait::CONST(), or (for future proofing, in case the macro implementation changes), call the macro again to access the const, as guilty!(Trait::CONST).

No runtime deps