mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-06 20:28:33 +00:00
Fix fallout from #57667
This commit is contained in:
parent
0f949c2fcc
commit
a03e20db6d
@ -29,7 +29,7 @@
|
|||||||
use std::fmt::{self, Display, Debug};
|
use std::fmt::{self, Display, Debug};
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
use std::ops::{Deref, DerefMut};
|
use std::ops::{Deref, DerefMut};
|
||||||
use std::{mem, ptr, slice, vec};
|
use std::{slice, vec};
|
||||||
|
|
||||||
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
use serialize::{Encodable, Decodable, Encoder, Decoder};
|
||||||
|
|
||||||
@ -66,45 +66,18 @@ impl<T: 'static> P<T> {
|
|||||||
pub fn map<F>(mut self, f: F) -> P<T> where
|
pub fn map<F>(mut self, f: F) -> P<T> where
|
||||||
F: FnOnce(T) -> T,
|
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.
|
self
|
||||||
// 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) }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Optionally produce a new `P<T>` from `self` without reallocating.
|
/// Optionally produce a new `P<T>` from `self` without reallocating.
|
||||||
pub fn filter_map<F>(mut self, f: F) -> Option<P<T>> where
|
pub fn filter_map<F>(mut self, f: F) -> Option<P<T>> where
|
||||||
F: FnOnce(T) -> Option<T>,
|
F: FnOnce(T) -> Option<T>,
|
||||||
{
|
{
|
||||||
let p: *mut T = &mut *self.ptr;
|
*self.ptr = f(*self.ptr)?;
|
||||||
|
Some(self)
|
||||||
// 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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user