Mat*_*ias 4 function-pointers rust
我想编写一个通用函数count_calls,该函数调用一个函数f,该函数采用函数指针 (lambda),其中count_calls计算函数f调用给定 lambda 函数的频率。
我对这种方法(Playground)感到挣扎。
fn count_calls<S, F>(s: S, f: F) -> u32
where
S: Clone,
F: Sized + FnMut(Fn() -> S) -> (),
{
let mut counter: u32 = 0;
f(|| {
counter += 1;
s.clone()
});
counter
}
#[cfg(test)]
mod stackoverflow {
use super::*;
fn f(p: fn() -> i32) {
p();
p();
}
#[test]
fn test() {
let counts = count_calls(3, f);
assert_eq!(counts, 2);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我得到错误:
error[E0277]: the size for values of type `(dyn std::ops::Fn() -> S + 'static)` cannot be known at compilation time
--> src/lib.rs:1:1
|
1 | / fn count_calls<S, F>(s: S, f: F) -> u32
2 | | where
3 | | S: Clone,
4 | | F: Sized + FnMut(Fn() -> S) -> (),
... |
12 | | counter
13 | | }
| |_^ doesn't have a size known at compile-time
|
= help: within `((dyn std::ops::Fn() -> S + 'static),)`, the trait `std::marker::Sized` is not implemented for `(dyn std::ops::Fn() -> S + 'static)`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
= note: required because it appears within the type `((dyn std::ops::Fn() -> S + 'static),)`
= note: required by `std::ops::FnMut`
Run Code Online (Sandbox Code Playgroud)
有人知道如何解决这个问题吗?
[编辑]
我认为使用Box<Fn()->S>可能是一个解决方案。但如果可能的话,我更喜欢仅堆栈的解决方案。
错误“在编译时无法知道类型值的大小(dyn std::ops::Fn() -> S + 'static)”是由您绑定的特征引起的F:
F: Sized + FnMut(Fn() -> S) -> ()
Run Code Online (Sandbox Code Playgroud)
这相当于F: Sized + FnMut(dyn Fn() -> S). 这意味着闭包F将按值接受一个 trait 对象 ( dyn Fn() -> S)。但是 trait 对象没有大小,不能按值传递(目前)。
一种解决方案是通过引用或在Box. rodrigo 的回答解释并讨论了这些解决方案。
不正确,我想。
一个想法是将另一个类型参数添加到count_calls:
fn count_calls<S, F, G>(s: S, f: F) -> u32
where
S: Clone,
F: Sized + FnMut(G),
G: Fn() -> S,
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用:
F: Sized + FnMut(Fn() -> S) -> ()
Run Code Online (Sandbox Code Playgroud)
这里的问题是 的类型参数count_calls是由 的调用者选择的count_calls。但我们实际上希望G始终成为我们自己的闭包类型。所以这是行不通的。
我们想要的是一个通用的闭包(我们可以在其中选择它的类型参数)。类似的事情是可能的,但仅限于生命周期参数。它被称为 HRTB,看起来像F: for<'a> Fn(&'a u32). 但这在这里没有帮助,因为我们需要一个类型参数并且for<T>不存在(还没有?)。
一种解决方案是不使用闭包,而是使用具有已知名称的类型来实现FnMut. 遗憾的是,您还不能Fn*在 stable 上为自己的类型实现特征。每晚,这都有效。
struct CallCounter<S> {
counter: u32,
s: S,
}
impl<S: Clone> FnOnce<()> for CallCounter<S> {
type Output = S;
extern "rust-call" fn call_once(self, _: ()) -> Self::Output {
// No point in incrementing the counter here
self.s
}
}
impl<S: Clone> FnMut<()> for CallCounter<S> {
extern "rust-call" fn call_mut(&mut self, _: ()) -> Self::Output {
self.counter += 1;
self.s.clone()
}
}
fn count_calls<S, F>(s: S, mut f: F) -> u32
where
S: Clone,
F: Sized + FnMut(&mut CallCounter<S>), // <----
{
let mut counter = CallCounter {
counter: 0,
s,
};
f(&mut counter); // <-------
counter.counter
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,现在您的公共接口中有这种奇怪的类型(应该是实现细节)。
除此之外,我想不出任何真正的解决方案(只有其他具有很多缺点的超级冗长的解决方案)。类型系统角的发展(特别是关于 GAT 和 HKT)可以在未来妥善解决这个问题。但是,我认为仍然缺少一些不同的功能;特别是,我认为所提议的 GAT 并不能解决这个问题。
因此,如果这是一个需要立即解决的现实生活问题,我会:
| 归档时间: |
|
| 查看次数: |
466 次 |
| 最近记录: |