Il codice simile a questo:È possibile utilizzare std :: rc :: Rc con un tipo di tratto?
// Simplified
pub trait Field: Send + Sync + Clone {
fn name(&self);
}
#[deriving(Clone)]
pub enum Select {
SelectOnly(Vec<Rc<Field>>),
SelectAll
}
L'errore è:
the trait `core::kinds::Sized` is not implemented for the type `Field+'static`
C'è un altro modo per avere il vettore con riferimento contati immutabili oggetti di tipo caratteristica?
Suppongo che posso riscrivere il codice come questo:
#[deriving(Clone)]
pub enum Select {
SelectOnly(Vec<Rc<Box<Field>>>),
SelectAll
}
E 'la strada giusta?
Se si sostituisce 'Rc' con' RefCell', non viene compilato. Ma 'RefCell' prende 'T:? Sized' (nella sua definizione, proprio come' Rc') ora. Cosa mi manca? –
John
Pensavo che avrei risposto io stesso - 'RefCell' è un tipo valido, ma è anche unsized. 'RefCell' non implementa' CoerceUnsized' (vedere [RFC-0982] (https://github.com/rust-lang/rfcs/blob/master/text/0982-dst-coercion.md)) –
John
rustc 1.20. 0-nightly (622e7e648 2017-06-21) errore [E0038]: il carattere 'Campo' non può essere trasformato in un oggetto ->: 9: 16 | 9 | SelectOnly (Rc ), | ^^^^^^^^^^ Il carattere 'Field' non può essere trasformato in un oggetto | = nota: il tratto non può richiedere quell'effetto 'Sé: dimensionato ' errore: interruzione a causa di errori precedenti –
Yash