Navigation Menu

Skip to content

Commit

Permalink
Use opt.take() instead of mem::replace(opt, None)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljedrz committed Oct 29, 2018
1 parent 4e88b73 commit 2203ec3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/librustc/ty/steal.rs
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

use rustc_data_structures::sync::{RwLock, ReadGuard, MappedReadGuard};
use std::mem;

/// The `Steal` struct is intended to used as the value for a query.
/// Specifically, we sometimes have queries (*cough* MIR *cough*)
Expand Down Expand Up @@ -51,7 +50,7 @@ impl<T> Steal<T> {

pub fn steal(&self) -> T {
let value_ref = &mut *self.value.try_write().expect("stealing value which is locked");
let value = mem::replace(value_ref, None);
let value = value_ref.take();
value.expect("attempt to read from stolen value")
}
}
8 changes: 3 additions & 5 deletions src/librustc_data_structures/tiny_list.rs
Expand Up @@ -22,8 +22,6 @@
//! If you expect to store more than 1 element in the common case, steer clear
//! and use a `Vec<T>`, `Box<[T]>`, or a `SmallVec<T>`.

use std::mem;

#[derive(Clone, Hash, Debug, PartialEq)]
pub struct TinyList<T: PartialEq> {
head: Option<Element<T>>
Expand Down Expand Up @@ -52,15 +50,15 @@ impl<T: PartialEq> TinyList<T> {
pub fn insert(&mut self, data: T) {
self.head = Some(Element {
data,
next: mem::replace(&mut self.head, None).map(Box::new),
next: self.head.take().map(Box::new)
});
}

#[inline]
pub fn remove(&mut self, data: &T) -> bool {
self.head = match self.head {
Some(ref mut head) if head.data == *data => {
mem::replace(&mut head.next, None).map(|x| *x)
head.next.take().map(|x| *x)
}
Some(ref mut head) => return head.remove_next(data),
None => return false,
Expand Down Expand Up @@ -100,7 +98,7 @@ impl<T: PartialEq> Element<T> {
if next.data != *data {
return next.remove_next(data)
} else {
mem::replace(&mut next.next, None)
next.next.take()
}
} else {
return false
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/method/probe.rs
Expand Up @@ -831,7 +831,7 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
}

let static_candidates = mem::replace(&mut self.static_candidates, vec![]);
let private_candidate = mem::replace(&mut self.private_candidate, None);
let private_candidate = self.private_candidate.take();
let unsatisfied_predicates = mem::replace(&mut self.unsatisfied_predicates, vec![]);

// things failed, so lets look at all traits, for diagnostic purposes now:
Expand Down

0 comments on commit 2203ec3

Please sign in to comment.