Skip to content

Commit

Permalink
feat: iter::map on Fn instead of FnOnce
Browse files Browse the repository at this point in the history
  • Loading branch information
julio4 committed Jan 4, 2025
1 parent 66be2c6 commit 70b04ac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 4 additions & 6 deletions corelib/src/iter/adapters/map.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ pub(crate) impl MapImpl<I, F> of MapTrait<I, F> {

impl MapIterator<
I,
B,
F,
impl TIter: Iterator<I>,
+core::ops::FnOnce<F, (TIter::Item,)>[Output: B],
impl func: core::ops::Fn<F, (TIter::Item,)>,
+Destruct<I>,
+Destruct<F>,
+Copy<F>,
> of Iterator<Map<I, F>> {
type Item = B;
fn next(ref self: Map<I, F>) -> Option<B> {
self.iter.next().map(self.f)
type Item = func::Output;
fn next(ref self: Map<I, F>) -> Option<func::Output> {
self.iter.next().map(@self.f)
}
}
9 changes: 9 additions & 0 deletions corelib/src/ops/function.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ impl FnOnceImpl<T, Args, +Destruct<T>, +Fn<T, Args>> of FnOnce<T, Args> {
}
}

/// An implementation of `FnOnce` when `Fn` is implemented and the receiver is by-reference.
/// Makes sure we can always pass an `Fn` to a function that expects an `FnOnce`.
impl FnOnceSnapImpl<T, Args, +Destruct<T>, +Fn<T, Args>> of FnOnce<@T, Args> {
type Output = Fn::<T, Args>::Output;
fn call(self: @T, args: Args) -> Self::Output {
Fn::call(self, args)
}
}

/// The version of the call operator that takes a by-snapshot receiver.
///
/// Instances of `Fn` can be called multiple times.
Expand Down

0 comments on commit 70b04ac

Please sign in to comment.