Skip to main content
Filter by
Sorted by
Tagged with
7 votes
1 answer
142 views

I want to build an application that interfaces with various cameras. Which specific camera is determined at runtime based on which is available. However, all cameras send frames one by one. Thus, to ...
AILogic's user avatar
  • 87
14 votes
1 answer
484 views

In Rust, the object Box<dyn Any> has a default life time of 'static. The compiler seems to accept using a different lifetime, for example Box<dyn Any + 'short>. However, this causes a ...
mwolfe 11's user avatar
  • 181
5 votes
1 answer
126 views

In Rust I have a trait Node. I would like to determine if two &dyn Node refer to the same object. I understand that &dyn Node is implemented as a fat pointer, with a base address and a vtable ...
ridiculous_fish's user avatar
3 votes
1 answer
107 views

I have a situation in my codebase where i have one function which has a boxed trait object calling a function which expects an implementor of that trait, and i got the error message shown below. For ...
Nikhil Nathanael's user avatar
5 votes
1 answer
113 views

I've encountered a strange variance behavior that I'm sure is a hole in my understanding of the type system, but feels like a compiler bug. trait Trait: 'static {} impl<T> Trait for T where T: '...
JMA's user avatar
  • 479
3 votes
1 answer
78 views

I have a custom smart pointer which I'm trying to extend to trait objects. Essentially what I'm trying to make is the following: use std::ptr::NonNull; struct Ptr<T: ?Sized> { data: NonNull&...
Jared Dewey's user avatar
3 votes
1 answer
98 views

What do I want A Trait to represent all the type of data, like String,Vec<String>,i32, etc Including some other complex data types, like Box<Arc<String>> and some of my customized ...
Ethan Teng's user avatar
1 vote
1 answer
141 views

I'm trying to create an object that can contain BufReader<File> and BufReader<Stdin> simultaneously. I'd like to avoid the more commonly used "trait object" (i.e. Box<dyn ...
foehn's user avatar
  • 469
2 votes
1 answer
488 views

I was experimenting with associated functions inside a trait's implementation, in order to use the trait's original implementation of the function. But I can't find a way to do that. My ...
Divyansh's user avatar
1 vote
1 answer
293 views

In Rust, why are trait objects usually used via references (&dyn Trait) or smart Pointers (like Box<dyn Trait>)? Does it have to be? Or is it better to use it this way?
sundegan's user avatar
4 votes
1 answer
2k views

I can't wrap my head around the second impl block. In my understanding, impl is typically used to implement a trait/methods on a concrete type like a struct. However, what does it mean to implement ...
Code learner's user avatar
0 votes
1 answer
622 views

What does this error mean? Reproduction error[E0308]: mismatched types --> src/main.rs:50:35 | 50 | PaymentType::InvoiceIn => InvoiceIn { | ___________________________________^...
Fred Hors's user avatar
  • 3,823
0 votes
1 answer
183 views

I am new to rust and trying to wrap my head around this error. If i use write this code pub struct A {} pub trait Trait { fn stuff(self: Box<Self>) -> Box<dyn Trait>; } impl Trait ...
user3929076's user avatar
0 votes
0 answers
63 views

I have the following struct: struct Struct<T: ?Sized = DefaultType> { _phantom: PhantomData<T>, } impl<T> Struct<T> where T: ?Sized + MaybeAnotherBound, { fn ...
TheOperator's user avatar
  • 6,606
0 votes
0 answers
76 views

I want to define a slice of MaybeUninit of trait objects, something like [MaybeUnint<dyn MyTrait>;2]. Of course, this doesn't work because Rust doesn't know the size of a dyn MyTrait at compile ...
14159's user avatar
  • 185
0 votes
0 answers
138 views

I'm studying Rust, and I'm struggling to replicate some OOP pattern using Rust's traits. Briefly, I'd like to know what is Rust's way to solve this kind of problem. I have an interface whose methods ...
felipetg's user avatar
1 vote
1 answer
491 views

I'm learning rust, and a project idea I had was to write a command line parsing framework, that enables the use of commands and options. I have previously done this same project in other languages. I ...
Levente Bokor's user avatar
3 votes
1 answer
1k views

Background I'm trying to create a trait object with a single associated function, but that function needs to be async. So, I've been trying to use the async_trait crate to enable just that. Problem ...
E Y's user avatar
  • 465
0 votes
0 answers
49 views

I am trying to create a simple Rust CRUD app, using Diesel and a repository pattern. My goal is to make the read/write methods generic, even if I know it might impact the code readability. Here is my ...
Ismaïl Mourtada's user avatar
5 votes
2 answers
716 views

Consider the following Rust code: #[derive(Debug, Clone, Copy)] struct Struct; trait Trait {} impl Trait for Struct {} fn f() { // Why does this coerce to the target type (Result<Box<dyn ...
RBF06's user avatar
  • 2,511
4 votes
0 answers
2k views

The pros/cons of using static vs dynamic dispatch have been addressed in a few related questions, e.g. 1, 2, 3. From a performance perspective, it is intuitive to assume that dynamic dispatch (via ...
bluenote10's user avatar
  • 27.4k
3 votes
3 answers
2k views

I know it's because of object safety: Object safe traits can be the base trait of a trait object. A trait is object safe if it has the following qualities (defined in RFC 255): ... It must not have ...
Simin Xiong's user avatar
3 votes
1 answer
159 views

I have a trait Transformable which looks something like this: pub trait Transformable { fn position(&self) -> Vec2f; fn set_position<V>(&mut self, position: V) where V: Into&...
junglie85's user avatar
  • 1,549
4 votes
2 answers
380 views

Problem Statement I have a set of structs, A, B, C, and D, which all implement a trait Runnable. trait Runnable { fn run(&mut self); } impl Runnable for A {...} impl Runnable for B {...} impl ...
RBF06's user avatar
  • 2,511
2 votes
2 answers
398 views

So I have been exploring/trying to understand static/dynamic dispatch, as well as trait objects in Rust. I think I get the gist of it. But there is still one thing that I cannot figure out: why does a ...
Martin Jocqueviel's user avatar
0 votes
0 answers
64 views

Something of a beginner at Rust here. I'm trying to implement a trait, Ring, for functions that map data types that map types implementingRings to themselves. Ring looks like this: trait Ring { fn ...
Sam Kinney's user avatar
1 vote
2 answers
203 views

I am studying the Traits and the Trait Object in Rust. In the Trait chapter, I solved the 6th exercise differently than the compiler suggestion. The following code defines two structs (Sheep and Cow) ...
Péter Szilvási's user avatar
2 votes
1 answer
163 views

I have an type alias for a Fn trait like type SomeSub = dyn for<'a> Fn(&'a str) -> &'a str; which I would like to use with a Box with an explicit lifetime like Box<SomeSub + 'b>....
Sascha's user avatar
  • 460
1 vote
2 answers
82 views

I am trying to implement a trait on a boxed trait object of the same trait. I've done this before for trait whose methods take &self which works fine, but not self. // The purpose of this trait is ...
Max888's user avatar
  • 3,880
2 votes
1 answer
824 views

I have two structs implementing the same trait using a different algorithm. I want to write the unit tests once and run them against both structs. What is the best way to do this? My concern is to ...
noam cohen's user avatar
0 votes
1 answer
74 views

I have a function that searches through a list of trait objects and attempts to find one whose implementor is of a specific type, however the compiler doesent accept the return type. pub struct ...
Isaksak's user avatar
13 votes
1 answer
8k views

This simple program yields a compiler error: #[tokio::main] async fn main() { tokio::spawn(async { foo().await; }); } async fn foo() { let f1 = bar(); let f2 = bar(); ...
guenhter's user avatar
  • 12.3k
1 vote
1 answer
86 views

As per the below example, I am trying to store functions as trait objects. I have worked out how to do this for functions which return a concrete type, by using as to cast from a function item to a ...
Max888's user avatar
  • 3,880
0 votes
1 answer
82 views

I want to implement HasChildren for all types which implement Element and for all functions which return impl Element. My case use is that I have a Vec<Box<dyn HasChildren>> in which I ...
Max888's user avatar
  • 3,880
0 votes
2 answers
157 views

Think below code, impl multiple times both for struct and traits: mod m { pub trait Foo { fn xyzzy(&self) { println!("foo!"); } } pub trait Bar { fn ...
James Yang's user avatar
2 votes
1 answer
238 views

I encountered a compile error related to lifetime in my Rust code. Here is the code causing the error: fn to_pointer_vec(ptrs: &[*const dyn ToString]) -> Vec<*const dyn ToString> { ...
keyboardman's user avatar
0 votes
1 answer
110 views

Basically I am trying to implement visitors-coding paradigm, where Expr trait needs to be implemented by Binary struct. I want to use Expr as a trait object. Any entity wanting to interact with Expr ...
Adnan's user avatar
  • 98
2 votes
2 answers
646 views

I'm failing to cast dyn SomeTrait + Send + Sync to dyn SomeTrait + Sync. More specifically, I am trying to cast &Vec<Box<dyn SomeTrait + Send + Sync>> to &Vec<Box<dyn ...
hjpev's user avatar
  • 771
0 votes
1 answer
108 views

I'm new to Rust and like many finding lifetimes quite hard to get the hang of. I get the basics, but I can't quite fathom how to make this work. I'm trying to implement an abstract data structure that ...
d11wtq's user avatar
  • 35.3k
1 vote
1 answer
236 views

I have a structure wrapping a Vec of Box<dyn Any> constructors which I've asserted to be clone. I have a trait Ctor which takes the clone logic and produces another Box<dyn Ctor>. It ...
dspyz's user avatar
  • 5,604
0 votes
1 answer
191 views

In the following code I have a simple trait, A, and a struct Foo that implements A... Next, I define a function that takes a reference to a trait object. From main() I pass in a reference to a ...
Chuck's user avatar
  • 1,870
0 votes
1 answer
183 views

Trying to filter a set of items, based on a predicate that is generic, because computed at run-time: fn main () { let el = vec![ vec![10, 20, 30], vec![40, 50, 60] ]; ...
doplumi's user avatar
  • 3,148
0 votes
1 answer
196 views

I am in a situation where Rust makes me add a HRTB for a generic type that is used as argument in a trait object. But this HRTB makes nested closures not work. Here's the trait I'm going to use to ...
Kurt Schelfthout's user avatar
0 votes
2 answers
192 views

I wrote two structs that implement a common trait Solve. Solve has an associated type Answer with trait bound Display. I want the function create_solver to return a trait object of Solve. I need help ...
stackcycle's user avatar
2 votes
0 answers
92 views

Consider the following (playground) trait Module {} struct Foo { module_box: Box<dyn Module + 'static>, module_rc: Rc<dyn Module + 'static>, } impl Foo { fn mut_box<'s>(...
plafer's user avatar
  • 185
0 votes
2 answers
84 views

Using Rc, I can cast an Rc of a concrete type to a trait object: use std::rc::Rc; trait Foo {} impl Foo for usize {} fn main() { let x: Rc<usize> = Rc::new(1); let y: Rc<dyn Foo>...
Román Cárdenas's user avatar
2 votes
1 answer
121 views

My real case is similar to the Rust doc about dyn trait with Screen and Draw trait. So I built an example totally similar to the book. But instead of initializing the vector in place, I need to have a ...
Zhou Lebo's user avatar
0 votes
1 answer
829 views

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 ...
Lukas's user avatar
  • 472
0 votes
2 answers
110 views

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 ...
Cody Martin's user avatar
3 votes
2 answers
2k views

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: ...
Da_Niel's user avatar
  • 41

1
2 3 4 5