lending_iterator/lending_iterator/constructors/from_iter.rs
1/// "Lifts" / converts an \[`Into`\][`Iterator`] into an
2/// <code>impl [LendingIterator]</code>
3///
4/// - This is a free function version of the [`.into_lending_iter()`] method
5/// provided by the eponymous [extension trait](https://docs.rs/extension_traits).
6///
7/// That is, feel free to check out that extension method, since in practice
8/// it's even more ergonomic to use.
9///
10/// [`.into_lending_iter()`]: trait@super::into_lending_iter#impl-into_lending_iter<I%2C%20IntoIter>
11///
12/// - See the [`.into_iter()`] method on [`LendingIterator`] for the reverse operation.
13///
14/// [`.into_iter()`]: LendingIterator::into_iter
15pub
16fn from_iter<I : IntoIterator> (it: I)
17 -> FromIter<I::IntoIter>
18{
19 constructors::FromIter(it.into_iter())
20}
21
22/// Note: since this wrapper only exists to avoid coherence issues,
23/// it is **guaranteed** to be a `#[repr(transparent)]` wrapper around its
24/// inner `I`.
25///
26/// This is a property `unsafe` code can rely on: it can thus use transmute to
27/// construct it.
28///
29/// It is also a property that will be upheld within future versions (should
30/// this property ever be broken in the future, the change would then be a
31/// semver-breaking one, and the type would be renamed to avoid footguns).
32#[repr(transparent)]
33pub
34struct FromIter<I : ?Sized + Iterator>(
35 pub I,
36);
37
38#[gat]
39impl<I : ?Sized + Iterator>
40 LendingIterator
41for
42 FromIter<I>
43{
44 type Item<'__>
45 where
46 Self : '__,
47 =
48 I::Item
49 ;
50
51 fn next (
52 self: &'_ mut FromIter<I>,
53 ) -> Option<I::Item>
54 {
55 self.0.next()
56 }
57}