@@ -89,16 +89,16 @@ supporting both versions of rust more complicated.
89
89
For example, let's make a simplified (and slightly contrived) version of the ` log ` crate in 2015
90
90
edition style:
91
91
92
- ``` rust,ignore
92
+ ``` rust
93
93
/// How important/severe the log message is.
94
94
#[derive(Copy , Clone )]
95
- pub struct LogLevel {
95
+ pub enum LogLevel {
96
96
Warn ,
97
97
Error
98
98
}
99
99
100
100
impl fmt :: Display for LogLevel {
101
- pub fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101
+ fn fmt (& self , f : & mut fmt :: Formatter ) -> fmt :: Result {
102
102
match self {
103
103
LogLevel :: Warn => write! (f , " warning" ),
104
104
LogLevel :: Error => write! (f , " error" ),
@@ -159,7 +159,7 @@ which would make our code compile, but `__impl_log` is meant to be an implementa
159
159
The cleanest way to handle this situation is to use the ` $crate:: ` prefix for macros, the same as
160
160
you would for any other path. Versions of the compiler >= 1.30 will handle this in both editions:
161
161
162
- ``` rust,ignore
162
+ ``` rust
163
163
macro_rules! warn {
164
164
($ ($ args : tt )* ) => {
165
165
$ crate :: __impl_log! ($ crate :: LogLevel :: Warn , format_args! ($ ($ args )* ))
@@ -193,7 +193,7 @@ solution is to add a level of indirection: we crate a macro that wraps `format_a
193
193
to our crate. That way everything works in both editions (sadly we have to pollute the global
194
194
namespace a bit, but that's ok).
195
195
196
- ``` rust,ignore
196
+ ``` rust
197
197
// I've used the pattern `_<my crate name>__<macro name>` to name this macro, hopefully avoiding
198
198
// name clashes.
199
199
#[doc(hidden)]
@@ -211,15 +211,17 @@ whatever tokens we get to the inner macro, and rely on it to report errors.
211
211
So the full 2015/2018 working example would be:
212
212
213
213
``` rust
214
+ use std :: fmt;
215
+
214
216
/// How important/severe the log message is.
215
- #[derive(Copy , Clone )]
216
- pub struct LogLevel {
217
+ #[derive(Debug , Copy , Clone )]
218
+ pub enum LogLevel {
217
219
Warn ,
218
220
Error
219
221
}
220
222
221
223
impl fmt :: Display for LogLevel {
222
- pub fn fmt (& self , f : & mut fmt :: Formatter ) -> fmt :: Result {
224
+ fn fmt (& self , f : & mut fmt :: Formatter ) -> fmt :: Result {
223
225
match self {
224
226
LogLevel :: Warn => write! (f , " warning" ),
225
227
LogLevel :: Error => write! (f , " error" ),
0 commit comments