5 ms·
how would you implement https://doc.rust-lang.org/stable/std/pin/macro.pin.html https://doc.rust-lang.org/stable/std/pin/macro.pin.html without macros? a macro
by kobebrookskC3 7mo ago
how would you implement https://doc.rust-lang.org/stable/std/pin/macro.pin.html https://doc.rust-lang.org/stable/std/pin/macro.pin.html without macros? a macro is used to shadow the original variable so that you can't move it (safely) after you pin it
- steveklabnik 7mo agoRegular variable definition shadows. Macros expand to regular Rust code, they could always be replaced by the expanded body.
- kobebrookskC3 7mo agoyes, but the code inside is unsafe. the pin macro is like a safe function.
- steveklabnik 7mo agoI'm not sure what that has to do with anything. The macro isn't what makes it safe. The unsafe code being properly written is.
- kobebrookskC3 7mo agobut without macros, how would you expose a safe interface? fn pin(x: T) -> Pin<&mut T> { ... } would move the value
- Philpax 7mo agoYour macroless variant of Rust would offer a safe builtin that does this. It doesn't need to be implemented with a macro.
- jibal 7mo agoSince macros just expand into code, how could you imagine that a macro is ever necessary?
- kobebrookskC3 7mo agothe macro uses unsafe inside, so that's another instance of unsafe you'll need to check, whereas the pin macro is like a safe function
- jibal 7mo agoExcellent goalpost moving! Congratulations!
- kobebrookskC3 7mo agono, you just missed my point. expanding the implementation is not a safe abstraction. show me how you'd implement the functionality of the pin macro as a safe abstraction.
- jitl 7mo agoeverything in zig is unsafe and needs to be checked like rust unsafe, so…
- jibal 7mo agoI didn't miss that you totally changed the subject and now you're attacking a strawman. See Steve Klabnick's response to your other comment where you did this. Of course macros are good for encapsulation and abstraction, but that's a different subject--and note that the discussion was about Zig vs. Rust, and Zig has no macros so there's unencapsulated unsafe code all over the place. I won't respond further.
- kobebrookskC3 7mo agoi was responding this claim > It would be perfectly possible to design a variant of Rust that gets you to 80-90% of Rust's usability, with the same safety, without macros. i then present an api that i think relies on macros to expose a safe api > Of course macros are good for encapsulation and abstraction, but that's a different subject. no it's not. exposing safe abstractions is pretty much rust's raison d'être