Skip to content

Commit 869726d

Browse files
authored
Rollup merge of rust-lang#81619 - SkiFire13:resultshunt-inplace, r=the8472
Implement `SourceIterator` and `InPlaceIterable` for `ResultShunt`
2 parents a1c3449 + 2fb56cc commit 869726d

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

library/alloc/tests/vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,9 @@ fn test_from_iter_specialization_with_iterator_adapters() {
10041004
.map_while(Option::Some)
10051005
.peekable()
10061006
.skip(1)
1007-
.map(|e| std::num::NonZeroUsize::new(e));
1007+
.map(|e| if e != usize::MAX { Ok(std::num::NonZeroUsize::new(e)) } else { Err(()) });
10081008
assert_in_place_trait(&iter);
1009-
let sink = iter.collect::<Vec<_>>();
1009+
let sink = iter.collect::<Result<Vec<_>, _>>().unwrap();
10101010
let sinkptr = sink.as_ptr();
10111011
assert_eq!(srcptr, sinkptr as *const usize);
10121012
}

library/core/src/iter/adapters/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,26 @@ where
194194
self.try_fold(init, ok(fold)).unwrap()
195195
}
196196
}
197+
198+
#[unstable(issue = "none", feature = "inplace_iteration")]
199+
unsafe impl<S: Iterator, I, E> SourceIter for ResultShunt<'_, I, E>
200+
where
201+
I: SourceIter<Source = S>,
202+
{
203+
type Source = S;
204+
205+
#[inline]
206+
unsafe fn as_inner(&mut self) -> &mut S {
207+
// SAFETY: unsafe function forwarding to unsafe function with the same requirements
208+
unsafe { SourceIter::as_inner(&mut self.iter) }
209+
}
210+
}
211+
212+
// SAFETY: ResultShunt::next calls I::find, which has to advance `iter` in order to
213+
// return `Some(_)`. Since `iter` has type `I: InPlaceIterable` it's guaranteed that
214+
// at least one item will be moved out from the underlying source.
215+
#[unstable(issue = "none", feature = "inplace_iteration")]
216+
unsafe impl<I, T, E> InPlaceIterable for ResultShunt<'_, I, E> where
217+
I: Iterator<Item = Result<T, E>> + InPlaceIterable
218+
{
219+
}

0 commit comments

Comments
 (0)