I'd like to wrap a set of plain types acquired from the shared memory, via a trait bound Plain
, but I find it hard to do this.
To clarify, say a type T
is "plain" if:
- it contains no pointers nor references;
- it has no destructor, i.e.
T: !Drop
; - it contains no
UnsafeCell
s; -
&T
can be safely transmuted from a&[u8]
, where the&[u8]
has the length ofT
's size and is properly aligned toT
, andT
's internal padding should be guaranteed to be zero; -
&mut T
can be safely transmuted from a zero-filled&mut [u8]
, where the length and alignment of&mut [u8]
takes after 5.
Generally, such a plain type T
will be processed as following:
- create a page of shared memory, and allocate as layout of
T
in the page; - fill the allocated bytes zero, and transmute such
&mut [u8]
into&mut T
; - write arbitrary valid value
T
into the mutable slice&mut T
and freeze it (i.e. no longer mutable); - another process open this shared page, and find the corresponding location of
&[u8]
(By a inter-process communication, the new process doesn't know the actual type inside, but knows only the offset from the page and the length of bytes); - it transmutes the
&[u8]
into&T
and read its value.
EDIT: it is considered as a specialization of a general type. For T
that is not "plain", we serialize T
to and deserialize T
from a [u8]
instead of transmuting, which means that the shared memory layer must be type-erased.