可以使用特征类型的std :: rc :: Rc吗?

时间:2014-10-13 07:33:59

标签: rust

代码如下所示:

// Simplified
pub trait Field: Send + Sync + Clone {
    fn name(&self);
}

#[deriving(Clone)]
pub enum Select {
    SelectOnly(Vec<Rc<Field>>),
    SelectAll
}

错误是:

the trait `core::kinds::Sized` is not implemented for the type `Field+'static`

有没有其他方法可以使用具有引用计数不可变特征类型对象的向量?

我想我可以像这样重写代码:

#[deriving(Clone)]
pub enum Select {
    SelectOnly(Vec<Rc<Box<Field>>>),
    SelectAll
}

这是正确的方法吗?

3 个答案:

答案 0 :(得分:3)

我相信它应该可以用DST,但Rust还没有。 DST的主要动机正是希望将trait对象与任何类型的智能指针一起使用。据我所知,这应该可以通过1.0发布。

作为临时解决方法,确实可以使用Rc<Box<T>>,尽管这种双重间接是不幸的。

答案 1 :(得分:3)

It is possible to create an trait object with an x = promoter[promoter['AssociatedGeneName'].str.contains(data['AssociatedGeneName'])] Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> x = promoter[promoter['AssociatedGeneName'].str.contains(data['Associated Gene Name'])] File "C:\Python34\lib\site-packages\pandas\core\strings.py", line 1226, in contains na=na, regex=regex) File "C:\Python34\lib\site-packages\pandas\core\strings.py", line 203, in str_contains regex = re.compile(pat, flags=flags) File "C:\Python34\lib\re.py", line 219, in compile return _compile(pattern, flags) File "C:\Python34\lib\re.py", line 278, in _compile return _cache[type(pattern), pattern, flags] File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 663, in __hash__ ' hashed'.format(self.__class__.__name__)) TypeError: 'Series' objects are mutable, thus they cannot be hashed as of Rust 1.1. This compiles:

Rc

Note that your original example used use std::rc::Rc; trait Field: Send + Sync { fn name(&self); } enum Select { Only(Vec<Rc<Field>>), All, } // --- struct Example; impl Field for Example { fn name(&self) {} } fn main() { let fields: Vec<Rc<Field>> = vec![Rc::new(Example)]; Select::Only(fields); } , but you cannot make such a trait into a trait object because it is not object safe. I've removed it to answer the question.

I also removed the redundancy of the enum variant names.

答案 2 :(得分:1)

可以在#18248#16918之后。

相关问题