trait-objects

What is the cited problem with using generic type parameters in trait objects?

岁酱吖の 提交于 2019-12-11 02:18:46
问题 I am reading Object Safety Is Required for Trait Objects and I don't understand the problem with generic type parameters. The same is true of generic type parameters that are filled in with concrete type parameters when the trait is used: the concrete types become part of the type that implements the trait. When the type is forgotten through the use of a trait object, there is no way to know what types to fill in the generic type parameters with. I am trying to code an example but I can't

How do you create a Box<T> when T is a trait-object?

霸气de小男生 提交于 2019-12-01 09:08:29
I have the following code extern crate rand; use rand::Rng; pub struct Randomizer { rand: Box<Rng>, } impl Randomizer { fn new() -> Self { let mut r = Box::new(rand::thread_rng()); // works let mut cr = Randomizer { rand: r }; cr } fn with_rng(rng: &Rng) -> Self { let mut r = Box::new(*rng); // doesn't work let mut cr = Randomizer { rand: r }; cr } } fn main() {} It complains that error[E0277]: the trait bound `rand::Rng: std::marker::Sized` is not satisfied --> src/main.rs:16:21 | 16 | let mut r = Box::new(*rng); | ^^^^^^^^ `rand::Rng` does not have a constant size known at compile-time | =

How do you create a Box<T> when T is a trait-object?

家住魔仙堡 提交于 2019-12-01 06:13:33
问题 I have the following code extern crate rand; use rand::Rng; pub struct Randomizer { rand: Box<Rng>, } impl Randomizer { fn new() -> Self { let mut r = Box::new(rand::thread_rng()); // works let mut cr = Randomizer { rand: r }; cr } fn with_rng(rng: &Rng) -> Self { let mut r = Box::new(*rng); // doesn't work let mut cr = Randomizer { rand: r }; cr } } fn main() {} It complains that error[E0277]: the trait bound `rand::Rng: std::marker::Sized` is not satisfied --> src/main.rs:16:21 | 16 | let

Understanding Traits and Object Safety

雨燕双飞 提交于 2019-11-28 14:00:37
I am struggling with the basics of object safety. If I have this code struct S { x: i32 } trait Trait: Sized { fn f(&self) -> i32 where Self: Sized; } fn object_safety_dynamic(x: Trait) {} I receive fn object_safety_dynamic(x: Trait) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `traits::Trait` cannot be made into an object = note: the trait cannot require that `Self : Sized` When adding / changing :Sized as the trait's inheritance or f 's bound I receive slightly different error messages. Could someone explain: Why does this particular example not work? The chapter Trait Objects states "So

What is the difference between <T: Trait> Box<T> and &Trait / Box<Trait>?

送分小仙女□ 提交于 2019-11-28 07:18:00
问题 When writing code with traits you can put the trait in a trait bound: use std::fmt::Debug; fn myfunction1<T: Debug>(v: Box<T>) { println!("{:?}", v); } fn myfunction2<T: Debug>(v: &T) { println!("{:?}", v); } fn main() { myfunction1(Box::new(5)); myfunction2(&5); } Or directly in a Box or reference type: use std::fmt::Debug; fn myfunction3(v: Box<Debug>) { println!("{:?}", v); } fn myfunction4(v: &Debug) { println!("{:?}", v); } fn main() { myfunction3(Box::new(5)); myfunction4(&5); } These

Understanding Traits and Object Safety

微笑、不失礼 提交于 2019-11-27 08:06:24
问题 I am struggling with the basics of object safety. If I have this code struct S { x: i32 } trait Trait: Sized { fn f(&self) -> i32 where Self: Sized; } fn object_safety_dynamic(x: Trait) {} I receive fn object_safety_dynamic(x: Trait) { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `traits::Trait` cannot be made into an object = note: the trait cannot require that `Self : Sized` When adding / changing :Sized as the trait's inheritance or f 's bound I receive slightly different error messages.

The trait cannot be made into an object

纵饮孤独 提交于 2019-11-26 07:47:13
问题 I have the following code: extern crate futures; // 0.1.24 use futures::Future; use std::io; struct Context; pub trait MyTrait { fn receive(context: Context) -> Future<Item = (), Error = io::Error>; } pub struct MyStruct { my_trait: MyTrait, } When I try to compile it I get the error message: error[E0038]: the trait `MyTrait` cannot be made into an object --> src/lib.rs:13:5 | 13 | my_trait: MyTrait, | ^^^^^^^^^^^^^^^^^ the trait `MyTrait` cannot be made into an object | = note: method

Sending trait objects between threads in Rust

感情迁移 提交于 2019-11-26 07:45:59
问题 I\'d like to send a trait object between threads, but can\'t figure out if it\'s possible. It seems like it might not be, as they apparently do not fulfill the Send trait. The following code demonstrates what I\'m trying to do: use std::{ sync::mpsc::{channel, Receiver, Sender}, thread, }; trait Bar { fn bar(&self); } struct Foo { foo: i32, } impl Bar for Foo { fn bar(&self) { println!(\"foo: {}\", self.foo); } } fn main() { let foo = Box::new(Foo { foo: 1 }) as Box<dyn Bar>; let (tx, rx):