RcPtr.exchange

Stores the non shared RcPtr pointer ptr in the shared(RcPtr) pointed to by this and returns the value formerly pointed-to by this, atomically or with mutex.

  1. RcPtr exchange(typeof(null) )
    struct RcPtr
    scope
    exchange
    (
    MemoryOrder order = MemoryOrder.seq
    this This
    )
    (
    typeof(null)
    )
    if (
    isMutable!This
    )
  2. RcPtr exchange(Rhs rhs)

Examples

//lvalue exchange
{
    shared x = RcPtr!(shared long).make(123);
    auto y = RcPtr!(shared long).make(42);

    auto z = x.exchange(y);

    assert(x.load.get == 42);
    assert(y.get == 42);
    assert(z.get == 123);
}

//rvalue exchange
{
    shared x = RcPtr!(shared long).make(123);
    auto y = RcPtr!(shared long).make(42);

    import core.lifetime : move;
    auto z = x.exchange(move(y));

    assert(x.load.get == 42);
    assert(y == null);
    assert(z.get == 123);
}

//null exchange (same as move)
{
    shared x = RcPtr!(shared long).make(123);

    auto z = x.exchange(null);

    assert(x.load == null);
    assert(z.get == 123);
}

//swap:
{
    shared x = RcPtr!(shared long).make(123);
    auto y = RcPtr!(shared long).make(42);

    import core.lifetime : move;
    //opAssign is same as store
    y = x.exchange(move(y));

    assert(x.load.get == 42);
    assert(y.get == 123);
}

Meta