Rust 中的惯用回调 [英] Idiomatic callbacks in Rust

查看:31
本文介绍了Rust 中的惯用回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C/C++ 中,我通常使用普通函数指针进行回调,也可能传递一个 void* userdata 参数.像这样:

In C/C++ I'd normally do callbacks with a plain function pointer, maybe passing a void* userdata parameter too. Something like this:

typedef void (*Callback)();

class Processor
{
public:
    void setCallback(Callback c)
    {
        mCallback = c;
    }

    void processEvents()
    {
        for (...)
        {
            ...
            mCallback();
        }
    }
private:
    Callback mCallback;
};

在 Rust 中这样做的惯用方法是什么?具体来说,我的 setCallback() 函数应该采用什么类型,mCallback 应该采用什么类型?它应该需要一个 Fn 吗?也许FnMut?我要保存它Boxed 吗?一个例子会很棒.

What is the idiomatic way of doing this in Rust? Specifically, what types should my setCallback() function take, and what type should mCallback be? Should it take an Fn? Maybe FnMut? Do I save it Boxed? An example would be amazing.

推荐答案

简短回答:为了获得最大的灵活性,您可以将回调存储为一个装箱的 FnMut 对象,并在回调类型上使用通用的回调 setter.答案的最后一个示例中显示了此代码.如需更详细的解释,请继续阅读.

Short answer: For maximum flexibility, you can store the callback as a boxed FnMut object, with the callback setter generic on callback type. The code for this is shown in the last example in the answer. For a more detailed explanation, read on.

问题中与 C++ 代码最接近的等价物是将回调声明为 fn 类型.fn 封装了由 fn 关键字定义的函数,很像 C++ 的函数指针:

The closest equivalent of the C++ code in the question would be declaring callback as a fn type. fn encapsulates functions defined by the fn keyword, much like C++'s function pointers:

type Callback = fn();

struct Processor {
    callback: Callback,
}

impl Processor {
    fn set_callback(&mut self, c: Callback) {
        self.callback = c;
    }

    fn process_events(&self) {
        (self.callback)();
    }
}

fn simple_callback() {
    println!("hello world!");
}

fn main() {
    let p = Processor {
        callback: simple_callback,
    };
    p.process_events(); // hello world!
}

此代码可以扩展为包含一个 Option> 来保存用户数据".与功能相关联.即便如此,它也不会是惯用的 Rust.Rust 将数据与函数关联的方法是在匿名闭包中捕获它,就像在现代 C++ 中一样.由于闭包不是 fnset_callback 将需要接受其他类型的函数对象.

This code could be extended to include an Option<Box<Any>> to hold the "user data" associated with the function. Even so, it would not be idiomatic Rust. The Rust way to associate data with a function is to capture it in an anonymous closure, just like in modern C++. Since closures are not fn, set_callback will need to accept other kinds of function objects.

在 Rust 和 C++ 闭包中,具有相同调用签名的闭包大小不同,以适应它们可能捕获的不同值.此外,每个闭包定义都会为闭包的值生成一个唯一的匿名类型.由于这些限制,该结构不能命名其 callback 字段的类型,也不能使用别名.

In both Rust and C++ closures with the same call signature come in different sizes to accommodate the different values they might capture. Additionally, each closure definition generates a unique anonymous type for the closure's value. Due to these constraints, the struct cannot name the type of its callback field, nor can it use an alias.

在结构体字段中嵌入闭包而不引用具体类型的一种方法是使结构体泛型.该结构将自动调整其大小和回调类型,以适应您传递给它的具体函数或闭包:

One way to embed a closure in the struct field without referring to a concrete type is by making the struct generic. The struct will automatically adapt its size and the type of callback for the concrete function or closure you pass to it:

struct Processor<CB>
where
    CB: FnMut(),
{
    callback: CB,
}

impl<CB> Processor<CB>
where
    CB: FnMut(),
{
    fn set_callback(&mut self, c: CB) {
        self.callback = c;
    }

    fn process_events(&mut self) {
        (self.callback)();
    }
}

fn main() {
    let s = "world!".to_string();
    let callback = || println!("hello {}", s);
    let mut p = Processor { callback };
    p.process_events();
}

和以前一样,set_callback() 将接受用 fn 定义的函数,但这个函数也将接受闭包为 ||println!("hello world!"),以及捕获值的闭包,例如 ||println!("{}", somevar).因此,处理器不需要 userdata 伴随回调;set_callback 的调用者提供的闭包将自动从其环境中捕获所需的数据,并在调用时使其可用.

As before, set_callback() will accept functions defined with fn, but this one will also accept closures as || println!("hello world!"), as well as closures that capture values, such as || println!("{}", somevar). Because of this the processor doesn't need userdata to accompany the callback; the closure provided by the caller of set_callback will automatically capture the data it needs from its environment and have it available when invoked.

但是FnMut 有什么关系,为什么不只是Fn?由于闭包保存了捕获的值,因此在调用闭包时必须应用 Rust 的通常更改规则.根据闭包对它们所持有的值的作用,它们被分为三个系列,每个系列都标有一个特征:

But what's the deal with the FnMut, why not just Fn? Since closures hold captured values, Rust's usual mutation rules must apply when calling the closure. Depending on what the closures do with the values they hold, they are grouped in three families, each marked with a trait:

  • Fn 是只读取数据的闭包,可以安全地多次调用,可能来自多个线程.以上两个闭包都是 Fn.
  • FnMut 是修改数据的闭包,例如通过写入捕获的 mut 变量.它们也可能被多次调用,但不能并行调用.(从多个线程调用 FnMut 闭包会导致数据竞争,因此只能通过互斥锁的保护来完成.)闭包对象必须由调用者声明为可变的.
  • FnOnce 是闭包,它们消耗它们捕获的一些数据,例如通过将捕获的值传递给按值获取它的函数.顾名思义,这些只能被调用一次,并且调用者必须拥有它们.
  • Fn are closures that only read data, and may be safely called multiple times, possibly from multiple threads. Both above closures are Fn.
  • FnMut are closures that modify data, e.g. by writing to a captured mut variable. They may also be called multiple times, but not in parallel. (Calling a FnMut closure from multiple threads would lead to a data race, so it can only be done with the protection of a mutex.) The closure object must be declared mutable by the caller.
  • FnOnce are closures that consume some of the data they capture, e.g. by passing a captured value to a function that takes it by value. As the name implies, these may be called only once, and the caller must own them.

有点违反直觉,当为接受闭包的对象类型指定 trait bound 时,FnOnce 实际上是最宽松的.声明泛型回调类型必须满足 FnOnce 特性意味着它将接受任何闭包.但这是有代价的:这意味着持有者只能调用一次.由于 process_events() 可能会选择多次调用回调,并且由于方法本身可能会被多次调用,因此下一个最宽松的界限是 FnMut.请注意,我们必须将 process_events 标记为变异的 self.

Somewhat counter-intuitively, when specifying a trait bound for the type of an object that accepts a closure, FnOnce is actually the most permissive one. Declaring that a generic callback type must satisfy the FnOnce trait means that it will accept literally any closure. But that comes with a price: it means the holder is only allowed to call it once. Since process_events() may opt to invoke the callback multiple times, and as the method itself may be called more than once, the next most permissive bound is FnMut. Note that we had to mark process_events as mutating self.

尽管回调的通用实现非常高效,但它有严重的接口限制.它要求每个 Processor 实例都被参数化为一个具体的回调类型,这意味着单个 Processor 只能处理一个回调类型.鉴于每个闭包都有不同的类型,泛型 Processor 无法处理 proc.set_callback(|| println!("hello")) 后跟 proc.set_callback(|| println!(world")) .扩展结构以支持两个回调字段需要将整个结构参数化为两种类型,随着回调数量的增加,这将很快变得笨拙.如果回调的数量需要是动态的,例如,添加更多类型参数将不起作用.实现一个 add_callback 函数,该函数维护不同回调的向量.

Even though the generic implementation of the callback is extremely efficient, it has serious interface limitations. It requires each Processor instance to be parameterized with a concrete callback type, which means that a single Processor can only deal with a single callback type. Given that each closure has a distinct type, the generic Processor cannot handle proc.set_callback(|| println!("hello")) followed by proc.set_callback(|| println!("world")). Extending the struct to support two callbacks fields would require the whole struct to be parameterized to two types, which would quickly become unwieldy as the number of callbacks grows. Adding more type parameters wouldn't work if the number of callbacks needed to be dynamic, e.g. to implement an add_callback function that maintains a vector of different callbacks.

要移除类型参数,我们可以利用 trait对象,Rust 的特性,允许基于特征自动创建动态接口.这有时被称为类型擦除,是 C++ 中的一种流行技术[1][2],不要与 Java 和 FP 语言对该术语的有些不同的用法混淆.熟悉 C++ 的读者会认识到实现 Fn 的闭包和 Fn trait 对象之间的区别,等同于一般函数对象和 std::function 之间的区别 C++ 中的值.

To remove the type parameter, we can take advantage of trait objects, the feature of Rust that allows automatic creation of dynamic interfaces based on traits. This is sometimes referred to as type erasure and is a popular technique in C++[1][2], not to be confused with Java and FP languages' somewhat different use of the term. Readers familiar with C++ will recognize the distinction between a closure that implements Fn and an Fn trait object as equivalent to the distinction between general function objects and std::function values in C++.

特征对象是通过使用 & 运算符借用对象并将其强制转换为对特定特征的引用而创建的.在这种情况下,由于 Processor 需要拥有回调对象,我们不能使用借用,而必须将回调存储在堆分配的 Box(Rust等效于 std::unique_ptr),在功能上等效于 trait 对象.

A trait object is created by borrowing an object with the & operator and casting or coercing it to a reference to the specific trait. In this case, since Processor needs to own the callback object, we cannot use borrowing, but must store the callback in a heap-allocated Box<dyn Trait> (the Rust equivalent of std::unique_ptr), which is functionally equivalent to a trait object.

如果Processor 存储Box,它不再需要是通用的,而是set_callback 方法现在通过 impl Trait 参数.因此,它可以接受任何类型的可调用对象,包括带有状态的闭包,并在将其存储在 Processor 之前对其进行适当的装箱.set_callback 的通用参数不限制处理器接受哪种回调,因为接受回调的类型与存储在 Processor 结构中的类型分离.

If Processor stores Box<dyn FnMut()>, it no longer needs to be generic, but the set_callback method now accepts a generic c via an impl Trait argument. As such, it can accept any kind of callable, including closures with state, and properly box it before storing it in the Processor. The generic argument to set_callback doesn't limit what kind of callback the processor accepts, as the type of the accepted callback is decoupled from the type stored in the Processor struct.

struct Processor {
    callback: Box<dyn FnMut()>,
}

impl Processor {
    fn set_callback(&mut self, c: impl FnMut() + 'static) {
        self.callback = Box::new(c);
    }

    fn process_events(&mut self) {
        (self.callback)();
    }
}

fn simple_callback() {
    println!("hello");
}

fn main() {
    let mut p = Processor {
        callback: Box::new(simple_callback),
    };
    p.process_events();
    let s = "world!".to_string();
    let callback2 = move || println!("hello {}", s);
    p.set_callback(callback2);
    p.process_events();
}

盒装闭包内引用的生命周期

set_callback 接受的 c 参数类型上的 'static 生命周期绑定是一种简单的方法来说服编译器 c 中包含的 >references 可能是引用其环境的闭包,仅引用全局值,因此在回调的整个使用过程中将保持有效.但是静态边界也非常严厉:虽然它接受拥有对象的闭包很好(我们在上面通过使闭包 move 确保了这一点),但它拒绝引用本地环境的闭包,即使它们只引用比处理器寿命更长并且实际上是安全的值.

Lifetime of references inside boxed closures

The 'static lifetime bound on the type of the c argument accepted by set_callback is a simple way to convince the compiler that references contained in c, which might be a closure that refers to its environment, only refer to global values and will therefore remain valid throughout the use of the callback. But the static bound is also very heavy-handed: while it accepts closures that own objects just fine (which we've ensured above by making the closure move), it rejects closures that refer to local environment, even when they only refer to values that outlive the processor and would in fact be safe.

因为只要处理器还活着,我们就只需要回调活着,所以我们应该尝试将它们的生命周期与处理器的生命周期联系起来,这是一个比 'static 更不严格的限制.但是如果我们只是从 set_callback 中移除 'static 生命周期绑定,它就不再编译了.这是因为set_callback 创建了一个新框并将其分配给定义为Boxcallback 字段.由于定义没有为装箱的 trait 对象指定生命周期,'static 是隐含的,并且赋值将有效地扩大生命周期(从回调的未命名的任意生命周期到 'static),这是不允许的.修复方法是为处理器提供一个明确的生命周期,并将该生命周期与框中的引用和 set_callback 接收到的回调中的引用联系起来:

As we only need the callbacks alive as long as the processor is alive, we should try to tie their lifetime to that of the processor, which is a less strict bound than 'static. But if we just remove the 'static lifetime bound from set_callback, it no longer compiles. This is because set_callback creates a new box and assigns it to the callback field defined as Box<dyn FnMut()>. Since the definition doesn't specify a lifetime for the boxed trait object, 'static is implied, and the assignment would effectively widen the lifetime (from an unnamed arbitrary lifetime of the callback to 'static), which is disallowed. The fix is to provide an explicit lifetime for the processor and tie that lifetime to both the references in the box and the references in the callback received by set_callback:

struct Processor<'a> {
    callback: Box<dyn FnMut() + 'a>,
}

impl<'a> Processor<'a> {
    fn set_callback(&mut self, c: impl FnMut() + 'a) {
        self.callback = Box::new(c);
    }
    // ...
}

随着这些生命周期的明确,不再需要使用'static.闭包现在可以引用本地 s 对象,即不再需要 move,前提是 s 的定义放在p 的定义,以确保字符串比处理器寿命更长.

With these lifetimes being made explicit, it is no longer necessary to use 'static. The closure can now refer to the local s object, i.e. no longer has to be move, provided that the definition of s is placed before the definition of p to ensure that the string outlives the processor.

这篇关于Rust 中的惯用回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆