lender/sources/
once_with.rs1use crate::{higher_order::FnOnceHKA, DoubleEndedLender, ExactSizeLender, FusedLender, Lend, Lender, Lending};
2
3pub fn once_with<St, F>(state: St, f: F) -> OnceWith<St, F>
16where
17 F: for<'all> FnOnceHKA<'all, &'all mut St>,
18{
19 OnceWith { state, f: Some(f) }
20}
21
22pub struct OnceWith<St, F> {
28 state: St,
29 f: Option<F>,
30}
31
32impl<'lend, St, F> Lending<'lend> for OnceWith<St, F>
33where
34 F: for<'all> FnOnceHKA<'all, &'all mut St>,
35{
36 type Lend = <F as FnOnceHKA<'lend, &'lend mut St>>::B;
37}
38
39impl<St, F> Lender for OnceWith<St, F>
40where
41 F: for<'all> FnOnceHKA<'all, &'all mut St>,
42{
43 #[inline]
44 fn next(&mut self) -> Option<Lend<'_, Self>> {
45 self.f.take().map(|f| f(&mut self.state))
46 }
47 #[inline]
48 fn size_hint(&self) -> (usize, Option<usize>) {
49 if self.f.is_some() {
50 (1, Some(1))
51 } else {
52 (0, Some(0))
53 }
54 }
55}
56
57impl<St, F> DoubleEndedLender for OnceWith<St, F>
58where
59 F: for<'all> FnOnceHKA<'all, &'all mut St>,
60{
61 #[inline]
62 fn next_back(&mut self) -> Option<Lend<'_, Self>> {
63 self.next()
64 }
65}
66
67impl<St, F> ExactSizeLender for OnceWith<St, F>
68where
69 F: for<'all> FnOnceHKA<'all, &'all mut St>,
70{
71 #[inline]
72 fn len(&self) -> usize {
73 self.size_hint().0
74 }
75}
76
77impl<St, F> FusedLender for OnceWith<St, F> where F: for<'all> FnOnceHKA<'all, &'all mut St> {}