克隆和移动特征

时间:2015-08-27 19:50:50

标签: multithreading rust clone traits lifetime

我希望能够将实现特征<!doctype html> <html ng-app> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> </head> <body> <div class="wrapper" ng-app="test"> <div class="phone" ng-controller="TestCtrl" ng-init="type = 'search'"> <a href="" ng-click="type = 'search'"> search</a> <a href="" ng-click="type = 'filter'"> filter</a> <div > <input ng-show="type == 'search'" type="text" ng-model="value"> <select class="facet-value" ng-show="type == 'filter'" ng-model="value" ng-options="obj.name for obj in list"> </select> </div> </div> </body> <script type="text/javascript"> var app = angular.module('test', []); function TestCtrl($scope) { $scope.list = [{name : 'one'}, {name : 'two'}]; } </script> </html> 的对象的克隆版本移动到各种线程中。我似乎无法弄清楚这是如何实现的。我已经尝试克隆并移动Foo并将参数指定为泛型,但似乎无法使编译器满意。

我的第一次尝试:

Box<Foo>

导致以下错误:

use std::thread;

pub trait Foo {
    fn bar();
}

pub struct Thing;

impl Thing {
    pub fn something(handler: Box<Foo>) {
        let handler_1 = handler.clone();
        thread::spawn(move || {
            Thing::another_thread(handler_1)
        });
    }

    fn another_thread(handler: Box<Foo>) { }
}

接下来我尝试将其作为通用参数编写......

error: no method named clone found for type Box<Foo + 'static> in the current scope
error: the trait core::marker::Send is not implemented for the type Foo [E0277]

收到以下错误:

use std::thread;

pub trait Foo {
    fn bar();
}

pub struct Thing;

impl Thing {
    pub fn something<T: Foo + Clone + Send>(handler: T) {
        let handler_1 = handler.clone();
        thread::spawn(move || {
            Thing::another_thread(handler_1)
        });
    }

    fn another_thread<T: Foo>(handler: T) { }
}

现在我们已经达到了我失去的地步,我开始将error: the parameter type T may not live long enough [E0310] help: consider adding an explicit lifetime bound T: 'static... 戳入每个希望解决问题的缝隙中。但是,不幸的是,我不知道我试图实现的语法是什么。在此先感谢您的帮助!

0 个答案:

没有答案