#operator #operator-overloading #function #fn #ops #traits #fn-mut

fn_ops

临时特性用于函数运算符重载,等待 FnFnMutFnOnce 的稳定化

1 个不稳定版本

使用旧的 Rust 2015

0.1.0 2015 年 12 月 24 日

#4#fn-mut

Apache-2.0 协议

7KB
74

fn_ops

Build Status Version License Downloads

文档

临时特性用于函数运算符重载,等待 FnFnMutFnOnce 的稳定化。

use fn_ops::*;

struct IsMultipleOf(i32);

impl FnOnce<(i32, i32)> for IsMultipleOf {
    type Output = bool;

    fn call_once(self, (x, y): (i32, i32)) -> bool { self.call((x, y)) }
}

impl FnMut<(i32, i32)> for IsMultipleOf {
    fn call_mut(&mut self, (x, y): (i32, i32)) -> bool { self.call((x, y)) }
}

impl Fn<(i32, i32)> for IsMultipleOf {
    fn call(&self, (x, y): (i32, i32)) -> bool {
        x * self.0 == y
    }
}

fn assert_fn<Args, F: Fn<Args, Output = bool>>(args: Args, f: F) {
    assert!(f.call(args))
}

assert_fn((1, 2), IsMultipleOf(2));
assert_fn((1, 2, 3), |x, y, z| x != y && y != z && z != x);

无运行时依赖