Is there a way to "check and set" the value of a `std::cell::Cell`?

Daniel Robertson

I was looking for something that would compile to the magical cmpxchg instruction. After digging into the documentation, I couldn't find anything that would accomplish this for Cell.

Maybe this is an antipattern?

Code:

After digging into the code I added the following to Cell's implementation to see if it would work.

pub fn cmp_and_set(&self, old: T, new: T) -> T {
    unsafe {
        ::intrinsics::atomic_cxchg(self.value.get(), old, new)
    }
}
// ^ there are many many reasons why this is not so great
// this is just an example of what I'm looking for

simple usage

fn main() {
    let c0 = Cell::new(10);
    let val0 = c0.cmp_and_set(11, 5);
    assert_eq!(c0.get(), 5);
    let val1 = c0.cmp_and_set(10, 42);
    assert_eq!(c0.get(), 42);
}

As far as I can tell, for very basic cases it works, but again there are many many reasons why the particular implementation is less than stellar. The fact that I edited the standard library to get what I was looking for means I'm certainly attempting to implement some sort of antipattern.

Background:

This was prompted from re-reading the following from The Rust Programming Language

It is still possible to violate your own invariants using this wrapper, so be careful when using it. If a field is wrapped in Cell, it's a nice indicator that the chunk of data is mutable and may not stay the same between the time you first read it and when you intend to use it.

Matthieu M.

TL;DR: No, there's not, because it's unnecessary.


Compare and Set is only valuable in two actors (or more) are modifying the object in parallel.

While Cell allows internal mutability, it is not thread-safe, so you will never be in a situation where two actors will be attempting to modify it in parallel.

Therefore, you can just use get(), compare, and set() if it suits you. And nobody will change the value between your get() and set(), providing you do not call other code yourself.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy the cell value to another cell if

From Dev

Replace value in cell as cell reference

From Dev

Is there a way to have my formulas reference a single cell value so that I can change multiple formulas with the one cell?

From Dev

Change value in a cell based on value in another cell

From Dev

Clear cell value of adjacent cell if value is a date

From Dev

Clear cell value of adjacent cell if value is a date

From Dev

Comparing dataframe cell value to previous cell value

From Dev

Any way to convert excel cell value in to html format?

From Dev

Excel VBA: Way to find the value of a cell with multiple criteria

From Dev

Can this cell numeric value be "displayed" in a different way in Excel?

From Dev

Simplest way to edit value of a single cell in SQL Management Studio

From Dev

Is there a way to show an object in a cell without refreshing the cell?

From Dev

Excel copy cell value to another cell if cell value changes

From Dev

Excel copy cell value to another cell if cell value changes

From Dev

Change Cell Value if There is Overflow

From Dev

Cancel a cell value in a QTableView

From Dev

get cell value in mRender

From Dev

Get the value of a textbox to a cell

From Dev

Return value of adjacent cell

From Dev

find a value of table cell

From Dev

Compare value of cell in vba

From Dev

Changing value of a cell with JavaScript

From Dev

String that changes with cell value?

From Dev

Html table cell value

From Dev

PHPExcel search cell by value

From Dev

Increasing value of cell

From Dev

Adding a Value to the Neighbor Cell

From Dev

DataGridView Cell Value Changing

From Dev

Adding a value to a cell in javascript

Related Related

HotTag

Archive