91cc872987
Add Cell::update This commit adds a new method `Cell::update`, which applies a function to the value inside the cell. Previously discussed in: https://github.com/rust-lang/rfcs/issues/2171 ### Motivation Updating `Cell`s is currently a bit verbose. Here are several real examples (taken from rustc and crossbeam): ```rust self.print_fuel.set(self.print_fuel.get() + 1); self.diverges.set(self.diverges.get() | Diverges::Always); let guard_count = self.guard_count.get(); self.guard_count.set(guard_count.checked_add(1).unwrap()); if guard_count == 0 { // ... } ``` With the addition of the new method `Cell::update`, this code can be simplified to: ```rust self.print_fuel.update(|x| x + 1); self.diverges.update(|x| x | Diverges::Always); if self.guard_count.update(|x| x.checked_add(1).unwrap()) == 1 { // ... } ``` ### Unresolved questions 1. Should we return the old value instead of the new value (like in `fetch_add` and `fetch_update`)? 2. Should the return type simply be `()`? 3. Naming: `update` vs `modify` vs `mutate` etc. cc @SimonSapin
76 lines
1.8 KiB
Rust
76 lines
1.8 KiB
Rust
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
// file at the top-level directory of this distribution and at
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
|
|
#![feature(ascii_ctype)]
|
|
#![feature(box_syntax)]
|
|
#![feature(cell_update)]
|
|
#![feature(core_float)]
|
|
#![feature(core_private_bignum)]
|
|
#![feature(core_private_diy_float)]
|
|
#![feature(dec2flt)]
|
|
#![feature(decode_utf8)]
|
|
#![feature(exact_size_is_empty)]
|
|
#![feature(fixed_size_array)]
|
|
#![feature(float_internals)]
|
|
#![feature(flt2dec)]
|
|
#![feature(fmt_internals)]
|
|
#![feature(hashmap_internals)]
|
|
#![feature(iterator_step_by)]
|
|
#![feature(iterator_flatten)]
|
|
#![feature(iterator_repeat_with)]
|
|
#![feature(nonzero)]
|
|
#![feature(pattern)]
|
|
#![feature(range_is_empty)]
|
|
#![feature(raw)]
|
|
#![feature(refcell_replace_swap)]
|
|
#![feature(slice_patterns)]
|
|
#![feature(slice_rotate)]
|
|
#![feature(sort_internals)]
|
|
#![feature(specialization)]
|
|
#![feature(step_trait)]
|
|
#![feature(str_internals)]
|
|
#![feature(test)]
|
|
#![feature(trusted_len)]
|
|
#![feature(try_from)]
|
|
#![feature(try_trait)]
|
|
#![feature(exact_chunks)]
|
|
#![cfg_attr(stage0, feature(atomic_nand))]
|
|
#![feature(reverse_bits)]
|
|
#![feature(inclusive_range_fields)]
|
|
#![feature(iterator_find_map)]
|
|
|
|
extern crate core;
|
|
extern crate test;
|
|
extern crate rand;
|
|
|
|
mod any;
|
|
mod array;
|
|
mod ascii;
|
|
mod atomic;
|
|
mod cell;
|
|
mod char;
|
|
mod clone;
|
|
mod cmp;
|
|
mod fmt;
|
|
mod hash;
|
|
mod intrinsics;
|
|
mod iter;
|
|
mod mem;
|
|
mod nonzero;
|
|
mod num;
|
|
mod ops;
|
|
mod option;
|
|
mod pattern;
|
|
mod ptr;
|
|
mod result;
|
|
mod slice;
|
|
mod str;
|
|
mod str_lossy;
|
|
mod tuple;
|