assumeTrusted

Calling any system functions.

template assumeTrusted(alias F)
@trusted
assumeTrusted
(
Args...
)
(
Args args
)

Examples

auto bar(int b) @system {
    return b + 1;
}

const b = assumeTrusted!bar(5);
assert(b == 6);

// applicable to 0-ary function
static auto foo() @system {
    return 3;
}

const a = assumeTrusted!foo;
assert(a == 3);

// // It can be used for alias
alias trustedBar = assumeTrusted!bar;
alias trustedFoo = assumeTrusted!foo;
//    assert(is(typeof(trustedFoo) == function));

import core.stdc.stdlib;

auto ptr = assumeTrusted!malloc(100);
assert(ptr !is null);
ptr.assumeTrusted!free;

ptr = assumeTrusted!calloc(10, 100);
ptr.assumeTrusted!free;

alias lambda = assumeTrusted!((int a) @system => a * 3);

assert(lambda(42) == 3 * 42);

{
    import std.concurrency;

    static void task() @safe {
        const result = 2 * assumeTrusted!(receiveOnly!int);
        assumeTrusted!({ ownerTid.send(result); });
        alias trusted_owner = assumeTrusted!(ownerTid);
        alias trusted_send = assumeTrusted!(send!(string));
        trusted_send(trusted_owner, "Hello");
    }

    auto tid = assumeTrusted!({ return spawn(&task); });
    assumeTrusted!({ send(tid, 21); });
    assert(assumeTrusted!(receiveOnly!(const(int))) == 21 * 2);
    assert(assumeTrusted!(receiveOnly!(string)) == "Hello");
}