Skip to content

Commit 11f01bf

Browse files
committedMay 22, 2019
Auto merge of #61044 - Centril:rollup-ztsgb9p, r=Centril
Rollup of 8 pull requests Successful merges: - #60300 (Allow null-pointer-optimized enums in FFI if their underlying representation is FFI safe) - #60773 (Always try to project predicates when finding auto traits in rustdoc) - #60809 (Add FAQ for NLL migration) - #61023 (Migrate from recursion to iterate on qualify consts visitor impl) - #61029 (Simplify RefCell minimum_spanning_tree example) - #61030 (Make maybe_codegen_consume_direct iterate instead of doing recursion) - #61034 (rustc_metadata: parametrize schema::CrateRoot by 'tcx and rip out old unused incremental infra.) - #61037 (Update clippy submodule) Failed merges: r? @ghost
·
1.88.01.37.0
2 parents 37ff5d3 + 6806517 commit 11f01bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+582
-838
lines changed
 

‎src/libcore/cell.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,26 @@
6767
//! mutability:
6868
//!
6969
//! ```
70+
//! use std::cell::{RefCell, RefMut};
7071
//! use std::collections::HashMap;
71-
//! use std::cell::RefCell;
7272
//! use std::rc::Rc;
7373
//!
7474
//! fn main() {
7575
//! let shared_map: Rc<RefCell<_>> = Rc::new(RefCell::new(HashMap::new()));
76-
//! shared_map.borrow_mut().insert("africa", 92388);
77-
//! shared_map.borrow_mut().insert("kyoto", 11837);
78-
//! shared_map.borrow_mut().insert("piccadilly", 11826);
79-
//! shared_map.borrow_mut().insert("marbles", 38);
76+
//! // Create a new block to limit the scope of the dynamic borrow
77+
//! {
78+
//! let mut map: RefMut<_> = shared_map.borrow_mut();
79+
//! map.insert("africa", 92388);
80+
//! map.insert("kyoto", 11837);
81+
//! map.insert("piccadilly", 11826);
82+
//! map.insert("marbles", 38);
83+
//! }
84+
//!
85+
//! // Note that if we had not let the previous borrow of the cache fall out
86+
//! // of scope then the subsequent borrow would cause a dynamic thread panic.
87+
//! // This is the major hazard of using `RefCell`.
88+
//! let total: i32 = shared_map.borrow().values().sum();
89+
//! println!("{}", total);
8090
//! }
8191
//! ```
8292
//!
@@ -102,27 +112,15 @@
102112
//!
103113
//! impl Graph {
104114
//! fn minimum_spanning_tree(&self) -> Vec<(i32, i32)> {
105-
//! // Create a new scope to contain the lifetime of the
106-
//! // dynamic borrow
107-
//! {
108-
//! // Take a reference to the inside of cache cell
109-
//! let mut cache = self.span_tree_cache.borrow_mut();
110-
//! if cache.is_some() {
111-
//! return cache.as_ref().unwrap().clone();
112-
//! }
113-
//!
114-
//! let span_tree = self.calc_span_tree();
115-
//! *cache = Some(span_tree);
116-
//! }
115+
//! self.span_tree_cache.borrow_mut()
116+
//! .get_or_insert_with(|| self.calc_span_tree())
117+
//! .clone()
118+
//! }
117119
//!
118-
//! // Recursive call to return the just-cached value.
119-
//! // Note that if we had not let the previous borrow
120-
//! // of the cache fall out of scope then the subsequent
121-
//! // recursive borrow would cause a dynamic thread panic.
122-
//! // This is the major hazard of using `RefCell`.
123-
//! self.minimum_spanning_tree()
120+
//! fn calc_span_tree(&self) -> Vec<(i32, i32)> {
121+
//! // Expensive computation goes here
122+
//! vec![]
124123
//! }
125-
//! # fn calc_span_tree(&self) -> Vec<(i32, i32)> { vec![] }
126124
//! }
127125
//! ```
128126
//!

‎src/libcore/num/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ assert_eq!(size_of::<Option<core::num::", stringify!($Ty), ">>(), size_of::<", s
5050
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
5151
#[repr(transparent)]
5252
#[rustc_layout_scalar_valid_range_start(1)]
53+
#[cfg_attr(not(stage0), rustc_nonnull_optimization_guaranteed)]
5354
pub struct $Ty($Int);
5455
}
5556

0 commit comments

Comments
 (0)
Please sign in to comment.