generics - 如何在不使函数泛型的情况下将具有泛型的闭包传递给函数?

标签 generics rust closures

我有一个函数可以与枚举一起使用来应用二进制函数。这是给口译员的:

use std::ops::*;

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Scalar {
    I64(i64),
    I32(i32),
    //many many others
}

pub trait TMath: Add + Mul + Sized {} //mark numerical types
impl<T: Add + Mul> TMath for T {}

fn add<T: TMath>(x: T, y: T) -> <T as Add>::Output {
    x + y
}

pub type NatBinExpr<T: TMath> = Fn(&T, &T) -> T;

我想做的事:

let result = bin_op(add, &Scalar::I32(1), &Scalar::I32(2));

同时也让它适用于任意二元函数:

let result = bin_op(Scalar::concat, &Scalar::I32(1), &Scalar::I32(2));

但是,我还没有找到一种方法来传递闭包而不使 bin_op 通用:

fn bin_op(apply: &NatBinExpr???, x: &Scalar, y: &Scalar) -> Scalar {
    match (x, y) {
        (Scalar::I64(a), Scalar::I64(b)) => Scalar::I64(apply(a, b)),
        (Scalar::I32(a), Scalar::I32(b)) => Scalar::I32(apply(a, b)),
    }
}

使 bin_op 通用化是不对的; bin_opScalar 进行操作,但内部操作是通用的。

我原来asked this question on Reddit

最佳答案

基本上有两种不同的方式来讨论函数类型:

  • 指针:fn(A, B) -> C,
  • 特征:Fn(A, B) -> C, FnMut(A, B) -> C, FnOnce(A, B) -> C.

无论哪种情况,它们都以参数和结果类型为特征。

那么,apply的参数和结果类型是什么?

视情况而定。

从您的示例中,我们可以看到 [i64, i32, ... 中的 TFnOnce(T, T) -> T ]

这不是一个类型,而是许多类型。因此它需要的不是单一的功能,而是多种功能;或者可能是多次实现 FnOnce 的函数对象。


函数对象路由仅在夜间可用,并且需要大量样板文件(宏对此有帮助):

#![feature(fn_traits)]
#![feature(unboxed_closures)]

use std::ops::*;

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum Scalar {
    I64(i64),
    I32(i32),
    //many many others
}

pub trait TMath: Add + Mul + Sized {} //mark numerical types

impl<T: Add + Mul> TMath for T {}

struct Adder;

impl FnOnce<(i64, i64)> for Adder {
    type Output = i64;
    extern "rust-call" fn call_once(self, args: (i64, i64)) -> i64 {
        args.0 + args.1
    }
}

impl FnMut<(i64, i64)> for Adder {
    extern "rust-call" fn call_mut(&mut self, args: (i64, i64)) -> i64 {
        args.0 + args.1
    }
}

impl Fn<(i64, i64)> for Adder {
    extern "rust-call" fn call(&self, args: (i64, i64)) -> i64 {
        args.0 + args.1
    }
}

impl FnOnce<(i32, i32)> for Adder {
    type Output = i32;
    extern "rust-call" fn call_once(self, args: (i32, i32)) -> i32 {
        args.0 + args.1
    }
}

impl FnMut<(i32, i32)> for Adder {
    extern "rust-call" fn call_mut(&mut self, args: (i32, i32)) -> i32 {
        args.0 + args.1
    }
}

impl Fn<(i32, i32)> for Adder {
    extern "rust-call" fn call(&self, args: (i32, i32)) -> i32  {
        args.0 + args.1
    }
}

fn bin_op<F>(apply: &F, x: Scalar, y: Scalar) -> Scalar
    where
        F: Fn(i64, i64) -> i64,
        F: Fn(i32, i32) -> i32,
{
    match (x, y) {
        (Scalar::I64(a), Scalar::I64(b))
            => Scalar::I64((apply as &Fn(i64, i64) -> i64)(a, b)),
        (Scalar::I32(a), Scalar::I32(b))
            => Scalar::I32((apply as &Fn(i32, i32) -> i32)(a, b)),
        _ => unreachable!(),
    }
}

fn main() {
    let result = bin_op(&Adder, Scalar::I32(1), Scalar::I32(2));
    println!("{:?}", result);
}

Prints I32(3).

关于generics - 如何在不使函数泛型的情况下将具有泛型的闭包传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52539771/

相关文章:

rust - From Trait - 如何从切片实现它

iterator - 错误 : cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495]

swift - 将 nil-coalescing 运算符与两个可选值一起使用时类型推断失败

java - 此代码出现运行时错误与编译错误的原因

java - 在java方法重写中使用通配符

javascript - 针对 globar var 的私有(private) var 当全局改变时不改变

rust - 闭包可能比当前函数长寿

java - 如何通过检查找到返回类型的参数化类型?

hashmap - 模式匹配选项时,引用类型不兼容的匹配臂引发错误

javascript - 您如何测试仅在闭包范围内可见的 javascript 方法?