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 iterate over tuple of trait objects or unsized types

This question was originally found in this post in reddit. Whilst experienced Rust user will spot out that the elements in a tuple does not have to be the same (if they are, you should use array!), and so it does not make sense to iterate through…
Earth Engine
  • 10,048
  • 5
  • 48
  • 78
2
votes
1 answer

Factorize methods taking &Box and &T as argument

I have a method that I need to call with a trait parameter (let's call it Listener). The reason is that sometimes I have previously stored this trait parameter into a parent structure so it is inside a Box, and sometimes not. So I have the two…
jolivier
  • 7,380
  • 3
  • 29
  • 47
2
votes
2 answers

"Expected trait A, found &A" when trying to box a trait object

I'm trying to make a trait that can either retrieve (and return a reference to) a trait object of another trait, or create one (and return a boxed version of it), leaving the choice to the implementor (which means I need to restrict the returned…
Michail
  • 1,843
  • 1
  • 17
  • 21
2
votes
1 answer

Is it possible to auto implement a trait which casts a trait object to another trait object?

I have a trait which manages conversions to different trait objects. The trait looks like this: (Boo and Gee are both different Traits) trait Foo { fn as_boo(&mut self) -> Option<&mut Boo> { None } fn as_gee(&mut self) ->…
lncr
  • 826
  • 8
  • 16
2
votes
2 answers

Rc to Option?

I'm trying to implement a method that looks like: fn concretify(rc: Rc) -> Option { Rc::try_unwrap(rc).ok().and_then(|trait_object| { let b: Box = unimplemented!(); b.downcast().ok().map(|b| *b) …
Nathan Ringo
  • 973
  • 2
  • 10
  • 30
2
votes
2 answers

Variable parameterised over a trait not a struct?

I'm trying to wrap my head around Rust's generics. I'm writing something to extract HTML from different web sites. What I want is something like this: trait CanGetTitle { fn get_title(&self) -> String; } struct Spider { pub…
jbrown
  • 7,518
  • 16
  • 69
  • 117
2
votes
2 answers

How to get the v-ptr for a given Trait/Struct combination?

In Rust, a &T where T is a trait is a fat reference, which actually corresponds to raw::TraitObject: pub struct TraitObject { pub data: *mut (), pub vtable: *mut (), } Using TraitObject, one can de-construct and re-construct a &T at…
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
1
vote
1 answer

Store function returning impl Trait as trait object

As per the below example, I am trying to store functions as trait objects. I have worked out how to do this for functions which return a concrete type, by using as to cast from a function item to a function pointer. However, I would also like to…
Max888
  • 3,089
  • 24
  • 55
1
vote
2 answers

How to cast Vec> to Vec> in rust?

I'm failing to cast dyn SomeTrait + Send + Sync to dyn SomeTrait + Sync. More specifically, I am trying to cast &Vec> to &Vec>. I tried simply reassining as suggested here as well as using…
Einliterflasche
  • 473
  • 6
  • 18
1
vote
1 answer

Why can't I clone a `Vec` of cloneable constructors?

I have a structure wrapping a Vec of Box constructors which I've asserted to be clone. I have a trait Ctor which takes the clone logic and produces another Box. It compiles fine if I don't implement the trait or if I don't call…
dspyz
  • 5,280
  • 2
  • 25
  • 63
1
vote
0 answers

Not understanding type error when using trait objects in Rust

I have the following struct defined in my project: pub struct HitableList <'a> { pub list : Vec> } impl <'a> HitableList <'a> { pub fn new (l : &Vec>) -> HitableList<'a> { HitableList…
1
vote
1 answer

Can I have a collection over trait objects where their associated types implement a shared trait?

I know that generally you can't have something like Vec> because of course you need to specify the associated type. Now, I can have Vec>> of course. But what about something like this: trait Foo…
cadolphs
  • 9,014
  • 1
  • 24
  • 41
1
vote
1 answer

How do I approach this trait object problem?

I have code similar to the one below (playground): trait SomeTrait { fn some_fn(&self) -> i32; } #[derive(Debug, Clone, Eq, PartialEq)] enum Foo { Bar, Baz(Box), } fn main() { // Do something with Foo } I have a…
Pro Poop
  • 357
  • 5
  • 14
1
vote
1 answer

Trait object cannot be shared between threads safely

When I am writing a zookeeper client in RUST. code like this : pub trait ZookeeperHandler { fn on_child_change(&self, path : &str, value : &String); fn on_valude_change(&self, path : &str, value: String); fn reload_all_child(&self, path…
dongwei
  • 23
  • 2
1
vote
1 answer

Why can't we make a trait with non-method associated functions into a trait object?

According to the explanation, Rust rejects to make a trait with non-method associated functions into a trait object. Methods that do not take a self parameter can’t be called since there won’t be a way to get a pointer to the method table for…
Tim
  • 99
  • 1
  • 5