Is there a way to fail to compile code because it is not wrapped in a macro? For example:
struct MyStruct(i32);
fn main() {
allow! {
// this would be fine because it is inside my special `allow` macro
let my_struct = MyStruct(100);
}
// but this would fail at compile time because it is not
let my_struct = MyStruct(100);
}
I don't think that there's any way to do this currently, and there's no plausible reason why you'd do this; could you please expand why you may want to do this?
PS you could always make the structure name a macro...
PS you could always make the structure name a macro…
I would be doing multiple operations that all require the same constraint inside the macro, but just simplified it for the example.
there’s no plausible reason why you’d do this
I'm not so sure. Consider going across a ffi boundry that requires to you initialize something before and clean it up after. Right now, it would look something like this:
struct Guard;
impl Guard { fn new() -> Self { Guard } } // do some "before stuff" here
struct MyStruct<'a>(&'a Guard, i32);
fn main() {
let guard = Guard::new();
// the reference to guard that MyStruct owns ensures the "before stuff" was done
let my_struct = MyStruct(&guard, 100);
// impl `std::ops::drop` for `Guard` for some "after stuff"
}
But as you can see, if I could make some macro like I mentioned, it would clean this up a lot and be pretty cool :D.
Yeah that is a cool idea, but it would still require me to mark my public interface to be unsafe because it wouldn't stop users to create a MyStruct(100); outside of the fn ffi_boundary(). I like the idea though.
I made it hold a reference to the guard so trickery can't be used to pass it outside the closure (possible in safe-code with catch_unwind otherwise) and make takes the Factory by value so it can only be called once.