PR Proposal: `AtomicOptionBox::store_if_none`
What do you think about extending the AtomicOptionBox API with a function that only replaces the current content of an AtomicOptionBox if it's currently None?
Possible signature (subject to bikeshedding):
pub fn store_if_none(&self, other: Box<T>, order: Ordering) -> Result<(),Box<T>>
The Err content of the returned Result in case of failure is the other argument.
The implementation can use AtomicPtr::compare_exchange.
If this makes sense to you, I can prepare a PR with an implementation.
Motivation
My current use-case for AtomicOptionBox is as a cross-thread communication mechanism in a context where queuing is undesirable.
AtomicOptionBox::store works well for message types in which the latest takes precedence, so the sender can overwrite the older message regardless of whether the receiver got it or not.
The proposed AtomicOptionBox::store_if_none will work in an inverse situation, when the earliest message takes precedence.
The same can be achieved with std::sync::mpsc::sync_channel(bound=1) and try_send, but AtomicOptionBox, arguably, better expresses intent, and in my use-case would avoid adding a different communication mechanism.
For a similar reason as #9, this API could allow using plain release memory ordering. Since it "knows" that when the compare-exchange succeeds it won't need to order operations on a pointee because it will get back a none/null.
In combination with an acquire-ordered take, this could allow a very efficient 1-element channel with only the minimum needed orderings.