syntax: implement in-place folding of P<T> and Vec<T>.
This commit is contained in:
parent
946bb4b10b
commit
f8df4fadc8
@ -35,8 +35,15 @@ pub trait MoveMap<T> {
|
||||
}
|
||||
|
||||
impl<T> MoveMap<T> for Vec<T> {
|
||||
fn move_map(self, f: |T| -> T) -> Vec<T> {
|
||||
self.move_iter().map(f).collect()
|
||||
fn move_map(mut self, f: |T| -> T) -> Vec<T> {
|
||||
use std::{mem, ptr};
|
||||
for p in self.mut_iter() {
|
||||
unsafe {
|
||||
// FIXME(#5016) this shouldn't need to zero to be safe.
|
||||
mem::move_val_init(p, f(ptr::read_and_zero(p)));
|
||||
}
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,8 +31,14 @@ impl<T: 'static> P<T> {
|
||||
f(*self.ptr)
|
||||
}
|
||||
|
||||
pub fn map(self, f: |T| -> T) -> P<T> {
|
||||
self.and_then(|x| P(f(x)))
|
||||
pub fn map(mut self, f: |T| -> T) -> P<T> {
|
||||
use std::{mem, ptr};
|
||||
unsafe {
|
||||
let p = &mut *self.ptr;
|
||||
// FIXME(#5016) this shouldn't need to zero to be safe.
|
||||
mem::move_val_init(p, f(ptr::read_and_zero(p)));
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user