Skip to content

Commit 6fcb375

Browse files
authored
Rollup merge of rust-lang#58021 - ishitatsuyuki:57667-fix, r=RalfJung
Fix fallout from rust-lang#57667
2 parents de5c3c4 + a03e20d commit 6fcb375

File tree

1 file changed

+6
-33
lines changed

1 file changed

+6
-33
lines changed

src/libsyntax/ptr.rs

+6-33
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
use std::fmt::{self, Display, Debug};
3030
use std::iter::FromIterator;
3131
use std::ops::{Deref, DerefMut};
32-
use std::{mem, ptr, slice, vec};
32+
use std::{slice, vec};
3333

3434
use serialize::{Encodable, Decodable, Encoder, Decoder};
3535

@@ -66,45 +66,18 @@ impl<T: 'static> P<T> {
6666
pub fn map<F>(mut self, f: F) -> P<T> where
6767
F: FnOnce(T) -> T,
6868
{
69-
let p: *mut T = &mut *self.ptr;
69+
let x = f(*self.ptr);
70+
*self.ptr = x;
7071

71-
// Leak self in case of panic.
72-
// FIXME(eddyb) Use some sort of "free guard" that
73-
// only deallocates, without dropping the pointee,
74-
// in case the call the `f` below ends in a panic.
75-
mem::forget(self);
76-
77-
unsafe {
78-
ptr::write(p, f(ptr::read(p)));
79-
80-
// Recreate self from the raw pointer.
81-
P { ptr: Box::from_raw(p) }
82-
}
72+
self
8373
}
8474

8575
/// Optionally produce a new `P<T>` from `self` without reallocating.
8676
pub fn filter_map<F>(mut self, f: F) -> Option<P<T>> where
8777
F: FnOnce(T) -> Option<T>,
8878
{
89-
let p: *mut T = &mut *self.ptr;
90-
91-
// Leak self in case of panic.
92-
// FIXME(eddyb) Use some sort of "free guard" that
93-
// only deallocates, without dropping the pointee,
94-
// in case the call the `f` below ends in a panic.
95-
mem::forget(self);
96-
97-
unsafe {
98-
if let Some(v) = f(ptr::read(p)) {
99-
ptr::write(p, v);
100-
101-
// Recreate self from the raw pointer.
102-
Some(P { ptr: Box::from_raw(p) })
103-
} else {
104-
drop(Box::from_raw(p));
105-
None
106-
}
107-
}
79+
*self.ptr = f(*self.ptr)?;
80+
Some(self)
10881
}
10982
}
11083

0 commit comments

Comments
 (0)