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
90
votes
4 answers

What makes something a "trait object"?

Recent Rust changes have made "trait objects" more prominent to me, but I only have a nebulous grasp of what actually makes something into a trait object. One change in particular is the upcoming change to allow trait objects to forward trait…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
68
votes
1 answer

Sending trait objects between threads in Rust

I'd like to send a trait object between threads, but can't figure out if it's possible. It seems like it might not be, as they apparently do not fulfill the Send trait. The following code demonstrates what I'm trying to do: use std::{ …
guff
  • 803
  • 1
  • 6
  • 6
47
votes
3 answers

The trait cannot be made into an object

I have the following code: extern crate futures; // 0.1.24 use futures::Future; use std::io; struct Context; pub trait MyTrait { fn receive(context: Context) -> Future; } pub struct MyStruct { my_trait:…
Alexander
  • 833
  • 2
  • 8
  • 17
18
votes
3 answers

Why can't `&(?Sized + Trait)` be cast to `&dyn Trait`?

In the code below it is not possible to obtain a reference to a trait object from a reference to a dynamically-sized type implementing the same trait. Why is this the case? What exactly is the difference between &dyn Trait and &(?Sized + Trait) if I…
w1th0utnam3
  • 963
  • 7
  • 19
13
votes
1 answer

Difference between &mut and ref mut for trait objects

First of all, I'm not asking what's the difference between &mut and ref mut per se. I'm asking because I thought: let ref mut a = MyStruct is the same as let a = &mut MyStruct Consider returning a trait object from a function. You can return a…
Rodolfo
  • 335
  • 1
  • 4
  • 10
13
votes
2 answers

What is the difference between Box and &Trait / Box?

When writing code with traits you can put the trait in a trait bound: use std::fmt::Debug; fn myfunction1(v: Box) { println!("{:?}", v); } fn myfunction2(v: &T) { println!("{:?}", v); } fn main() { …
Erik Vesteraas
  • 4,675
  • 2
  • 24
  • 37
12
votes
2 answers

Why are trait methods with generic type parameters object-unsafe?

To quote the Book (emphasis mine), The same is true of generic type parameters that are filled in with concrete type parameters when the trait is used: the concrete types become part of the type that implements the trait. When the type is forgotten…
nalzok
  • 14,965
  • 21
  • 72
  • 139
12
votes
1 answer

How to move a value out of an object-safe trait object?

A Mech carries a driver, which is a Named entity. At run-time, an omitted Mech constructor consults external source for the specific type of driver to use. trait Named { fn name(self) -> String; } struct Person { first_name: String, …
mwgkgk
  • 173
  • 8
12
votes
2 answers

Can't clone Vec> because Trait cannot be made into an object

I'm trying to clone a vector of boxed traits. Naturally simply deriving Clone on all the structs that implement my trait isn't enough, because the compiler doesn't know at compile time that all the structs implementing the trait have Clone. Okay, so…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
12
votes
1 answer

Function returning a closure not working inside my filter

I cannot get this to compile without using a closure. I'm trying to get the function apply to return the correct kind of closure in the first place. #![feature(conservative_impl_trait)] #![allow(dead_code)] fn accumulate<'a>(tuples: &[(&'a str,…
iopq
  • 1,023
  • 12
  • 16
11
votes
1 answer

How to Return a Result with generic error

I want to write a function that reads the contents of a file, and raises an error if it fails. I want to call this function from a python script, so I'm including some mentions of Python below in case it might be relevant. As I have tried showing in…
Simen Russnes
  • 2,002
  • 2
  • 26
  • 56
11
votes
2 answers

function pointer vs Fn trait object

fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 { // definition f(arg) + f(arg) } do_twice(|x| x + 1, 5) // call This function accepts both, closures and function pointers. It takes a function pointer as parameter type. When should I prefer…
10
votes
1 answer

Are vtables generated for all types that implement a trait?

If I have a trait Foo, and some implementors Bar, Baz. impl Foo for Bar { } impl Foo for Baz { } But say I only use one of them ever as a trait object, let bar = Bar {..}; let foo: &dyn Foo = &bar; Then will my binary still have vtable for…
zombiesauce
  • 1,009
  • 1
  • 7
  • 22
10
votes
2 answers

Understanding Traits and Object Safety

I am struggling with the basics of object safety. If I have this code struct S { x: i32, } trait Trait: Sized { fn f(&self) -> i32 where Self: Sized; } fn object_safety_dynamic(x: Trait) {} I receive error[E0038]: the trait…
left4bread
  • 1,514
  • 2
  • 15
  • 25
9
votes
1 answer

How do I pass Rc>> to a function accepting Rc>>?

I have originally asked this question here, but it was marked as duplicate, although it duplicates only a part of it in my opinion, so I have created a more specific one: Consider the following code: use std::rc::Rc; trait MyTrait { fn…
Dmitry Uvarov
  • 673
  • 6
  • 6
1
2 3
12 13