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


#1

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.


#2

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.


#3

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


#4

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


#5

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

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.


#6

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).


#7

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