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
2 answers

How can a trait object take a trait with generic methods as an argument?

So trait objects can't have methods with generics - that looks fine. But in this language the only ways to use abstraction mechanism are available through generics and trait objects. Which means that for each trait I have to decide beforehand if it…
Vlad
  • 3,001
  • 1
  • 22
  • 52
1
vote
0 answers

Create a conditional view or proxy over data returned by a function

I have a JSON object which represents some loose configuration. I'd like to provide functionality over "views" of the data based on some runtime conditions rather than working with the data directly. In some cases, I still need to work with the…
Avba
  • 14,822
  • 20
  • 92
  • 192
1
vote
0 answers

trait cannot be made into an object due to generic type parameters

Throughout my code I have objects which implement Trait1. I also have a variety of object which implement Trait2, which expects Trait1 objects as a parameter. When I try to create a vector of objects which fulfill Trait2 I get a compiler…
matanmarkind
  • 219
  • 3
  • 13
1
vote
0 answers

How can I convert dyn FnMut into a custom trait object?

As Fn-like traits can be implemented multiple times, one can not do this: trait Callable { fn call_me(&self); } impl Callable for F where F: Fn(T), T: Default, { fn call_me(&self) { self(T::default()) } } I'd like…
Lilymonade
  • 465
  • 4
  • 11
1
vote
1 answer

Turn generic method into trait object safe method

I would like to make an adapter that removes generic parameters (to produce a trait object) as in the example below. use std::ops::Deref; fn make_dyn_box(iter_in: I) where I: Iterator, S: Deref, { let mut…
dontarius
  • 99
  • 1
  • 8
1
vote
1 answer

Trait object discrepancy between Vec and HashMap

I've been struggling to understand why the following code behaves the way it does (Playground): use std::collections::HashMap; trait Trait<'a> { fn get_enum(&'a self) -> Enum<'a>; } #[derive(Clone)] enum Enum<'a> { Arr(Vec<&'a dyn…
chub500
  • 712
  • 6
  • 18
1
vote
1 answer

Why does adding a generic type to a trait affect the lifetime of trait objects and associated types?

I have following code: trait T { type AT; fn foo(&self); } struct AbstractT { t: Box>, } impl T for AbstractT { type AT = AT; fn foo(&self) { self.t.foo(); …
FLashM
  • 23
  • 4
1
vote
1 answer

How to apply a lifetime to VecDeque>?

I'm trying to create a VecDeque of structs that all implement an Animal trait. This code works, but I don't understand why adding ' static fixes it and how to make it use 'a instead. pub trait Animal { fn says(self) -> Option; } use…
John
  • 15
  • 5
1
vote
1 answer

trait cannot be made into an object

I'm working on a ray tracer and want to model all hitable objects to provide a common interface. I implemented a trait named Object which all hitable objects implement. I created a struct called Intersection which contains an f32 value and a…
SATHWIK MATSA
  • 43
  • 3
  • 6
1
vote
1 answer

Which Rust 1.2 containers support trait objects?

In the Rust Edition Guide it says that in Rust 1.2, more container types support trait objects. It gave the example of Rc, but it didn't give a complete list. What other containers support trait objects in Rust 1.2+?
user102008
  • 30,736
  • 10
  • 83
  • 104
1
vote
3 answers

How to put a reference to a trait object in an Option?

I'd like to store a reference to an io::Write trait object inside an Option in a struct but I can't figure out how. I can put the reference in directly like this: pub struct Parameters<'a> { pub log: &'a (io::Write + 'a), // Other elements…
Bob
  • 1,037
  • 1
  • 9
  • 14
1
vote
1 answer

Why sized trait is required for a builder function to generate Rc?

This code works fine (playground): use std::rc::Rc; trait Foo { fn foo(&self); } struct Bar { v: Rc, } impl Bar where T: Foo { fn new(rhs: Rc) -> Bar { Bar{v: rhs} } } struct Zzz { } impl Zzz { fn…
Eric Wu
  • 15
  • 5
1
vote
0 answers

Confused by lifetime of Box in struct

struct Wrap<'a> { pub data: Option<&'a i32>, } pub trait Boxable { fn get_data(&self) -> Option<&i32>; } impl<'a> Boxable for Wrap<'a> { fn get_data(&self) -> Option<&i32> { self.data } } struct ContTrait { pub vbox:…
1
vote
1 answer

Sharing weak trait object references

I'm trying to provide "views" of non-owned structs to separate components of a system. Assume a set of traits with distinct methods: Drawable, Modifiable and a number of structs which implement at least one of the traits - SimpleBox, Panel,…
1
vote
1 answer

Default trait method implementation for all trait objects

I have a trait MyTrait, and I want all trait objects &MyTrait to be comparable to each other and to nothing else. I have that now based on How to test for equality between trait objects?. The problem is that I need to use MyTraitComparable…
Mark
  • 18,730
  • 7
  • 107
  • 130