Closed kupiakos closed 10 months ago
This would all be so much easier if MaybeUninit
supported T: ?Sized!
Yeah 😔
you basically would like to be able to write some T : ?Sized
genericity that would allow you to cover both T : Sized
and [u8]
equally, right? That seems a smaller API addition than the one I've just shown in that drop-down section, and indeed should be possible. The key idea would be to have a IsSizedOrByteSlice
trait, and then implement it for <T>
and for [u8]
, right?
I was more thinking along the lines of unifying methods between Out<T: Sized>
and Out<[T]>
through a trait. I have a draft in https://github.com/danielhenrymantilla/rust-uninit/pull/10. What do you think?
I want to write a "read from flash" function that takes an
Out<'_, T>
whereT: FromBytes + ?Sized
.FromBytes
lets us describe that a type is constructible from any bit pattern. They all defer down to a common "read into raw slice" function.In other words:
There are two issues with this approach. The first is that I can't write that
Out<'_, T>
toOut<'_, [u8]>
conversion function generically since pretty much all of the useful functions forOut
are gated onT: ?Sized
orT = [U]
. I can't get the pointer and I can't get the size of the pointee. The second is that I can'tassume_init
after I know the data has been read.I'd like if I all of these functions worked for
T: ?Sized
:as_mut_ptr
(which could be genericallyas
cast to a*const u8
even ifT: ?Sized
). Unfortunately I can't get the slice length on stable with this untilmem::size_of_val_raw
is stabilized since I can't deref that pointer oras
cast it to a different slice type.mem::size_of_val
.as_mut_uninit
(not positive this is possible and might require specialization).assume_init
. I recognizeassume_all_init
is there as well to make it clear every element is enabled, but I'd also really like to be able toassume_init
in this context somehow.My current workaround, which is fine enough, is to have a
read
which always takes anOut<[u8]>
and aStorage::read_a
which is takes anOut<T>
withT: Sized
. We haven't had the need to need to read slices of[T]
yet.This would all be so much easier if
MaybeUninit<T>
supportedT: ?Sized
!