-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nicer syntax for include_cpp
#42
Comments
I would recommend against an attribute macro.
For the autocxx use case I would probably expect something resembling: autocxx::bridge! {
#include "bob.h"
...
} |
OK, I'll aim for that. Thanks. I want to come up with something that looks nice after |
@dtolnay...
Dare I say, that feels like a bug? Or at least a questionable design decision especially given the Rust preference for failing fast? I'm still quite attached to the idea of an attribute macro, not really for the rustfmt reason, but because it enables me to attach documentation to directives within the macro not just the outermost macro. I've tried fiddling around with function-style macros but I can't find a way to achieve that desirable property. To understand the nuances of (say) |
Do you mean (looking at the snippet at the top of the thread) documentation of |
Yeah -- sadly the implementation of procedural macros is pretty awful and this isn't among the biggest things wrong with it. :( |
Yes.
I missed an important detail - I'm thinking of contexts like IDEs (VSCode, specifically) where the help text pops up when you hover over something, and you can do things like "go to definition" etc. (I'm assuming the emission of help is driven by As it happens, help text does not seem to pop up very reliably for function-like macros anyway at the moment, but I think that's probably rust-analyzer churn, it's worked in the past. It sometimes works :) I'm not sure this is an important enough reason to suffer the enumerated perils of attribute macros, but I do like the idea of people being able to get direct and immediate help on the different instructions they can give to |
Ah I see, good call. I found it's possible to make it work as follows, but this gets pretty deep into workaround territory so isn't necessarily advisable. But it supports both hover documentation and click-through on both use testing::outer;
outer! {
#include "test.h"
inner!(...)
inner!(...)
} // lib.rs
/// (DOCUMENTATION OF OUTER...)
#[macro_export]
macro_rules! outer {
(
$(#$include:ident $lit:literal)*
$($mac:ident!($($arg:tt)*))*
) => {
$($crate::$include!{__docs})*
$($crate::$mac!{__docs})*
$crate::do_the_work! {
$(#include $lit)*
$($mac!($($arg)*))*
}
};
}
/// (DOCUMENTATION OF INCLUDE...)
#[macro_export]
macro_rules! include {
($($tt:tt)*) => { $crate::usage!{$($tt)*} };
}
/// (DOCUMENTATION OF INNER...)
#[macro_export]
macro_rules! inner {
($($tt:tt)*) => { $crate::usage!{$($tt)*} };
}
#[doc(hidden)]
#[macro_export]
macro_rules! usage {
(__docs) => {};
($($tt:tt)*) => {
compile_error! {r#"usage: outer! {
#include "path/to/header.h"
inner!(...)
}
"#}
};
}
#[doc(hidden)]
#[macro_export]
macro_rules! do_the_work { // FIXME: this is a procedural macro
($($tt:tt)*) => {};
} |
Thanks. That looks like witchcraft. I like it. |
The current syntax:
has four problems:
ffi
mod to be specified.ffi
mod ispub
, etc.Allow
, etc.I am thinking of switching to a syntax a bit more like
cxx
:Hopefully I can persuade rustdoc to treat all the inner macros as document-able things. If not, they may become functions (which do nothing when called as actual functions, but can have docs attached). The problem there is that a single crate can either export a macro or functions. We'll see.
I should do this at the same time as #37 to have one major breaking change, though I don't think anyone's relying on the existing syntax yet.
The new syntax isn't perfect, since I wanted the
include_cpp!
line to act as much as possible like#include
in C++. So thinking still in progress.The text was updated successfully, but these errors were encountered: