News for hackers. Stuff that matters.
Re:Rust and C++ Interoperability in Chrome by steveklabnik on Wednesday August 19, 2020 @01:57PM (#24213621)
Rust does not have a textual substitution pre-processor, so like, you can do this sorta kinda but it would be way more involved.
Re:Rust and C++ Interoperability in Chrome by roblabla on Wednesday August 19, 2020 @02:19PM (#24213943)

    macro_rules! cpp_call {
        ($($tt:tt)*) => {
            unsafe { $($tt)* }
        }
    }
Yeah, not pretty, but it works :P
Re:Rust and C++ Interoperability in Chrome by rightbyte on Wednesday August 19, 2020 @04:59PM (#24216368)
Nature finds its ways.

Seriously though, maybe comment annotation would be better than abusing the macros like that. I though there was a more direct cpp way. (Edit: As in the c preprocessor, not C++.)

Re:Rust and C++ Interoperability in Chrome by luizfelberti on Wednesday August 19, 2020 @02:22PM (#24214010)
> Rust does not have a textual substitution pre-processor

Is that not pretty much what Rust's procedural macros are? [0]

Except for the fact that they do not allow emitting invalid token streams (which is something you don't want anyways), it can do any arbitrary manipulation of source code, but in a way that is integrated with the compiler and less prone to breaking the program.

[0] https://doc.rust-lang.org/reference/procedural-macros.html

Re:Rust and C++ Interoperability in Chrome by Arnavion on Wednesday August 19, 2020 @02:56PM (#24214578)
A proc macro would still need to be invoked as either

    cpp_call! { fn foo() { ... } }
or

   #[cpp_call] fn foo() { ... }
The former can be written even with a macro_rules macro. Regardless, both macros have to work by parsing the whole input tokentree `fn foo ( ) { ... }` and emitting it back with an `unsafe` prepended. The macro invocation cannot expand to just `unsafe`.
Re:Rust and C++ Interoperability in Chrome by steveklabnik on Wednesday August 19, 2020 @04:35PM (#24216039)
Yes, this is what I was meaning, thank you.
Re:Rust and C++ Interoperability in Chrome by qppo on Wednesday August 19, 2020 @02:47PM (#24214439)
its splitting hairs but not really. Rust's proc macros need to be explicitly invoked using special syntax at the usage site, either by annotating a statement or enclosing a block of tokens. On top of that, they do token replacement (or addition, with derive macros) which means they can only take valid input tokens as an argument (compared to macro_rules which can take arbitrary strings).

Textual substitution is less rigorous. It tells the compiler to replace a string with another, wherever it is seen. There's no restriction that it must be valid token input to be replaced or that it needs to be an explicit usage of the macro.

Re:Rust and C++ Interoperability in Chrome by ComputerGuru on Thursday August 20, 2020 @04:31AM (#24221155)
> Except for the fact that they do not allow emitting invalid token streams (which is something you don't want anyways)

Reminds me of some of stringification abused in the Linux kernel. What looks like a function call passing the literal text param1## as an argument, where it would then trigger a different stringification in a subsequent preprocessor pass that prepended ##foo to the passed argument.

Re:Rust and C++ Interoperability in Chrome by fullstop on Wednesday August 19, 2020 @02:30PM (#24214148)
I keep meaning to dive into Rust, but it feels like they've removed so many useful things in the name of safety.

I mean, I get it, these are all things which can be used to very easily cause chaos and shoot yourself in the foot. I look at code that I've written, though, and see so many challenges to writing the same thing in Rust.

I spend 99% of my time in embedded Linux or bare metal, though, so maybe my view point is a bit skewed.

There seem to be very few GUI libraries which can be used in Rust as well. I'll keep checking https://areweguiyet.com/

Re:Rust and C++ Interoperability in Chrome by ChrisSD on Wednesday August 19, 2020 @02:37PM (#24214273)
If you want to use a tool that pastes code fragments in arbitrary places you can. However, I'm not clear on the benefit over something that's aware of its context, or at the very least AST aware.
Re:Rust and C++ Interoperability in Chrome by qppo on Wednesday August 19, 2020 @03:04PM (#24214712)
Even modern C++ has eschewed macros for virtually every use case, the only reason I've had to use them (legitimately) has been conditional compilation and some compiler/platform specific issues.

Like if you're doing #define PI 3.14159 you're asking to get chirped in code review.

Re:Rust and C++ Interoperability in Chrome by rightbyte on Wednesday August 19, 2020 @04:49PM (#24216223)
Ye, everyone know it is:

    #define PI 3.1415927410125732421875
that is the float closest to pi or that there is the GNU extension USE_FUNKY_DEFINES-something with M_PI for cmath. (I know you meant constexpr:ions).
Re:Rust and C++ Interoperability in Chrome by Const-me on Thursday August 20, 2020 @07:12AM (#24222017)
Long time ago when computers were 32-bit I used to write something like this in C++:

    inline double pi()
    {
        __asm fldpi;
    }
On old enough compilers, this will get you 80-bit version of pi, more precision than even double. Completely useless now, modern compilers no longer emit x87 code.
Re:Rust and C++ Interoperability in Chrome by asveikau on Thursday August 20, 2020 @12:19AM (#24219845)
math.h has M_PI in posix.
Re:Rust and C++ Interoperability in Chrome by qppo on Thursday August 20, 2020 @12:31AM (#24219911)
The point is that using macros for constants is frowned upon in modern codebases, just like all other use cases for macros that break type safety and the one definition rule.
Re:Rust and C++ Interoperability in Chrome by asveikau on Thursday August 20, 2020 @02:04AM (#24220367)
Yes, I understand the point. But to your comment in a literal sense, if I were doing code review I would probably suggest using M_PI. Which happens to be a macro.

Which actually highlights that many things you want to use for practical purposes are C rather than C++, predate innovations such as const or namespaces, etc. It's possible and common to understand the downsides and still reach for the macro.

Re:Rust and C++ Interoperability in Chrome by steveklabnik on Wednesday August 19, 2020 @04:35PM (#24216037)
Just because we don't have textual substitution doesn't mean we don't have macros. We have a lot of macros!

(It took me a while to write back to you because I am busy writing bare-metal embedded Rust, btw. Including using some macros!)