teamhackback / hb-ddb

Vibe.d Async native D Postgres client
https://teamhackback.github.io/hb-ddb/docs/ddb/pg.html
1 stars 1 forks source link

Make properly @safe #12

Open wilzbach opened 7 years ago

wilzbach commented 7 years ago

Fix the holes in @trusted.

wilzbach commented 7 years ago

Biggest problem: Variant isn't @safe


struct SafeVariantN(size_t maxDataSize, AllowedTypesParam...)
{
    VariantN!(maxDataSize, AllowedTypesParam) _variant;
    alias _variant this;

    this(T)(T value) @trusted
    {
        static assert(_variant.allowed!(T), "Cannot store a " ~ T.stringof
            ~ " in a " ~ VariantN.stringof);
        _variant.opAssign(value);
    }

    ~this() @trusted {
        _variant.destroy();
    }

    this(this) @trusted {}

    SafeVariantN opAssign(T)(T rhs) @trusted
    {
        static if (!is(T == typeof(null)))
            _variant = _variant.opAssign(rhs._variant);
        return this;
    }
}

alias SafeVariant = SafeVariantN!(maxSize!(creal, char[], void delegate()));

...
    else static if (hasMember!(T, "_variant"))
    {
        static if (typeof(T.init._variant).stringof[0..8] == "VariantN")
            enum isVariantN = true;
        else
            enum isVariantN = false;
    }

...

import ddb.db : Variant = SafeVariant;