| 807 | } |
| 808 | |
| 809 | fn map_elements<F: FnMut(T) -> Result<Transformed<T>>>( |
| 810 | mut self, |
| 811 | f: F, |
| 812 | ) -> Result<Transformed<Self>> { |
| 813 | // Rewrite in place so the existing heap allocation can be reused. |
| 814 | // `mem::take` hands the inner `C` to `f` while leaving |
| 815 | // `C::default()` in the slot, so an unwinding drop finds a valid |
| 816 | // `C` even if `f` panics or the `?` short-circuits. |
| 817 | let inner = std::mem::take(&mut *self); |
| 818 | Ok(inner.map_elements(f)?.update_data(|c| { |
| 819 | *self = c; |
| 820 | self |
| 821 | })) |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | impl<'a, T: 'a, C: TreeNodeContainer<'a, T> + Clone + Default> TreeNodeContainer<'a, T> |