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
2
votes
1 answer

How to make type-erased version of a trait with associated type?

Say there is a collection trait that has an associated type for its items: trait CollectionItem { // ... } trait Collection { type Item: CollectionItem; fn get(&self, index: usize) -> Self::Item; // ... } Can I somehow…
Heinzi
  • 5,793
  • 4
  • 40
  • 69
2
votes
1 answer

How can I avoid consuming a value passed to an `impl Trait` parameter?

How can I keep ownership of a value after passing it to a function that takes a impl Trait as a parameter? I've tried both passing the parameter as a reference and without the & but none of them worked. trait Noise{ fn make_noise(&self); fn…
EscFox
  • 23
  • 3
2
votes
0 answers

Problem with trait object storing its reference elsewhere

I want to have a method to_holder of SomeTrait that'll store reference to the trait object to a struct, but the compiler forces Self of method to_holder to be Sized. Annotating Self: Sized does work, but that way I can't call to_holder from a trait…
danzou1ge6
  • 53
  • 2
2
votes
1 answer

rust generic type with trait bound when use trait objects

trait MyTrait { fn my_f(&self) ; } struct A {} struct B {} impl MyTrait for A { fn my_f(&self) { println!("A.my_f()"); } } impl MyTrait for B { fn my_f(&self) { println!("B.my_f()"); } } struct MyList
Matt.Z
  • 602
  • 7
  • 19
2
votes
2 answers

Why can't Box be pased to a function with &mut Trait as parameter

I'm sure this has been asked previously but haven't encountered a question that captures the precise scenario here. I have the following code: let mut pool: Box = <...> redis::cmd(COMMAND) .arg(LIST) …
rumdrums
  • 1,322
  • 2
  • 11
  • 25
2
votes
1 answer

Is it possible (in any way) to pass a trait object to a generic method?

I have a boxed trait object; I was wondering if, in any way, this can be passed to a method with a generic type bound: trait Trait { fn tmethod(&self) { println!("hello"); } } impl Trait for Vec {} fn call_tmethod(t:…
Marcus
  • 5,104
  • 2
  • 28
  • 24
2
votes
1 answer

Can Clone be implemented for a trait object with finite lifetime (without using unsafe code)?

First things first: What am I trying to achieve? I'm trying to use Iterator::fold() with an accumulator that itself is an Iterator, but in addition I need a clone of that iterator internally. fn whatever(some_iterator : T,…
soulsource
  • 197
  • 7
2
votes
1 answer

Incorrect type inference for Rust vector of trait object

I am having trouble understanding the following error: // test.rs struct A { a: u32 } trait B { } impl B for A { } struct C { c: Vec>, } fn test() { let a = A {a: 1}; let a_vec = vec![Box::new(a)]; let c = C { …
Jason Qiu
  • 23
  • 3
2
votes
1 answer

Using generics, trait aliases, and constructors in Rust

Context I have a DataStore trait that abstracts out data storage. (For example, I can create a simple implementation of this trait for data stores that wrap Vecs and HashMaps.) I would like this abstraction because some use cases/targets…
2
votes
2 answers

How to create Vec of references to generic trait objects in Rust?

I am new to Rust. My background is Java. I am trying to solve the following problem. I have a trait Fuel which is implemented for struct Diesel and struct Gas. I also have a trait Vehicle with Fuel generic type parameter. Vehicle is implemented for…
Vadim
  • 576
  • 1
  • 4
  • 13
2
votes
2 answers

How can I store a closure object in a struct?

I can't figure out how to store a closure object in a struct. The arguments and return for the closure object are known. Here's my condensed code: struct Instr where F: Fn([i32;4],[i32;3]) -> [i32;4] { name: String, op: F } fn…
Tumbleweed53
  • 1,491
  • 7
  • 13
2
votes
1 answer

How to convert an `Arc` into an `Arc`?

I have a library that needs things to implement a specific trait (TQDispatch). In my main project I have a vector of objects that all implement a different trait (Device) which I need for dynamic dispatch. The device trait is declared pub trait…
pm100
  • 48,078
  • 23
  • 82
  • 145
2
votes
1 answer

How do I pass in a trait object to a vector that will also have a uniform type in the vector of the vector?

This is really hard to explain concisely. But what I want is a struct that has a field of Vec "A" containing a vector of threads with another Vec "B" inside the Vec "A". Vec "A" holds both the Vec "B" and thread handle. Vec "B" has a uniform type,…
entropy32
  • 169
  • 3
  • 11
2
votes
2 answers

Cast Arc> to Arc

I am writing a graph implementation with edges and nodes. The graph should be accessed concurrently so I chose to build the Edges and Nodes as Arc> and Arc>. Unfortunately I get a compile error the parameter type 'T'…
CoronA
  • 7,717
  • 2
  • 26
  • 53
2
votes
2 answers

How to pass a closure via dynamic dispatch in an object-safe method?

How do you pass a closure to an object-safe trait method or otherwise via dynamic dispatch? I have answered this myself, but the answer leaves something wanting: FnOnce closures must be boxed since they are not sized and must be consumed on use…
dhardy
  • 11,175
  • 7
  • 38
  • 46