pub trait CallExt {
    fn ref_call<P>(&self, params: P) -> Self::Returns
    where
        Self: CallRef<P>
, { ... } fn mut_call<P>(&mut self, params: P) -> Self::Returns
    where
        Self: CallMut<P>
, { ... } fn into_call<P>(self, params: P) -> Self::Returns
    where
        Self: Sized,
        Self: CallInto<P>
, { ... } }
Available on crate feature callable only.
Expand description

Extension trait for calling Call* closures.

Provided Methods

For calling CallRef::ref_call_, with the ability to specify the types of the arguments..

Example
use core_extensions::{impl_call, CallExt};

struct PushTwice;

impl_call! { 
    fn ref_call[T](self: PushTwice, vector: &mut Vec<T>, value: T )
    where[ T: Clone ]
    {
        vector.push(value.clone());
        vector.push(value);
    }
}

let mut vector = Vec::new();

// `Call*` style closures encode multiple parameters as tuples
PushTwice.ref_call((&mut vector, 3));
assert_eq!(vector, [3, 3]);

// The turbofish operator can be used to specify the types of the arguments.
PushTwice.ref_call::<(_, u128)>((&mut vector, 5));
assert_eq!(vector, [3, 3, 5, 5]);

PushTwice.ref_call((&mut vector, 8));
assert_eq!(vector, [3, 3, 5, 5, 8, 8]);

For calling CallMut::mut_call_, with the ability to specify the types of the arguments..

Example
use core_extensions::{impl_call, CallExt};

struct ComputeFib {
    nums: [u128; 2],
}

impl_call! { 
    fn mut_call(self: ComputeFib, numbers: &mut Vec<u128>) {
        let [l, r] = self.nums;
        let next = l + r;
        self.nums = [r, next];
        numbers.push(r);
    }
}

let mut fibs = ComputeFib {nums: [0, 1]};
 
let mut numbers = Vec::new();

// The turbofish operator can be used to specify the types of the arguments.
fibs.mut_call::<&mut Vec<u128>>(&mut numbers);
assert_eq!(numbers, [1]);

fibs.mut_call(&mut numbers);
assert_eq!(numbers, [1, 1]);

fibs.mut_call(&mut numbers);
assert_eq!(numbers, [1, 1, 2]);

fibs.mut_call(&mut numbers);
assert_eq!(numbers, [1, 1, 2, 3]);

fibs.mut_call(&mut numbers);
assert_eq!(numbers, [1, 1, 2, 3, 5]);

fibs.mut_call(&mut numbers);
assert_eq!(numbers, [1, 1, 2, 3, 5, 8]);

For calling CallInto::into_call_, with the ability to specify the types of the arguments..

Example
use core_extensions::{impl_call, CallExt};

use std::iter::FromIterator;

struct IntoElem<T>(T);

impl_call! { 
    fn into_call[T](self: IntoElem<T>, nth: usize) -> Option<T::Item>
    where[ T: IntoIterator ]
    {
        self.0.into_iter().nth(nth)
    }
}
 
let list = vec![3, 5, 8, 13, 21, 34, 55, 89];
 
// The turbofish operator can be used to specify the types of the arguments.
assert_eq!(IntoElem(list.clone()).into_call::<usize>(0), Some(3));
 
assert_eq!(IntoElem(list.clone()).into_call(1), Some(5));
 
assert_eq!(IntoElem(list.clone()).into_call(2), Some(8));
 
assert_eq!(IntoElem(list.clone()).into_call(3), Some(13));
 
assert_eq!(IntoElem(list.clone()).into_call(7), Some(89));

Implementors