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

How can I create a variable to a trait with an associated type in Rust?

I want to create a variable that holds a trait. The trait implementation is unknown during compile time. Hence, I need a trait object. This works with "normal" traits but not when the trait has an associated type that is unknown during compile…
Lukas
  • 381
  • 3
  • 13
0
votes
2 answers

Cannot relax lifetime of Trait Object

I have the following code: use tokio; // 1.7.1 use futures::future::Future; use std::pin::Pin; use futures::FutureExt; use std::marker::PhantomData; use std::marker::Send; use std::sync::Arc; use async_trait::async_trait; struct Orchestrator { …
0
votes
1 answer

Define variable of type TimeZone

The following compiles and runs fine: use chrono_tz::Tz; use chrono::{TimeZone, NaiveDate}; use arrow2::temporal_conversions::parse_offset; fn my_func(tz: &str) -> (){ let ndt = NaiveDate::from_ymd_opt(2018, 9, 28).unwrap().and_hms_opt(2, 30,…
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
0
votes
1 answer

Rust: Trait inference not working when variable is a reference

I am implementing a linked list in Rust, and so far, the best way I have found to let the nodes point to other nodes or point to nothing is multiple structs that implement the same trait. (Option wouldn't work, because I couldn't figure out how to…
0
votes
0 answers

Derive `Clone` for struct containing trait object

I have a struct containing a trait object and I would like to be able to derive Clone on the struct. trait MyTrait {} #[derive(Clone)] pub struct Container { trait_object: Box, } I have tried Box but that…
Max888
  • 3,089
  • 24
  • 55
0
votes
2 answers

Get mutable reference to an item in trait objects collection

To play around with Rust, I'm trying to get the following code working (playground, please don't mind the commented blocks, they are for further investigations). Basically I would like to store in a collection several items of several types…
0
votes
1 answer

Return Vec mut ref trait objects from Vec

I am trying to implement part of a UI framework. In the bottom example, the impl of widgets_mut() for Vec needs to return a vector of trait objects like Vec<&mut dyn AnyWidget>. Given T is constrained to impl AnyWidget I don't understand why I am…
Max888
  • 3,089
  • 24
  • 55
0
votes
0 answers

How to properly use associated types and trait objects together

Here is my setup #[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)] pub struct Foo {} #[derive(Copy, Clone, Debug, Default, Deserialize, Serialize)] pub struct Bar {} pub trait MyTrait { type CreateMsg; type DeleteMsg; } impl…
ewoolsey
  • 91
  • 1
  • 7
0
votes
1 answer

Inconsistency of lifetime bound requirement when storing closures

When I try to store closures to a HashMap, I come across a lifetime bound requirement reported by the compiler. It seems like an inconsistent requirement. struct NoBox ()>(HashMap); impl NoBox where C: Fn() -> (), { …
Tim
  • 99
  • 1
  • 5
0
votes
1 answer

Rust struct with reference to a type with a dynamic parameter

Here's what I want to do: struct Foo { inner: T, } struct Bar<'a> { foo: &'a Foo, // This is pseudo code, I don't think Rust has this feature. } I want an instance of Bar to hold a reference to a Foo where the type…
A. Kriegman
  • 510
  • 1
  • 4
  • 18
0
votes
1 answer

How to use the typestate pattern in other struct

I want to use the typestate pattern to define several states that allow some exclusive operations on each of them. I'm using traits instead of an enum to allow further customizations. So, I'm able to use this pattern until I try to include it inside…
Deveres
  • 97
  • 7
0
votes
1 answer

Shared &str along multiple structs conflicts with Lifetimes

I have the following code: pub trait Regex: RegexClone { fn check(&self) -> Result; fn next(&self) -> Option>; } pub trait RegexClone { fn regex_clone(&self) -> Box
0
votes
0 answers

Problem with generic parameters and Self keyword in Trait methods. the trait RequestBuilderTrait cannot be made into an object

I'm trying to mock some structs by making them implement traits but I encounter an error when I define a trait as return type for a method: the trait RequestBuilderTrait cannot be made into an object. consider moving send_form to another…
0
votes
1 answer

Why doesn't a trait object move when it calls enumerate()?

Let me give the code first fn dump(iter: &mut dyn Iterator) { for (i, v) in iter.enumerate() { println!("{} {}", i, v); } for v in iter { println!("{}", v); } } #[test] fn test_trait() { let v =…
shimoo
  • 1
0
votes
1 answer

Type handling in the trait object

I'm new to rust and I recently ran into a problem with trait I have a trait that is used as the source of a message and is stored in a structure as a Box trait object. I simplified my logic and the code looks something like…
Papulatus
  • 677
  • 2
  • 8
  • 18