Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a macro from one crate in another?

I'm struggling to make macros from my rust lib available to other rust projects.

Here's an example of how I'm trying to get this work at the moment.

lib.rs:

#![crate_name = "dsp"]
#![feature(macro_rules, phase)]
#![phase(syntax)]

pub mod macros;

macros.rs:

#![macro_escape]

#[macro_export]
macro_rules! macro(...)

other_project.rs:

#![feature(phase, macro_rules)]
#![phase(syntax, plugin, link)] extern crate dsp;

macro!(...) // error: macro undefined: 'macro!'

Am I on the right track? I've been trying to use std::macros as a reference, but I don't seem to be having much luck. Is there anything obvious that I'm missing?

like image 471
mindTree Avatar asked Jul 15 '14 03:07

mindTree


1 Answers

Your attributes are tangled.

#![…] refers to the outer scope, while #[…] refers to the next item.

Here are some things of note:

  1. In lib.rs, the #![feature(phase)] is unnecessary, and the #![phase(syntax)] is meaningless.

  2. In other_project.rs, your phase attribute is applied to the crate, not to the extern crate dsp; item—this is why it does not load any macros from it. Remove the !.

like image 184
Chris Morgan Avatar answered Nov 12 '22 07:11

Chris Morgan