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

Why do some std::fmt::Debug* methods use dynamic dispatch?

These methods use dynamic dispatch (receive a trait object &Debug as parameter): DebugMap::entry DebugSet::entry DebugStruct::field DebugTuple::field DebugList::entry These methods use static dispatch and are written in terms of the related entry…
malbarbo
  • 10,717
  • 1
  • 42
  • 57
3
votes
2 answers

Rust serde deserialize dynamic trait

I have a recursive data structure in my pet-project: (this is a simplified example) pub trait Condition { fn validate(&self, s: &str) -> bool; } pub struct Equal { ref_val: String, } impl Condition for Equal { fn validate(&self, s:…
Da_Niel
  • 41
  • 4
3
votes
2 answers

Supertrait as return traitobject doesn't have a known size at compile time

I am working with the DHT11 library where the argument gpio, based on the esp32 implementation, for new must implement InputPin and OutputPin. So I created a function that returns a traitobject of a supertrait of those two traits. But when I want to…
elhe
  • 122
  • 10
3
votes
1 answer

Rust: can't collect an `Iterator>` into `Vec>`

I feel like this code should work since Boxs are supposed to be able to be implicitly converted into Boxs in most situations: struct Dog {} trait Animal {} impl Animal for Dog {} fn main() { let _: Vec> = [Dog…
A. Kriegman
  • 510
  • 1
  • 4
  • 18
3
votes
1 answer

Why is rustc's suggestion here wrong?

I'm trying to implement the following trait, where the <'baz> is required for a later impl<'baz> Baz<'baz> for T where T: OtherTrait†. trait Baz<'baz>: Clone { fn baz(&'baz mut self); fn bazzed(&self) -> Self { let mut x =…
tuydore
  • 33
  • 4
3
votes
1 answer

Why using boxed objects over trait objects?

In the book Rust for Rustaceans, the author writes: Broadly speaking, though, you’ll want to use static dispatch in your libraries and dynamic dispatch in your binaries. In a library, you want to allow your users to decide what kind of dispatch is…
Marcus
  • 5,104
  • 2
  • 28
  • 24
3
votes
1 answer

Trait can't be made Object Safe when Hash + PartialEq are Supertraits

I'm pretty new to Rust, so there is the possibility that this will be an easy Question. I'm trying to create a small registry for Handlers that should return any struct that implements the TransferObject Trait: pub trait TransferObject: Hash +…
stuchlyf
  • 187
  • 1
  • 2
  • 11
3
votes
3 answers

Trait objects from references

The below is (roughly) the trait object example taken from the rust book chapter 17.2. For my use case, I want to continue to use button and select_box after creating screen (see the commented out println!() after declaring screen), however I am not…
Max888
  • 3,089
  • 24
  • 55
3
votes
1 answer

How to implement Mul Trait for a custom struct type to work in both ways

// main.rs #[derive(Clone, Copy)] struct A(f64, f64); impl Mul for A where f64: From, T: Copy, // f64: Mul, { type Output = A; fn mul(mut self, rhs: T) -> Self::Output { self.0 = self.0 * f64::from(rhs); …
3
votes
2 answers

Drop order for when boxed struct contains reference

I'm trying to make a node structure but I have no idea why this won't compile (Rust playground): trait SomeTrait {} struct SomeObject<'a> { something: &'a dyn SomeTrait, } impl<'a> SomeTrait for SomeObject<'a> {} struct OtherObject {} impl…
Ludvig
  • 637
  • 6
  • 18
3
votes
2 answers

Calling static method on type alias

While using boxed closures I ran into the following issue: type Test = Rc i64>; fn test_bad() -> Test { Test::new(|| 42) } fn test_good() -> Test { Rc::new(|| 42) } In the first case, I'm using the type alias to refer to the…
max
  • 1,048
  • 10
  • 20
3
votes
1 answer

Why does Vec.sort() seem to require a static lifetime?

This is a vastly simplified example of the issue I'm running into, but given a trait Thing which implements Ord, and a struct Object which implements Thing, I have the following struct: pub struct MyStruct<'a> { my_things: HashMap
NanoWizard
  • 2,104
  • 1
  • 21
  • 34
3
votes
1 answer

Rust: Trait RangeBounds cannot be made into an object

I sumbled upon a problem while trying out Rust, that might point to a bigger non understanding of its concepts on my part. My goal is to programm a variant of Conway's Game of Life. I want the values when cells are created or stay alive not to be…
3
votes
1 answer

How do I deal with Sized requirements in a trait object?

I'm having trouble understanding traits and object safety in Rust. I have a StoreTrait for storing some data and a Resource struct that holds a reference to a StoreTrait. I want the Resource to have a reference to a store intance, because many of…
joepio
  • 4,266
  • 2
  • 17
  • 19
3
votes
0 answers

Why is a value not converted to a trait object with dynamic dispatch in a closure?

Given the following code: use std::iter::Iterator; trait Sequence { type SeqType: Iterator; fn seq(&self) -> Option; } struct Doubler<'a>(Option<&'a [u32]>); impl<'a> Sequence for Doubler<'a> { type SeqType…
Rodolfo
  • 335
  • 1
  • 4
  • 10