Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fallout from #57667 #58021

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 6 additions & 33 deletions src/libsyntax/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use std::fmt::{self, Display, Debug};
use std::iter::FromIterator;
use std::ops::{Deref, DerefMut};
use std::{mem, ptr, slice, vec};
use std::{slice, vec};

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

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

// Leak self in case of panic.
// FIXME(eddyb) Use some sort of "free guard" that
// only deallocates, without dropping the pointee,
// in case the call the `f` below ends in a panic.
mem::forget(self);

unsafe {
ptr::write(p, f(ptr::read(p)));

// Recreate self from the raw pointer.
P { ptr: Box::from_raw(p) }
}
self
}

/// Optionally produce a new `P<T>` from `self` without reallocating.
pub fn filter_map<F>(mut self, f: F) -> Option<P<T>> where
F: FnOnce(T) -> Option<T>,
{
let p: *mut T = &mut *self.ptr;

// Leak self in case of panic.
// FIXME(eddyb) Use some sort of "free guard" that
// only deallocates, without dropping the pointee,
// in case the call the `f` below ends in a panic.
mem::forget(self);

unsafe {
if let Some(v) = f(ptr::read(p)) {
ptr::write(p, v);

// Recreate self from the raw pointer.
Some(P { ptr: Box::from_raw(p) })
} else {
drop(Box::from_raw(p));
None
}
}
*self.ptr = f(*self.ptr)?;
Some(self)
}
}

Expand Down