通过Vec< Struct>进入新任务 [英] Passing a Vec<Struct> into a new task

查看:88
本文介绍了通过Vec< Struct>进入新任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将自定义结构的Vector传递给在新任务中执行的函数.我已经实现了Clone特质,我认为这是必需的,但是显然,我想要传递的向量需要实现'static+Send才能在闭包的环境中捕获.我不确定要如何满足这些寿命规范?

Im attempting to pass a Vector of a custom struct into a function that is executed in a new task. I've implemented the Clone trait, which I thought was needed for this, but apparently the vector I want to pass in needs to implement 'static+Send in order to be captured in the closure's environment. I'm not exactly sure how to go about satisfying those lifetime specs?

我正在尝试通过此功能启动该过程

I'm attempting to start the process from this function

pub fn start(server: Server, ip: &str, port: u16) {

    // Other things happening

    spawn(proc() {
        event_loop(server.events.clone(), from_conn_pool)
    });
}

fn event_loop(events: Vec<Event>, from_conn_pool: Receiver<Vec<Socket>>) {
    // Wizard magic
}

我收到的错误是:error: cannot capture variable of type rustic_io::server::Server<'_>,它在有界闭包中不满足'static+Send.

The error I receive is: error: cannot capture variable of type rustic_io::server::Server<'_>, which does not fulfill 'static+Send, in a bounded closure

server.rs

pub struct Server<'a> {
    pub sockets: Vec<Socket<'a>>,
    pub events: Vec<Event<'a>>
}

event.rs

pub struct Event<'a> {
    pub name: String,
    pub execute: &'a fn(data: &str, server: super::Server)
}

impl<'a> Event<'a> {
    pub fn new(event: &str, execute: &'a fn(data: &str, server: super::Server)) -> Event<'a> {
        Event {
            name: String::from_str(event),
            execute: execute
        }
    }
}

impl<'a> Clone for Event<'a> {
    fn clone(&self) -> Event<'a> {
        Event {
            name: self.name.clone(),
            execute: self.execute
        }
    }
}

事件循环任务将仅循环各种流,并且如果读取的数据与某个事件名称匹配,它将触发与之关联的功能.从Rust文档中,我可以看到可以通过调用spawn来启动命名函数,如下所示:

The event loop task will just loop over various streams and if data is read that matches a certain event name, it fires off the function associated with it. From the Rust documentation, I can see that you can start a named function off by calling spawn like so:

// Print something profound in a different task using a named function
fn print_message() { println!("I am running in a different task!"); }
spawn(print_message);

我相信这是我应该产生任务的方式,因为它是一个已执行的命名函数.我以为是因为我将其称为spawn(proc(){ ... }),所以它期望所有东西都可以由闭包拥有吗?我尝试将任务生成为spawn(event_loop(arg, arg2)),但是编译器给了我:error: mismatched types: expected proc():Send但找到了()(期望是fn但找到了())

Which, I believe is the way I should be spawning the task, because it is a named function that is getting executed. Im assuming that because I'm calling it as spawn(proc(){ ... }), it is expecting everything going in to be closure-ownable? I've tried spawning the task as spawn(event_loop(arg, arg2)), but the compiler gives me: error: mismatched types: expected proc():Send but found () (expected fn but found ())

在这一点上,我不知道如何将Vec<Event>放入新任务中?

At this point, I've got no idea how to get that Vec<Event> into a new task?

推荐答案

proc()拥有其环境.进入其中的所有内容都必须能够移动到其中,因此必须为'staticSend.

A proc() owns its environment. Anything coming into it must be able to be moved into it and must thus be 'static and Send.

这意味着您编写时

spawn(proc() {
    event_loop(server.events.clone(), from_conn_pool)
});

它正在尝试捕获server(因为server.events.clone()-请注意为此目的如何捕获整个Server;这不太可能是您想要的(可以在proc之前执行let events = server.events.clone();之类的操作然后使用events,或者您可以只承认clone()调用是多余的)和from_conn_pool.您遇到的问题是server:它不一定是'static,因为您没有指定任何类型的生存期,因此可以推断为任意生存期;也就是说,它可能包含非静态生存期事件,显然该事件不能转移到新任务.对于初学者,您需要Server<'static>.

it is trying to capture server (because of server.events.clone()—note how it’s capturing the entire Server for this purpose; it’s unlikely to be what you wanted (you can do something like let events = server.events.clone(); before the proc and then use events, or you can just acknowledge that the clone() call is superfluous anyway) and from_conn_pool. Your problem is with server: it is not necessarily 'static, because you didn’t specify any lifetime for the type and so it is inferred as an arbitrary lifetime; that is, it may contain an event of non-static lifetime, which evidently can’t be moved to a new task. You would for starters need Server<'static>.

但是问题随之而来:为什么要使用&'a fn(…)而不是仅仅使用fn(…)作为类型?如果您将整个参考资料放在此处,您的问题将消失.

But the question then rises: why are you using &'a fn(…) rather than just fn(…) as your type? If you drop the whole reference thing there, your problem will go away.

这篇关于通过Vec&lt; Struct&gt;进入新任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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