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
1
vote
0 answers

How do I use a trait object in a BinaryHeap?

I have the following simplified code: use std::collections::BinaryHeap; use std::rc::Rc; struct JobId; struct Coord; struct TimeStep; pub trait HasJob { fn id(&self) -> JobId; fn start(&self) -> Coord; fn end(&self) -> Coord; fn…
shadeMe
  • 706
  • 1
  • 10
  • 30
1
vote
1 answer

Create Read trait object from u8 slice

I'm trying to create a Read trait object from a u8 slice, to use in murmur3 crate, like this fn main() { let mut arr: [u8; 4] = [1, 2, 3, 4]; let mut slice: &mut [u8] = &mut arr; let mut read: &mut std::io::Read = &mut slice; } But I…
Mikhail Cheshkov
  • 226
  • 2
  • 14
1
vote
0 answers

How can I cast Rc to Rc>>

I have a trait MyTrait and a struct MyStruct that implements MyTrait. I also have a function that accepts Rc>> as an argument. Somewhere in the code I create an instance of Rc>>: let my_struct =…
Dmitry Uvarov
  • 673
  • 6
  • 6
0
votes
1 answer

Problem with returning generic type from function

I have a function that searches through a list of trait objects and attempts to find one whose implementor is of a specific type, however the compiler doesent accept the return type. pub struct GameObject{ pub modules: Vec>,…
Isaksak
  • 3
  • 1
0
votes
1 answer

Blanket `impl Trait1` for all types which `impl Trait2` and functions which return `impl Trait2`

I want to implement HasChildren for all types which implement Element and for all functions which return impl Element. My case use is that I have a Vec> in which I want to be able to store both types which implement Element and…
Max888
  • 3,089
  • 24
  • 55
0
votes
2 answers

Rust impl multiple times both for struct and traits

Think below code, impl multiple times both for struct and traits: mod m { pub trait Foo { fn xyzzy(&self) { println!("foo!"); } } pub trait Bar { fn xyzzy(&self) { println!("bar!"); } } pub trait Quux:…
James Yang
  • 476
  • 5
  • 6
0
votes
1 answer

Getting a slice of trait objects from a slice of an associated type

I have two related traits List and ListItem (playground link). trait List { type Item: ListItem; fn items(&self) -> Vec; /* more stuff */ } where Item is an associated type on List and ListItem has a custom implementation…
Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
0
votes
1 answer

Is there a way to implement trait objects with generic functions?

Basically I am trying to implement visitors-coding paradigm, where Expr trait needs to be implemented by Binary struct. I want to use Expr as a trait object. Any entity wanting to interact with Expr will need to implement Visitors trait. The visitor…
Adnan
  • 88
  • 1
  • 7
0
votes
1 answer

Storing an iterator over borrowed refs inside a Struct

I'm new to Rust and like many finding lifetimes quite hard to get the hang of. I get the basics, but I can't quite fathom how to make this work. I'm trying to implement an abstract data structure that holds a collection of records. One requirement…
d11wtq
  • 34,788
  • 19
  • 120
  • 195
0
votes
1 answer

What happens when I pass a concrete struct reference to a function that takes trait objects?

In the following code I have a simple trait, A, and a struct Foo that implements A... Next, I define a function that takes a reference to a trait object. From main() I pass in a reference to a concrete Foo. This works fine and successfully calls the…
Chuck
  • 1,850
  • 2
  • 17
  • 28
0
votes
1 answer

How can I avoid allocation when filtering on a set of items with a higher order function?

Trying to filter a set of items, based on a predicate that is generic, because computed at run-time: fn main () { let el = vec![ vec![10, 20, 30], vec![40, 50, 60] ]; println!("{:?}", use_predicate(el,…
doplumi
  • 2,938
  • 4
  • 29
  • 45
0
votes
1 answer

Trait objects force higher-ranked trait bounds, which break nested closures

I am in a situation where Rust makes me add a HRTB for a generic type that is used as argument in a trait object. But this HRTB makes nested closures not work. Here's the trait I'm going to use to make a trait object Box: trait OpTrait…
Kurt Schelfthout
  • 8,880
  • 1
  • 30
  • 48
0
votes
2 answers

Trait object as associated type of a trait object

I wrote two structs that implement a common trait Solve. Solve has an associated type Answer with trait bound Display. I want the function create_solver to return a trait object of Solve. I need help with writing the associate type Answer = ??. use…
0
votes
2 answers

Clone custom structs of concrete type as trait objects

Using Rc, I can cast an Rc of a concrete type to a trait object: use std::rc::Rc; trait Foo {} impl Foo for usize {} fn main() { let x: Rc = Rc::new(1); let y: Rc = x.clone(); } Playground. If I define a wrapper for an Rc…
Román Cárdenas
  • 492
  • 5
  • 15
0
votes
0 answers

Filter vec of trait objects by other trait and return all trait objects that implement given trait

I'm trying to filter a vec of trait objects by a trait and return a subset of the vec which contains all objects that implement given trait, similar to Bevys Query<>. I came up with this: pub fn filter<'a, T: Entity + Sized>(vector: &'a mut…
Joffi
  • 16
  • 2