以下函数
fn ptr_of(f: impl Fn()) -> fn() {
f as fn()
}
导致此错误:
non-primitive cast: `impl Fn()` as `fn()`
我错过了什么?
语境
我尝试做的事情实际上略有不同,如下所示,但上面的例子是问题的最简单形式。
trait FnExt {
fn do_smth_with_ptr(&self);
}
impl<T: Fn()> FnExt for T {
fn do_smth_with_ptr(&self) {
let ptr: fn() = self as fn(); //error here
todo!();
}
}
fn example() {
example2.do_smth_with_ptr();
}
fn example2() {};
我期望它能起作用,因为尝试ptr_of
使用类似以下示例的特定函数是有效的:
fn test() {}
let ptr = test as fn();