Questions tagged [trait-objects]

For questions related to Trait Objects (dynamically-dispatched objects) in Rust

Trait objects are the dynamically dispatched objects in Rust. They are unsized, so they are used behind some kind of reference (Box, for example).

Links:

183 questions
3
votes
1 answer

How to pass an Arc clone to a closure?

I'm trying to write a closure that uses an Arc by cloning it. Ideally I'd like to have the clone inside the closure, but I'm kinda forced to pass the original Arc, which might be the reason I'm getting the error: use std::sync::Arc; use…
Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
3
votes
2 answers

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

use std::sync::Arc; trait Trait {} struct TraitImpl {} impl Trait for TraitImpl {} fn main() { let value = TraitImpl {}; let _: Arc = Arc::new(value); // compiles let _: Arc = value.into(); // doesn't…
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
3
votes
1 answer

How to pass Rc> to fn that wants &dyn T?

I have trouble passing an argument to a fn. trait T {} struct S { others: Vec>> } impl S { fn bar(&self) { for o in self.others { foo(&o.borrow()); } } } fn foo(t: &dyn T) {} The compiler…
David
  • 1,672
  • 2
  • 13
  • 32
3
votes
1 answer

How to convert trait to concrete type?

I have a trait object, and I want to know the concrete object that it points to, but I cannot work out how to get the concrete object. What I want is something like the following: trait MyClonable { /** copy from another MyClonable */ fn…
Martin Ellison
  • 1,043
  • 1
  • 13
  • 25
3
votes
1 answer

Is there a way to store a random number generator as a trait object?

Is there a way to hold a generic random number generator in Rust? I'd like a way to write a generic piece of code such as: use rand::Rng; // 0.7.2 fn main() { // Purely random numbers on the interval 0 to 1 println!("Pure random"); let…
wyer33
  • 6,060
  • 4
  • 23
  • 53
3
votes
2 answers

How do I clone a Rc trait object and cast it to another trait object?

This is a follow up question from Rust dynamic cast trait object between different taits. The solution provided there works really well when we use references for trait objects, but this time I am trying to do the same with Rc pointers. For…
dospro
  • 450
  • 1
  • 5
  • 17
3
votes
1 answer

How to share heap-allocated trait objects?

I have a trait and a struct implementing that trait (a trait object). I'd like to allocate my trait objects on the heap and to have other structures refer to them. Box field trait Material {} struct Iron {} impl Material for Iron {} // It works,…
dying_sphynx
  • 1,136
  • 8
  • 17
3
votes
2 answers

Vec of Generics of Different Concrete Types

I have a trait Foo, and concrete types A and B are both bounded by the trait Foo. I want to return a Vec, where Foo could be either concrete type A or B, like shown below: trait Foo { } pub struct A {} pub struct B {} impl Foo for A {} impl…
Andrew Pham
  • 31
  • 1
  • 2
3
votes
1 answer

Match arms that return iterators?

I've got some code that attempts to run a match where each branch can return a different type, but all of these types implement Iterator. let found: Iterator = match requirements { Requirements::A => MatchingAs {…
Bosh
  • 8,138
  • 11
  • 51
  • 77
3
votes
2 answers

What does `impl TraitX for TraitY` mean in Rust?

For example: trait TraitX { } trait TraitY { } impl TraitX for TraitY { } I figured it would mean the same as impl TraitX for A { } but the error message suggests otherwise: $ rustc --version rustc 0.12.0-nightly (a70a0374e 2014-10-01…
Snowball
  • 11,102
  • 3
  • 34
  • 51
2
votes
1 answer

How to unit test two implementations of a trait?

I have two structs implementing the same trait using a different algorithm. I want to write the unit tests once and run them against both structs. What is the best way to do this? My concern is to test both implementations as black box and avoid…
noam cohen
  • 87
  • 1
  • 6
2
votes
1 answer

The lifetime of trait object pointer

I encountered a compile error related to lifetime in my Rust code. Here is the code causing the error: fn to_pointer_vec(ptrs: &[*const dyn ToString]) -> Vec<*const dyn ToString> { let mut vec: Vec<*const dyn ToString> = Vec::new(); for &ptr…
2
votes
0 answers

Rust trait objects: specific use case of variance and/or coercion

Consider the following (playground) trait Module {} struct Foo { module_box: Box, module_rc: Rc, } impl Foo { fn mut_box<'s>(&'s mut self) -> &'s mut (dyn Module + 's) { // this…
plafer
  • 185
  • 12
2
votes
1 answer

Error E0277 following book example dyn Trait, how to push a dyn trait in vector?

My real case is similar to the Rust doc about dyn trait with Screen and Draw trait. So I built an example totally similar to the book. But instead of initializing the vector in place, I need to have a register function to push components into the…
Zhou Lebo
  • 31
  • 2
2
votes
1 answer

What exactly is the requirement for "covering" a type & why does a single element tuple satisfy it?

Assuming the following code is present use core::any::Any; enum Value { Any(Box), Other, // placeholder, this code is adapted from mine } This code raises a diagnostic that I can't quite understand impl TryFrom for…