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
0
votes
0 answers

Can I have a mutable reference to a type and its trait object in the same scope?

Can I have a mutable reference to a value and a mutable reference to a trait object of the same value inside the same scope? Is that undefined behavior? An example code snippet is added below for clarification. In the below code is it valid to have…
0
votes
0 answers

Function accept type argument

I am working on a client server application where i send messages to / from each as protocol buffers using the rust crate prost. currently i have a generic send_message function that can take any struct that implements the prost::Message trait. …
matthewmturner
  • 566
  • 7
  • 21
0
votes
1 answer

Implement Trait for generic caller?

I was just reading this article about generics and Traits in the Rust by Example, where they seem to implement a Trait on a generic caller. Check their article for the full example, but here's a minimal working example: struct Example; trait Foo…
Eden Landau
  • 553
  • 4
  • 12
0
votes
1 answer

Trait with constructor that takes borrows to borrows cannot infer liftime on usage

I need a trait that allows me to construct a object that borrows an object that borrows something. In the following example that is PaperBin.…
0
votes
2 answers

Rust: how to reuse the implementations of getter and setter?

Since Rust does not support inheritance, we cannot reuse the states of another struct. Take an example in Head First Design Patterns, an abstract Duck class has an attribute FlyBehavior, and it also provides getter and setter. abstract class Duck { …
chenzhongpu
  • 6,193
  • 8
  • 41
  • 79
0
votes
1 answer

A parameter that accepts a sequence of trait objects and iterate through it multiple times in Rust

I have a function that takes a sequence of trait objects and iterates through it multiple times, e.g., trait Animal { fn make_noise(&self); } fn make_lots_of_noises(animals: &[&dyn Animal]) { animals.iter().for_each(|animal|…
Tony Beta Lambda
  • 529
  • 3
  • 18
0
votes
1 answer

Automatic error conversion using the '?' operator for custom types

I'm struggling to understand the nuances of the ? operator. Take the following code: link to playground use std::{error::Error as StdError, fmt}; #[derive(Debug)] struct MyError(Box); impl fmt::Display for MyError { fn fmt(&self,…
jeanluc
  • 1,608
  • 1
  • 14
  • 28
0
votes
1 answer

"temporary value dropped while borrowed" with HashMap<&str, &dyn Fn(&str) -> bool>

I have a HashMap of strings to functions. From my understanding the &dyn Fn(&str) -> bool is necessary since I want to use both functions and closures, however I'm getting this compile error: error[E0716]: temporary value dropped while borrowed …
tripplet
  • 335
  • 1
  • 11
0
votes
1 answer

Rust cache async traits

I'm running into an issue when I attempt to cache a value for as long as it's valid and update it when it becomes invalid. I believe the issue is due to my attempt to share state across async executions. Further, this component lives in a…
0
votes
0 answers

How can I change the data pointer of dynamic dispatch / a trait object?

I have a Vec named vec and a variable named x of type &dyn Trait. I want the data pointer of x to point to a position in vec. #[derive(Debug)] struct Point { x: u8, y: u8, } fn main() { let vec: Vec = vec![35, 1, 51, 10]; …
0
votes
1 answer

Rust says the function parameter does not live enough, even though proper lifetime have been placed

For background context: I am creating an Observer/Subscriber based Global Event System (using single shared event systems). I decided to use FnMut as my callback closure. The lifetime 'a placed at the impl of the makeshift struct Data<'a> should…
entropy32
  • 169
  • 3
  • 11
0
votes
1 answer

"associated type ... must be specified", but isn't used

Why is the associated type required here, even though it's never used? trait Tr { type Ty; fn go(&self) -> () {} } fn foo(t: dyn Tr) -> () { t.go() } I get 2 | type Ty; | -------- `Ty` defined here ... 6 | fn foo(t: dyn…
joel
  • 6,359
  • 2
  • 30
  • 55
0
votes
1 answer

Store lambda returning iterator in the struct

I want to store the lambda that will return iterator in the struct. The lambda is needed because not all the containers implement iter() function (e.g. String::chars()), so I need a generic way to get iterator from container. use…
Alex
  • 9,891
  • 11
  • 53
  • 87
0
votes
1 answer

How do I create a trait object from another trait object?

The latest stable version of Rust (1.27) allows implementing a trait for trait objects (dyn Trait), so I tried the following: trait T1 { fn f1(&self); } trait T2 { fn f2(&self); } impl T2 for dyn T1 { fn f2(&self) { self.f1() …
Earth Engine
  • 10,048
  • 5
  • 48
  • 78
0
votes
1 answer

Is it possible to pass an Arc> to a function without using a type parameter?

I have to pass a Arc> to a function: use std::sync::{Arc, RwLock}; fn main() { let closure = || println!("Hello World"); let wrapped_closure = Arc::new(RwLock::new(&closure)); execute(wrapped_closure); } fn execute(f:…
kangalio
  • 652
  • 8
  • 16
1 2 3
12
13