Trait core_extensions::callable::CallInto
source · [−]pub trait CallInto<Params> {
type Returns;
fn into_call_(self, params: Params) -> Self::Returns;
}
Available on crate feature
callable
only.Expand description
Implementable alternative to std::ops::FnOnce
.
Parameters
The Call*
traits encode multiple parameters like this:
-
0 parameters: by taking a
()
parameter, eg:foo.ref_call(())
. -
1 parameters: by taking the single parameter, eg:
foo.ref_call(10)
. -
2 or more parameters: by taking a tuple of the parameters, eg:
foo.ref_call((10, 20))
.
Example
use core_extensions::{impl_call, CallExt};
use std::iter::FromIterator;
struct Duplicator<T>(T);
impl_call! {
fn into_call[T](self: Duplicator<T>) -> T
where[
T: IntoIterator + Default,
T: FromIterator<<T as IntoIterator>::Item>,
T::Item: Clone,
] {
self.0
.into_iter()
.flat_map(|elem| vec![elem; 2] )
.collect()
}
}
assert_eq!(Duplicator(vec![3, 5]).into_call(()), vec![3, 3, 5, 5]);
assert_eq!(Duplicator(vec!["hi", "ho"]).into_call(()), vec!["hi", "hi", "ho", "ho"]);
Closure impls
Closures implement the Call*
traits,
and they always require a tuple of the parameters to be passed in.
use core_extensions::CallExt;
let orig = vec![3, 5, 8, 13, 21, 34];
let list = orig.clone();
let fn_0 = || list.into_iter().next();
assert_eq!(fn_0.into_call(()), Some(3));
let list = orig.clone();
let fn_1 = |i: usize| list.into_iter().nth(i);
assert_eq!(fn_1.into_call((3,)), Some(13));
let list = orig.clone();
let fn_2 = |s: usize, i: usize| list.into_iter().skip(s).nth(i);
assert_eq!(fn_2.into_call((3, 1)), Some(21));
Required Associated Types
Required Methods
sourcefn into_call_(self, params: Params) -> Self::Returns
fn into_call_(self, params: Params) -> Self::Returns
calls this function