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) )
  2. RcPtr exchange(Rhs rhs)
    struct RcPtr
    scope
    exchange
    (
    MemoryOrder order = MemoryOrder.seq
    Rhs
    this This
    )
    (
    scope Rhs rhs
    )
    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);
}

Meta