Skip to content

Commit 6b6ff21

Browse files
committed
syntax: Rewrite P::map and MoveMap to not use unstable ptr::read_and_drop
While this is slower than before, it's not necessarily forward compatible with planned changes to the drop flag. Benchmarking suggests that this does not dramatically impact the compile times of rust.
1 parent 9d0b91d commit 6b6ff21

File tree

2 files changed

+4
-17
lines changed

2 files changed

+4
-17
lines changed

src/libsyntax/fold.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use codemap::{respan, Span, Spanned};
2525
use owned_slice::OwnedSlice;
2626
use parse::token;
2727
use ptr::P;
28-
use std::ptr;
2928
use util::small_vector::SmallVector;
3029

3130
use std::rc::Rc;
@@ -36,14 +35,8 @@ pub trait MoveMap<T> {
3635
}
3736

3837
impl<T> MoveMap<T> for Vec<T> {
39-
fn move_map<F>(mut self, mut f: F) -> Vec<T> where F: FnMut(T) -> T {
40-
for p in &mut self {
41-
unsafe {
42-
// FIXME(#5016) this shouldn't need to zero to be safe.
43-
ptr::write(p, f(ptr::read_and_drop(p)));
44-
}
45-
}
46-
self
38+
fn move_map<F>(self, mut f: F) -> Vec<T> where F: FnMut(T) -> T {
39+
self.into_iter().map(|p| f(p)).collect()
4740
}
4841
}
4942

src/libsyntax/ptr.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
use std::fmt::{self, Display, Debug};
4040
use std::hash::{Hash, Hasher};
4141
use std::ops::Deref;
42-
use std::ptr;
4342

4443
use serialize::{Encodable, Decodable, Encoder, Decoder};
4544

@@ -66,15 +65,10 @@ impl<T: 'static> P<T> {
6665
}
6766

6867
/// Transform the inner value, consuming `self` and producing a new `P<T>`.
69-
pub fn map<F>(mut self, f: F) -> P<T> where
68+
pub fn map<F>(self, f: F) -> P<T> where
7069
F: FnOnce(T) -> T,
7170
{
72-
unsafe {
73-
let p = &mut *self.ptr;
74-
// FIXME(#5016) this shouldn't need to drop-fill to be safe.
75-
ptr::write(p, f(ptr::read_and_drop(p)));
76-
}
77-
self
71+
P(f(*self.ptr))
7872
}
7973
}
8074

0 commit comments

Comments
 (0)