Messages in this thread |  | | Date | Mon, 08 Sep 2025 00:51:02 +0200 | | Subject | Re: [PATCH] rust: pin-init: add references to previously initialized fields | | From | "Benno Lossin" <> |
| |
On Sun Sep 7, 2025 at 11:39 PM CEST, Danilo Krummrich wrote: > On Sun Sep 7, 2025 at 11:06 PM CEST, Benno Lossin wrote: >> On Sun Sep 7, 2025 at 7:29 PM CEST, Danilo Krummrich wrote: >> I have some ideas of changing the syntax to be more closure-esque: >> >> init!(|this| -> Result<MyStruct, Error> { >> let x = 42; >> MyStruct { >> x, >> } >> }) >> >> There we could add another parameter, that would then serve this >> purpose. We should also probably rename `this` to `slot` & then use >> `this` for the initialized version. > > I think that's a pretty good idea, but the part that I think is a little bit > confusing remains: `this` will need to have different fields depending on where > it's accessed.
Yeah (that's also the main issue with the macro implementation).
>> But as I said before, implementing the `this` thing from a macro >> perspective is rather difficult (I have two ideas on how to do it and >> both are bad...). >> >>> But as you say, that sounds tricky to implement and is probably not very >>> intuitive either. I'd rather say keep it as it is, if we don't want something >>> like the `let b <- b` syntax I proposed for formatting reasons. >> >> I don't feel like that's conveying the correct thing, it looks as if you >> are only declaring a local variable. > > Yeah, it's not great, but given that it's a custom syntax it also does not > create wrong expectations I'd say.
I'd say it looks like combining the `<-` operation already used by the `init!` macro & a `let` binding. Thus introducing a local variable that's (pin) initialized in-place. Not a field of the current struct.
> Anyways, I'm fine with either. For now we probably want to land the version as > it is and revisit once you settle on the syntax rework you mentioned above.
I actually came up with a third option that looks best IMO:
init!(MyStruct { x: 42, #[with_binding] y: 24, z: *y, })
The `#[with_binding]` attribute makes the macro generate a variable `y`. `x` & `z` don't give access to their value. (we of course should come up with a better name).
Any thoughts?
--- Cheers, Benno
|  |