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

Construct an array of objects in Rust

This is my very first Rust program that actually has a purpose and I'm still lost with the syntax. So I have trait objects like these: trait HC<'a> { fn info(&self) -> TestInfo<'static>; fn test(&self) ->…
MegaBrutal
  • 310
  • 2
  • 9
1
vote
1 answer

Create vector of trait objects

I am trying to create a vector of trait objects but I'm getting a type mismatch. I also tried using .map() instead of a for loop but had the same problem. This is a minimal version of my real code, I realise this example doesn't require the use of…
Max888
  • 3,089
  • 24
  • 55
1
vote
1 answer

Failure to apply turbofish notation on (boxed) trait objects

I can create a Write trait object that points to a (heap-stored) File object as such: let a : Box = Box::new(File::open("/dev/null").unwrap()); // Compiles fine. Using the turbofish notation, however, produces an error (Rust…
Nubarke
  • 2,020
  • 2
  • 13
  • 12
1
vote
2 answers

Ergonomically passing a slice of trait objects

I am converting a variety of types to String when they are passed to a function. I'm not concerned about performance as much as ergonomics, so I want the conversion to be implicit. The original, less generic implementation of the function simply…
Zombie_Pigdragon
  • 304
  • 4
  • 13
1
vote
0 answers

Use Rust trait object with generic method

I am trying to write a trait describing a client for an HTTP server where I can tie the response to the query type: trait Query { type Output; } trait ApiClient { fn send(&self, q: &Q) -> Q::Output where Self: Sized; } I am…
Demurgos
  • 1,568
  • 18
  • 40
1
vote
2 answers

Implementation of a Trait over a Rc doesn't resolve to a Rc, following implementations were found as Trait>

I'm trying to wrap a DST around a Rc with the aim of cloning it and accessing it from various parts of the code but the following error appears on compilation. Here is a minimal reproducible example of the error (playground): use std::rc::Rc; trait…
cdecompilador
  • 178
  • 2
  • 9
1
vote
1 answer

Problem with implementing a trait over another trait with associated types in Rust

I have this trait: trait Pokemon { type Move; fn pick_move(&self) -> Self::Move; } Certain types implement the trait, like so: #[derive(PartialEq, Clone, Copy)] enum Fire { Charmander, Charmeleon, …
Wave Metric
  • 129
  • 1
  • 3
  • 13
1
vote
2 answers

Type does not implement Copy error when using supertrait of Copy in an enum

I'm new to Rust traits, so this could be due to a misunderstanding of supertraits, dyn or anything else. I'm trying to use a trait object in an enum to: Put a trait bound on the concrete types that can be used in this element of the enum Make sure…
Kyle_S-C
  • 1,107
  • 1
  • 14
  • 31
1
vote
0 answers

Why is my concrete type that implements MyTrait not a valid "dyn MyTrait" trait object?

So I'm storing trait objects behind Rc::Weak references as part of an implementation of the Observer Pattern in Rust, and I'm running into an issue where I try to store a Weak inside of a Vec>, where T is a concrete instance of…
1
vote
1 answer

"temporary value dropped while borrowed" with capturing closure

Please consider the following example (playground): struct Animal<'a> { format: &'a dyn Fn() -> (), } impl <'a>Animal<'a> { pub fn set_formatter(&mut self, _fmt: &'a dyn Fn() -> ()) -> () {} // Getting rid of 'a here satisfies the compiler …
Attila Kun
  • 2,215
  • 2
  • 23
  • 33
1
vote
1 answer

Rust: return a generic struct from a function where (only) is different

I'm looking for help with the correct syntax or Rust approach. My use case: I have a generic struct FileData, which has a variable called provider. Provider must implement AsRef<[u8]> so that data may come from static bytes, heap allocated memory,…
Juergen
  • 699
  • 7
  • 20
1
vote
1 answer

How to write trait & impl with lifetimes for iterators?

I'm trying to understand how to write a trait and an impl for it for my own types that will process some input data. I'm starting with a simple example where I want to process the input 1, 2, 3, 4 with a trait Processor. One implementation will skip…
user655321
  • 1,572
  • 2
  • 16
  • 33
1
vote
1 answer

A struct with a trait field, but optional

Say I have a struct whose implementation writes somewhere, i.e. to something that implements the std::io::Write trait. However, I don't want the struct to own this. The following code works: fn main() { let mut out = std::io::stdout(); let…
cb7
  • 493
  • 4
  • 10
1
vote
1 answer

How to find generic bounds for producer that creates objects with trait-lifetime bounds

I have reduced my actual code to this minimal example: trait Producer

where P: Something { fn produce(&self) -> Box

; } struct P1 {} impl Producer for P1 { fn produce(&self) -> Box { Box::new(B1 {}) } } trait…

fyaa
  • 646
  • 1
  • 7
  • 25
1
vote
0 answers

Is it possible to call clone on a struct that contains a FnMut closure trait object?

I have a loop that pops items off a queue, invokes the callback with the item and then needs to add the item back on to the queue. The reason for this is that the items are really repeating timers, however I have removed any timer code in this…
zman
  • 203
  • 2
  • 7