Better HN
top
new
best
ask
show
jobs
Log in
Sign up
Better HN
Comment
by
kobebrookskC3
· 9mo ago · 0 comments
how would you implement
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
Comments
[–]
steveklabnik
9mo ago
Regular variable definition shadows. Macros expand to regular Rust code, they could always be replaced by the expanded body.
[–]
kobebrookskC3
OP
9mo ago
yes, but the code inside is unsafe. the pin macro is like a safe function.
[–]
steveklabnik
9mo ago
I'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
OP
9mo ago
but without macros, how would you expose a safe interface?
fn pin(x: T) -> Pin<&mut T> { ... }
would move the value
[–]
jibal
9mo ago
Since macros just expand into code, how could you imagine that a macro is ever necessary?
[–]
kobebrookskC3
OP
9mo ago
the 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
9mo ago
Excellent goalpost moving! Congratulations!
[–]
kobebrookskC3
OP
9mo ago
no, 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.