Slot — type of a `T`-shaped space in memory

Slot is a library of a type of memory spaces shaped as a type parametre. It can be used, for example, in a buffer for a stack- or static-allocated Vec-like type (example). It can also serve as an alternative to core::mem::uninitialized, which is deprecandum.

It is similar to core::mem::ManuallyDrop, but getting the value is unsafe, so creating an uninitialized Slot can be safe.

I suspect that this is unsound:

impl<T> Slot<T> {
    #[inline]
    pub const fn new() -> Self {
        #[allow(unions_with_drop_fields)]
        union U<T> { u: (), v: T }
        Self { x: unsafe { U { u: () }.v } }
    }
}

If I call Slot::<Void>::new() (with enum Void{}), then you're creating an instance of an uninhabited type (in a safe method!), so this seems to have the same problems that have put mem::uninitialized on the path to deprecation.

This is why MaybeUninit in the RFC has two variants, like your U enum, not just the one that Slot has.

@scottmcm Good catch — i published a new version which makes no values of arbitrary (potentially-void) type in new.

Is there a reason you didn't just copy MaybeUninit? I'm still confused by things like why Slot::default has a different implementation from Slot::new...

I not knew about MaybeUninit when i wrote Slot :smiling_face:

Legacy reason: before const fn, new merely called default.
Now in git master, default merely calls new; i'm not sure it's worth a release.

Slot is probably a better name :+1:. But yeah, there's supposed to be a type in std for this (though for some reason there still isn't).

1 Like

Fair enough! :slightly_smiling_face: I thought you did since it was mentioned in the OP.