Or: Turducken 2.0 The Reckoning
https://code.dlang.org/packages/rebindable https://github.com/FeepingCreature/rebindable
Rebindable offers a proxy type, rebindable.DeepUnqual
(DeepUnqual!T
) that can "stand in" for T
in layout, but does not share T
's constructor, destructor, copy constructor, invariants or constness.
It's effectively "std.typecons.Rebindable
for structs (and everything else)".
This proxy type is useful when implementing data structures where the lifetime of a contained value is different from the lifetime of the data structure itself.
This project sprang from my thread over in general, https://forum.dlang.org/thread/kkefkykirldffkdoqdwj@forum.dlang.org "Will D always have a way to rebind an arbitrary data type?"
To my knowledge, rebindable exploits no compiler bugs and invokes no undefined behavior. All casts are such that pointer fields are matched with pointer fields (or void[]) at the same offset. It is also totally independent of the vagaries of Phobos functions like moveEmplace
, which Turducken used.
Of course, you must still make sure that mutable fields in DeepUnqual!T
are never exposed as immutable. You can do this by only returning T
by value.
See also: Turducken Type Technique https://forum.dlang.org/thread/ekbxqxhnttihkoszzvxl@forum.dlang.org