Function 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.

RcPtr exchange(MemoryOrder order = MemoryOrder.seq, This) (
  typeof(null)
) scope
if (isMutable!This);

RcPtr exchange(MemoryOrder order = MemoryOrder.seq, Rhs, This) (
  scope Rhs rhs
) scope
if (isRcPtr!Rhs && isMoveAssignable!(Rhs, This) && !is(Rhs == shared));

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);
}