{
"$type": "site.standard.document",
"bskyPostRef": {
"cid": "bafyreihipyc4i4wpwmyawwv7lultbjqepnvlbzazjrjuznlkobwn2hh6di",
"uri": "at://did:plc:ivbknywyskln22er3nkssdhl/app.bsky.feed.post/3mmk4t6uhzux2"
},
"path": "/t/another-experiment-to-make-unsafe-rust-safer-preventing-ub-in-maybeuninit-with-compile-time-error/24352#post_13",
"publishedAt": "2026-05-23T17:20:44.000Z",
"site": "https://internals.rust-lang.org",
"tags": [
"ArrayVec"
],
"textContent": "fuji-184:\n\n> My goal is a lossless safer abstraction of maybeuninit. So that it can still be used as building block to create data structure or low level programming\n>\n> But now I'm confused to choose compile time error, zero branching, but 2 different type because it is what causes the compile time error so no branching is needed. Or 1 type but using runtime branching\n\nYou’re approaching this problem too abstractly. Both of the possibilities you have named are usually already possible in safer ways (static checks with ordinary functions returning values of different types, and dynamic checks with `Option`). `MaybeUninit` is used in cases where **neither** of those approaches apply.\n\nYou cannot design a better `MaybeUninit` by _just_ thinking about what additional checks would make it safer to use. What you need to do is:\n\n 1. Find **specific existing code that uses** `MaybeUninit`.\n 2. Think about how that code uses `MaybeUninit` and what it actually needs.\n 3. Design something that is safer and **make sure that code can be rewritten to use it**.\n 4. Apply this to other code too, in order to show that it is useful in more than one situation.\n\n\n\n**Validate your design by showing that it can improve existing real-world unsafe code.**\n\nfuji-184:\n\n> 1 type with optional written len for array. Or dedicated type for array with name UninitArray (MaybeUninit<[ array ]>). That will have method to get slice from it as long as it is < the written len, no need to wait it to be fully written\n\nThis is what ArrayVec does. You don’t need to write this type because it already exists.",
"title": "Another Experiment To Make Unsafe Rust Safer: Preventing UB In MaybeUninit With Compile Time Error"
}