d的扩大@nogc

原文

import std.traits;
 // 从函数或闭包类型中转换`@nogc`
auto assumeNoGC(T) (T t) if (isFunctionPointer!T || isDelegate!T)
{
    enum attrs = functionAttributes!T | FunctionAttribute.nogc;
    return cast(SetFunctionAttributes!(T, functionLinkage!T, attrs)) t;
}

 //不能按`@nogc`标记函数,但是你知道不会使用垃集
void funcThatMightUseGC(int timeout)
{
    if (unlikelyCondition(timeout))
        throw new Exception("已抛");

    doMoreStuff();
}

void funcThatCantAffortGC() @nogc
{
    // 用转换闭包调用垃集.
    assumeNoGC( (int timeout)
        {
            funcThatMightUseGC(timeout);
        })(10000);
}
posted @ 2022-06-11 22:44  zjh6  阅读(15)  评论(0)    收藏  举报  来源