Can I use std :: rc :: Rc with a feature type?

The code is as follows:

// Simplified pub trait Field: Send + Sync + Clone { fn name(&self); } #[deriving(Clone)] pub enum Select { SelectOnly(Vec<Rc<Field>>), SelectAll } 

Error:

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

Is there any other way to have a vector with reference immutable objects of type attributes?

I believe that I can rewrite the code as follows:

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

Is it correct?

+5
source share
3 answers

I believe this is possible with DST, but Rust does not exist yet. The main motivation for DST was precisely the desire to use feature objects with any smart pointer. As far as I know, this should be possible in version 1.0.

As a temporary workaround, you can use Rc<Box<T>> , although this kind of double indirectness is unsuccessful.

+3
source

You can create a feature object with Rc from Rust 1.1. This compiles:

 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); } 

Please note that in your original example Clone used, but you cannot make such an attribute into an object-object, because it is not protected by objects , I deleted it to answer the question.

I also removed the redundancy of enumeration option names.

+3
source

This is possible after # 18248 and # 16918 .

+1
source

Source: https://habr.com/ru/post/1204543/


All Articles