Skip to content

Commit c2b9cdd

Browse files
committed
Use the ? operator
1 parent 3c3115a commit c2b9cdd

File tree

12 files changed

+78
-128
lines changed

12 files changed

+78
-128
lines changed

rayon-core/src/internal/worker.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,8 @@ pub fn if_in_worker_thread<F, R>(if_true: F) -> Option<R>
6060
where
6161
F: FnOnce(&WorkerThread) -> R,
6262
{
63-
let worker_thread = registry::WorkerThread::current();
64-
if worker_thread.is_null() {
65-
None
66-
} else {
67-
unsafe {
68-
let wt = WorkerThread {
69-
thread: &*worker_thread,
70-
};
71-
Some(if_true(&wt))
72-
}
63+
unsafe {
64+
let thread = registry::WorkerThread::current().as_ref()?;
65+
Some(if_true(&WorkerThread { thread }))
7366
}
7467
}

rayon-core/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl ThreadPoolBuilder {
202202
/// will return an error. An `Ok` result indicates that this
203203
/// is the first initialization of the thread pool.
204204
pub fn build_global(self) -> Result<(), ThreadPoolBuildError> {
205-
let registry = try!(registry::init_global_registry(self));
205+
let registry = registry::init_global_registry(self)?;
206206
registry.wait_until_primed();
207207
Ok(())
208208
}
@@ -235,7 +235,8 @@ impl ThreadPoolBuilder {
235235

236236
/// Get the thread name for the thread with the given index.
237237
fn get_thread_name(&mut self, index: usize) -> Option<String> {
238-
self.get_thread_name.as_mut().map(|c| c(index))
238+
let f = self.get_thread_name.as_mut()?;
239+
Some(f(index))
239240
}
240241

241242
/// Set a closure which takes a thread index and returns

rayon-core/src/registry.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ pub fn init_global_registry(
7474
called = true;
7575
});
7676
if called {
77-
init_result.map(|()| &**global_registry())
77+
init_result?;
78+
Ok(&**global_registry())
7879
} else {
7980
Err(ThreadPoolBuildError::new(
8081
ErrorKind::GlobalPoolAlreadyInitialized,
@@ -87,7 +88,9 @@ pub fn init_global_registry(
8788
/// function. Declared `unsafe` because it writes to `THE_REGISTRY` in
8889
/// an unsynchronized fashion.
8990
unsafe fn init_registry(builder: ThreadPoolBuilder) -> Result<(), ThreadPoolBuildError> {
90-
Registry::new(builder).map(|registry| THE_REGISTRY = Some(leak(registry)))
91+
let registry = Registry::new(builder)?;
92+
THE_REGISTRY = Some(leak(registry));
93+
Ok(())
9194
}
9295

9396
struct Terminator<'a>(&'a Arc<Registry>);
@@ -179,12 +182,12 @@ impl Registry {
179182
/// Returns the current `WorkerThread` if it's part of this `Registry`.
180183
pub fn current_thread(&self) -> Option<&WorkerThread> {
181184
unsafe {
182-
if let Some(worker) = WorkerThread::current().as_ref() {
183-
if worker.registry().id() == self.id() {
184-
return Some(worker);
185-
}
185+
let worker = WorkerThread::current().as_ref()?;
186+
if worker.registry().id() == self.id() {
187+
Some(worker)
188+
} else {
189+
None
186190
}
187-
None
188191
}
189192
}
190193

rayon-core/src/thread_pool/mod.rs

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub struct ThreadPool {
5454
}
5555

5656
pub fn build(builder: ThreadPoolBuilder) -> Result<ThreadPool, ThreadPoolBuildError> {
57-
let registry = try!(Registry::new(builder));
57+
let registry = Registry::new(builder)?;
5858
Ok(ThreadPool { registry })
5959
}
6060

@@ -165,14 +165,8 @@ impl ThreadPool {
165165
/// [snt]: struct.ThreadPoolBuilder.html#method.num_threads
166166
#[inline]
167167
pub fn current_thread_index(&self) -> Option<usize> {
168-
unsafe {
169-
let curr = WorkerThread::current().as_ref()?;
170-
if curr.registry().id() == self.registry.id() {
171-
Some(curr.index())
172-
} else {
173-
None
174-
}
175-
}
168+
let curr = self.registry.current_thread()?;
169+
Some(curr.index())
176170
}
177171

178172
/// Returns true if the current worker thread currently has "local
@@ -198,14 +192,8 @@ impl ThreadPool {
198192
/// [deque]: https://en.wikipedia.org/wiki/Double-ended_queue
199193
#[inline]
200194
pub fn current_thread_has_pending_tasks(&self) -> Option<bool> {
201-
unsafe {
202-
let curr = WorkerThread::current().as_ref()?;
203-
if curr.registry().id() == self.registry.id() {
204-
Some(!(*curr).local_deque_is_empty())
205-
} else {
206-
None
207-
}
208-
}
195+
let curr = self.registry.current_thread()?;
196+
Some(!curr.local_deque_is_empty())
209197
}
210198

211199
/// Execute `oper_a` and `oper_b` in the thread-pool and return
@@ -324,12 +312,8 @@ impl fmt::Debug for ThreadPool {
324312
#[inline]
325313
pub fn current_thread_index() -> Option<usize> {
326314
unsafe {
327-
let curr = WorkerThread::current();
328-
if curr.is_null() {
329-
None
330-
} else {
331-
Some((*curr).index())
332-
}
315+
let curr = WorkerThread::current().as_ref()?;
316+
Some(curr.index())
333317
}
334318
}
335319

@@ -342,11 +326,7 @@ pub fn current_thread_index() -> Option<usize> {
342326
#[inline]
343327
pub fn current_thread_has_pending_tasks() -> Option<bool> {
344328
unsafe {
345-
let curr = WorkerThread::current();
346-
if curr.is_null() {
347-
None
348-
} else {
349-
Some(!(*curr).local_deque_is_empty())
350-
}
329+
let curr = WorkerThread::current().as_ref()?;
330+
Some(!curr.local_deque_is_empty())
351331
}
352332
}

src/iter/chain.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ where
6060
}
6161

6262
fn opt_len(&self) -> Option<usize> {
63-
match (self.a.opt_len(), self.b.opt_len()) {
64-
(Some(a_len), Some(b_len)) => a_len.checked_add(b_len),
65-
_ => None,
66-
}
63+
self.a.opt_len()?.checked_add(self.b.opt_len()?)
6764
}
6865
}
6966

src/iter/chunks.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,16 @@ where
163163
type Item = Vec<P::Item>;
164164

165165
fn next(&mut self) -> Option<Self::Item> {
166-
match self.inner.take() {
167-
Some(producer) => {
168-
if self.len > self.chunk_size {
169-
let (left, right) = producer.split_at(self.chunk_size);
170-
self.inner = Some(right);
171-
self.len -= self.chunk_size;
172-
Some(left.into_iter().collect())
173-
} else {
174-
debug_assert!(self.len > 0);
175-
self.len = 0;
176-
Some(producer.into_iter().collect())
177-
}
178-
}
179-
_ => None,
166+
let producer = self.inner.take()?;
167+
if self.len > self.chunk_size {
168+
let (left, right) = producer.split_at(self.chunk_size);
169+
self.inner = Some(right);
170+
self.len -= self.chunk_size;
171+
Some(left.into_iter().collect())
172+
} else {
173+
debug_assert!(self.len > 0);
174+
self.len = 0;
175+
Some(producer.into_iter().collect())
180176
}
181177
}
182178

@@ -201,24 +197,20 @@ where
201197
P: Producer,
202198
{
203199
fn next_back(&mut self) -> Option<Self::Item> {
204-
match self.inner.take() {
205-
Some(producer) => {
206-
if self.len > self.chunk_size {
207-
let mut size = self.len % self.chunk_size;
208-
if size == 0 {
209-
size = self.chunk_size;
210-
}
211-
let (left, right) = producer.split_at(self.len - size);
212-
self.inner = Some(left);
213-
self.len -= size;
214-
Some(right.into_iter().collect())
215-
} else {
216-
debug_assert!(self.len > 0);
217-
self.len = 0;
218-
Some(producer.into_iter().collect())
219-
}
200+
let producer = self.inner.take()?;
201+
if self.len > self.chunk_size {
202+
let mut size = self.len % self.chunk_size;
203+
if size == 0 {
204+
size = self.chunk_size;
220205
}
221-
_ => None,
206+
let (left, right) = producer.split_at(self.len - size);
207+
self.inner = Some(left);
208+
self.len -= size;
209+
Some(right.into_iter().collect())
210+
} else {
211+
debug_assert!(self.len > 0);
212+
self.len = 0;
213+
Some(producer.into_iter().collect())
222214
}
223215
}
224216
}

src/iter/intersperse.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ where
4747
}
4848

4949
fn opt_len(&self) -> Option<usize> {
50-
match self.base.opt_len() {
51-
None => None,
52-
Some(0) => Some(0),
53-
Some(len) => len.checked_add(len - 1),
50+
match self.base.opt_len()? {
51+
0 => Some(0),
52+
len => len.checked_add(len - 1),
5453
}
5554
}
5655
}

src/iter/map_with.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,8 @@ where
194194
type Item = R;
195195

196196
fn next(&mut self) -> Option<R> {
197-
self.base
198-
.next()
199-
.map(|item| (self.map_op)(&mut self.item, item))
197+
let item = self.base.next()?;
198+
Some((self.map_op)(&mut self.item, item))
200199
}
201200

202201
fn size_hint(&self) -> (usize, Option<usize>) {
@@ -211,9 +210,8 @@ where
211210
R: Send,
212211
{
213212
fn next_back(&mut self) -> Option<R> {
214-
self.base
215-
.next_back()
216-
.map(|item| (self.map_op)(&mut self.item, item))
213+
let item = self.base.next_back()?;
214+
Some((self.map_op)(&mut self.item, item))
217215
}
218216
}
219217

src/iter/mod.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,9 +1350,8 @@ pub trait ParallelIterator: Sized + Send {
13501350
K: Ord + Send,
13511351
F: Sync + Send + Fn(&Self::Item) -> K,
13521352
{
1353-
self.map(|x| (f(&x), x))
1354-
.min_by(|a, b| (a.0).cmp(&b.0))
1355-
.map(|(_, x)| x)
1353+
let (_, x) = self.map(|x| (f(&x), x)).min_by(|a, b| (a.0).cmp(&b.0))?;
1354+
Some(x)
13561355
}
13571356

13581357
/// Computes the maximum of all the items in the iterator. If the
@@ -1434,9 +1433,8 @@ pub trait ParallelIterator: Sized + Send {
14341433
K: Ord + Send,
14351434
F: Sync + Send + Fn(&Self::Item) -> K,
14361435
{
1437-
self.map(|x| (f(&x), x))
1438-
.max_by(|a, b| (a.0).cmp(&b.0))
1439-
.map(|(_, x)| x)
1436+
let (_, x) = self.map(|x| (f(&x), x)).max_by(|a, b| (a.0).cmp(&b.0))?;
1437+
Some(x)
14401438
}
14411439

14421440
/// Takes two iterators and creates a new iterator over both.
@@ -2329,10 +2327,8 @@ pub trait IndexedParallelIterator: ParallelIterator {
23292327
where
23302328
P: Fn(Self::Item) -> bool + Sync + Send,
23312329
{
2332-
self.map(predicate)
2333-
.enumerate()
2334-
.find_any(|&(_, p)| p)
2335-
.map(|(i, _)| i)
2330+
let (i, _) = self.map(predicate).enumerate().find_any(|&(_, p)| p)?;
2331+
Some(i)
23362332
}
23372333

23382334
/// Searches for the sequentially **first** item in the parallel iterator
@@ -2363,10 +2359,8 @@ pub trait IndexedParallelIterator: ParallelIterator {
23632359
where
23642360
P: Fn(Self::Item) -> bool + Sync + Send,
23652361
{
2366-
self.map(predicate)
2367-
.enumerate()
2368-
.find_first(|&(_, p)| p)
2369-
.map(|(i, _)| i)
2362+
let (i, _) = self.map(predicate).enumerate().find_first(|&(_, p)| p)?;
2363+
Some(i)
23702364
}
23712365

23722366
/// Searches for the sequentially **last** item in the parallel iterator
@@ -2397,10 +2391,8 @@ pub trait IndexedParallelIterator: ParallelIterator {
23972391
where
23982392
P: Fn(Self::Item) -> bool + Sync + Send,
23992393
{
2400-
self.map(predicate)
2401-
.enumerate()
2402-
.find_last(|&(_, p)| p)
2403-
.map(|(i, _)| i)
2394+
let (i, _) = self.map(predicate).enumerate().find_last(|&(_, p)| p)?;
2395+
Some(i)
24042396
}
24052397

24062398
#[doc(hidden)]

src/iter/try_reduce_with.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ where
121121
}
122122

123123
fn complete(self) -> Option<T> {
124-
self.opt_result.map(|result| match result {
124+
let result = self.opt_result?;
125+
Some(match result {
125126
Ok(ok) => T::from_ok(ok),
126127
Err(error) => T::from_error(error),
127128
})

0 commit comments

Comments
 (0)