Keeping a mutable reference to a struct with immutable members
pineapple via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun May 29 12:09:13 PDT 2016
On Sunday, 29 May 2016 at 18:52:36 UTC, pineapple wrote:
> What's the best way to handle something like this?
Well I did get something to work but it's ugly and I refuse to
believe there isn't a better way to handle this.
Where `Range` is an alias to a struct with an immutable member,
and `this.source` is the attribute that I need to be able to
re-assign to a locally scoped return value:
import core.stdc.stdlib : malloc, free;
if(this.source) free(source);
ubyte* newptr = cast(ubyte*) malloc(Range.sizeof);
assert(newptr !is null, "Failed to allocate memory.");
Range saved = this.original.save;
ubyte* savedptr = cast(ubyte*) &saved;
for(size_t i; i < Range.sizeof; i++){
newptr[i] = savedptr[i];
}
this.source = cast(Range*) newptr;
More information about the Digitalmars-d-learn
mailing list