trait-objects

How to coerce a Vec of structs to a Vec of trait objects?

五迷三道 提交于 2021-02-05 07:21:29
问题 Trying to create a DB struct that is a HashMap of vectors. Each Vec contains Box<dyn Model> . use std::collections::HashMap; trait Model { fn id(&self) -> i32; } struct User; struct Message; impl Model for User { fn id(&self) -> i32 { 4 } } impl Model for Message { fn id(&self) -> i32 { 3 } } struct DB { users: Vec<Box<User>>, messages: Vec<Box<Message>>, tables: HashMap<String, Vec<Box<dyn Model>>>, } impl DB { fn new() -> Self { let users: Vec<Box<User>> = Vec::new(); let messages: Vec<Box

How to coerce a Vec of structs to a Vec of trait objects?

元气小坏坏 提交于 2021-02-05 07:21:06
问题 Trying to create a DB struct that is a HashMap of vectors. Each Vec contains Box<dyn Model> . use std::collections::HashMap; trait Model { fn id(&self) -> i32; } struct User; struct Message; impl Model for User { fn id(&self) -> i32 { 4 } } impl Model for Message { fn id(&self) -> i32 { 3 } } struct DB { users: Vec<Box<User>>, messages: Vec<Box<Message>>, tables: HashMap<String, Vec<Box<dyn Model>>>, } impl DB { fn new() -> Self { let users: Vec<Box<User>> = Vec::new(); let messages: Vec<Box

Why can't I create a trait object with let _: Arc<dyn Trait> = value.into()?

試著忘記壹切 提交于 2020-06-16 02:49:24
问题 use std::sync::Arc; trait Trait {} struct TraitImpl {} impl Trait for TraitImpl {} fn main() { let value = TraitImpl {}; let _: Arc<dyn Trait> = Arc::new(value); // compiles let _: Arc<dyn Trait> = value.into(); // doesn't compile } Result: error[E0277]: the trait bound `std::sync::Arc<dyn Trait>: std::convert::From<TraitImpl>` is not satisfied --> src/main.rs:10:35 | 10 | let _: Arc<dyn Trait> = value.into(); // doesn't compile | ^^^^ the trait `std::convert::From<TraitImpl>` is not

Use trait object to pass str in rust

与世无争的帅哥 提交于 2019-12-24 00:59:26
问题 I'm reading a rust book and is confused by one example here. use std::fmt::Display; fn main() { test("hello"); test2("hello") } fn test(s: &dyn Display) { println!("{}", s); } fn test2(s: &str) { println!("{}", s); } Here passing &'static str as a trait object failed. It complains that "hello" does not have a fixed size at compile time while it is a pointer type. Yet the second call works: why? 回答1: &dyn Display expects a reference. You feed it a &str . So the compiler legitimately thinks

Why can't `&(?Sized + Trait)` be casted to `&dyn Trait`?

半腔热情 提交于 2019-12-23 11:34:13
问题 In the code below it is not possible to obtain a reference to a trait object from a reference to a dynamically-sized type implementing the same trait. Why is this the case? What exactly is the difference between &dyn Trait and &(?Sized + Trait) if I can use both to call trait methods? A type implementing FooTraitContainerTrait might e.g. have type Contained = dyn FooTrait or type Contained = T where T is a concrete type that implements FooTrait . In both cases it's trivial to obtain a &dyn

Why can't `&(?Sized + Trait)` be casted to `&dyn Trait`?

梦想的初衷 提交于 2019-12-23 11:33:23
问题 In the code below it is not possible to obtain a reference to a trait object from a reference to a dynamically-sized type implementing the same trait. Why is this the case? What exactly is the difference between &dyn Trait and &(?Sized + Trait) if I can use both to call trait methods? A type implementing FooTraitContainerTrait might e.g. have type Contained = dyn FooTrait or type Contained = T where T is a concrete type that implements FooTrait . In both cases it's trivial to obtain a &dyn

How do I pass Rc<RefCell<Box<MyStruct>>> to a function accepting Rc<RefCell<Box<dyn MyTrait>>>?

梦想的初衷 提交于 2019-12-12 11:26:08
问题 I have originally asked this question here, but it was marked as duplicate, although it duplicates only a part of it in my opinion, so I have created a more specific one: Consider the following code: use std::rc::Rc; trait MyTrait { fn trait_func(&self); } struct MyStruct1; impl MyStruct1 { fn my_fn(&self) { // do something } } impl MyTrait for MyStruct1 { fn trait_func(&self) { // do something } } fn my_trait_fn(t: Rc<dyn MyTrait>) { t.trait_func(); } fn main() { let my_str: Rc<MyStruct1> =

Default trait method implementation for all trait objects

不羁的心 提交于 2019-12-11 08:28:38
问题 I have a trait MyTrait , and I want all trait objects &MyTrait to be comparable to each other and to nothing else. I have that now based on How to test for equality between trait objects?. The problem is that I need to use MyTraitComparable everywhere, instead of MyTrait . Is there a way to get around this? use std::any::Any; trait MyTrait {} trait MyTraitComparable: MyTrait { fn as_any(&self) -> &Any; fn equals(&self, other: &MyTraitComparable) -> bool; } impl<S: 'static + MyTrait +