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
8
votes
3 answers

How to pass a boxed trait object by value in Rust?

I was writing some code and had a trait with a method that takes self by value. I want to call this method on a Box'd trait object (consuming the Box and its value). Is this possible? If so, how? In terms of code, a minimal example looks like the…
Milo Brandt
  • 435
  • 4
  • 10
8
votes
1 answer

Why can a &str not be passed to a function accepting a &dyn Display trait object?

I'm reading a Rust book and I am confused by this example: use std::fmt::Display; fn main() { test("hello"); test2("hello") } fn test(s: &dyn Display) { println!("{}", s); } fn test2(s: &str) { println!("{}", s); } Passing…
osamu
  • 983
  • 1
  • 9
  • 13
8
votes
2 answers

Why can a function on a trait object not be called when bounded with `Self: Sized`?

I have the following code: trait Bar { fn baz(&self, arg: impl AsRef) where Self: Sized; } struct Foo; impl Bar for Foo { fn baz(&self, arg: impl AsRef) {} } fn main() { let boxed: Box = Box::new(Foo); …
Tim Diekmann
  • 7,755
  • 11
  • 41
  • 69
8
votes
3 answers

Confusing error in Rust with trait object lifetime

Can anyone tell what the problem is with the following code? The compiler is complaining about lifetimes, but the error message makes absolutely no sense. I've tried everything I could think of, but nothing seems to help. use…
Antimony
  • 37,781
  • 10
  • 100
  • 107
7
votes
1 answer

Why can't some traits be made into objects

I understand the rules for when a trait can be made into a trait object, but I don't understand why these rules exist. For example: trait Resource { const RESOURCE_ID: u64; } trait ResourceStatic { fn static_id() -> u64; } trait…
wrongusername
  • 18,564
  • 40
  • 130
  • 214
7
votes
1 answer

Why does using Option::map to Box::new a trait object not work?

trait FooTrait {} struct FooStruct; impl FooTrait for FooStruct {} fn main() { let maybe_struct: Option = None; // Does not compile let maybe_trait: Option> = maybe_struct.map(Box::new); //…
Tomáš Dvořák
  • 1,490
  • 1
  • 9
  • 20
6
votes
2 answers

Why does Rust allow you to call `Iterator::for_each()` on a trait object?

I was playing around with some API concepts and noticed something peculiar in Rust's Iterator trait. I have the following trait definition: trait Observable { type Item; fn subscribe(self, f: F) -> bool where Self: Sized, …
Jose Quesada
  • 417
  • 3
  • 6
6
votes
1 answer

What are the rules for blanket implementations regarding modules?

I've been trying to find documentation on how Rust resolves trait blanket implementations in case module boundaries are involved, but didn't find much directly related to it. Let's consider an example with two similar, but slightly different code…
soulsource
  • 197
  • 7
6
votes
1 answer

How can I create a list of owned trait objects without allocating each item on the heap separately?

I want an owned list of Rust trait objects. I could implement it as Vec> but that allocates space on the heap for every trait object. What I’d prefer is a CompactList type with a memory representation that looks…
Calebmer
  • 2,972
  • 6
  • 29
  • 36
6
votes
3 answers

How do you create a Box, or a boxed unsized value in general?

I have the following code extern crate rand; use rand::Rng; pub struct Randomizer { rand: Box, } impl Randomizer { fn new() -> Self { let mut r = Box::new(rand::thread_rng()); // works let mut cr = Randomizer { rand: r…
Andreas
  • 6,447
  • 2
  • 34
  • 46
5
votes
2 answers

How do you implement a trait for T where T: Foo OR Bar

In rust, you can automatically implement a trait for any type that implements some other combination of traits. Ie: impl SomeTrait for T { some_function(&self) { /*...*/ } } What I'm trying to do is define a…
nebulaeandstars
  • 229
  • 1
  • 8
5
votes
1 answer

Cloning an Rc pointer over a trait object in Rust?

I am learning Rust and don't understand why the following doesnt work. I gather we are unable to clone an Rc pointer over a trait object? How am I to pass such a reference to an function defined only by a trait, as attempted in some_function? use…
Mark
  • 672
  • 1
  • 6
  • 19
5
votes
1 answer

Is there a heapless trait object?

Is there a way to implement trait objects completely in stack memory? This is the code that I use Box and thus heap memory: extern crate alloc; use alloc::vec::Vec; use alloc::boxed::Box; pub trait ConnectionImp { fn send_data(&self); } pub…
p3zhy
  • 81
  • 8
5
votes
1 answer

Assert equality of trait objects?

The usual assert_eq! macro requires that PartialEq be implemented across a struct - I have a vector of trait objects, Vec>, where Element is a trait requiring Debug, pub trait Element: std::fmt::Debug. I cannot similarly require…
user8866053
5
votes
2 answers

Rust structs that have Box fields and that impl async traits

I'm running into an issue with structs that have Box fields and that impl async traits. Specifically error: future cannot be sent between threads safely It looks like the error occurs because I use a Box field in a struct that impl's an async…
geofflittle
  • 437
  • 1
  • 3
  • 14
1
2
3
12 13