|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
11 |
| -use owned_slice::OwnedSlice; |
12 |
| - |
13 | 11 | use std::ptr;
|
14 | 12 |
|
15 |
| -pub trait MoveMap<T>: Sized { |
16 |
| - fn move_map<F>(self, mut f: F) -> Self where F: FnMut(T) -> T { |
17 |
| - self.move_flat_map(|e| Some(f(e))) |
18 |
| - } |
| 13 | +pub trait MoveMap { |
| 14 | + type Item; |
| 15 | + fn move_map<F>(self, f: F) -> Self |
| 16 | + where F: FnMut(Self::Item) -> Self::Item; |
| 17 | +} |
19 | 18 |
|
| 19 | +pub trait MoveFlatMap { |
| 20 | + type Item; |
20 | 21 | fn move_flat_map<F, I>(self, f: F) -> Self
|
21 |
| - where F: FnMut(T) -> I, |
22 |
| - I: IntoIterator<Item=T>; |
| 22 | + where F: FnMut(Self::Item) -> I, |
| 23 | + I: IntoIterator<Item = Self::Item>; |
| 24 | +} |
| 25 | + |
| 26 | +impl<Container, T> MoveMap for Container |
| 27 | + where for<'a> &'a mut Container: IntoIterator<Item = &'a mut T> |
| 28 | +{ |
| 29 | + type Item = T; |
| 30 | + fn move_map<F>(mut self, mut f: F) -> Container where F: FnMut(T) -> T { |
| 31 | + for p in &mut self { |
| 32 | + unsafe { |
| 33 | + // FIXME(#5016) this shouldn't need to zero to be safe. |
| 34 | + ptr::write(p, f(ptr::read_and_drop(p))); |
| 35 | + } |
| 36 | + } |
| 37 | + self |
| 38 | + } |
23 | 39 | }
|
24 | 40 |
|
25 |
| -impl<T> MoveMap<T> for Vec<T> { |
| 41 | +impl<T> MoveFlatMap for Vec<T> { |
| 42 | + type Item = T; |
26 | 43 | fn move_flat_map<F, I>(mut self, mut f: F) -> Self
|
27 | 44 | where F: FnMut(T) -> I,
|
28 | 45 | I: IntoIterator<Item=T>
|
@@ -69,11 +86,13 @@ impl<T> MoveMap<T> for Vec<T> {
|
69 | 86 | }
|
70 | 87 | }
|
71 | 88 |
|
72 |
| -impl<T> MoveMap<T> for OwnedSlice<T> { |
| 89 | +impl<T> MoveFlatMap for ::ptr::P<[T]> { |
| 90 | + type Item = T; |
73 | 91 | fn move_flat_map<F, I>(self, f: F) -> Self
|
74 | 92 | where F: FnMut(T) -> I,
|
75 | 93 | I: IntoIterator<Item=T>
|
76 | 94 | {
|
77 |
| - OwnedSlice::from_vec(self.into_vec().move_flat_map(f)) |
| 95 | + let v: Vec<_> = self.into(); |
| 96 | + v.move_flat_map(f).into() |
78 | 97 | }
|
79 | 98 | }
|
0 commit comments