www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - New library: rebindable, create a type that can stand in for any other

reply FeepingCreature <feepingcreature gmail.com> writes:
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
Sep 29 2021
parent reply FeepingCreature <feepingcreature gmail.com> writes:
On Wednesday, 29 September 2021 at 10:22:40 UTC, FeepingCreature 
wrote:
 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.
I forgot to mention: It does this by recursively crawling the member types of `T`, replacing all primitives with non-const equivalents. Is this terrible? Yes, it's very terrible. I also don't see how to avoid it.
Sep 29 2021
parent bauss <jj_1337 live.dk> writes:
On Wednesday, 29 September 2021 at 10:51:26 UTC, FeepingCreature 
wrote:
 On Wednesday, 29 September 2021 at 10:22:40 UTC, 
 FeepingCreature wrote:
 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.
I forgot to mention: It does this by recursively crawling the member types of `T`, replacing all primitives with non-const equivalents. Is this terrible? Yes, it's very terrible. I also don't see how to avoid it.
Terrible but I love it
Oct 01 2021