News for hackers. Stuff that matters.
Re:Go Protobuf: The New Opaque API by delusional on Tuesday December 17, 2024 @08:38AM (#42441234)
> There is a way to have your cake and eat it too: rust.

Suppose my cake is that I have a struct A which holds a value, that doesn't have a default value, from your library B. Suppose that at the time I want to allocate A I don't yet have the information I need to initialize B, but I also know that I won't need B before I do have that information and can initialize it. In simple terms. I want to allocate A, which requires allocating B, but I don't want to initialize B, yet.

What do I do?

If you answer involves Option<B> then you're asking me to to grow my struct for no gain. That is clearly not _better_.

Re:Go Protobuf: The New Opaque API by tsimionescu on Tuesday December 17, 2024 @09:48AM (#42441755)
Doesn't Rust have explicit support for uninitialized memory, using the borrow checker to make sure you don't access it before initializing it? Or does that just work for local variables, not members of structs?
Re:Go Protobuf: The New Opaque API by steveklabnik on Tuesday December 17, 2024 @11:32AM (#42442732)
You can’t do the “declare before initializing” thing with structs, that’s correct.
Re:Go Protobuf: The New Opaque API by the_gipsy on Tuesday December 17, 2024 @09:32AM (#42441618)
Then you can't eat it too (or else you'll get very sick with NPEs/panics), sorry.
Re:Go Protobuf: The New Opaque API by whytevuhuni on Tuesday December 17, 2024 @12:07PM (#42443071)
More specifically, it could result in undefined behavior, if a panic happens between the allocation and initialization (i.e., it was allocated, not initialized, panicked, and something observed the incomplete struct after the panic). Alternatively, the allocation would always have to leak on panic, or the struct would have to be deallocated without a destructor running.