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
5
votes
2 answers

Get Rc>> on a sub-type

I have the following definitions: trait A { fn f(&self); } trait B: A { // ... } I'd like implement this kind of function: fn convert(v: Rc>) -> Rc> { } I'd like to have a way for returning a value that share the…
Corebreaker
  • 357
  • 1
  • 11
5
votes
1 answer

Rust: polymorphic calls for structs in a vector

I'm a complete newbie in Rust and I'm trying to get some understanding of the basics of the language. Consider the following trait trait Function { fn value(&self, arg: &[f64]) -> f64; } and two structs implementing it: struct Add {} struct…
Gabriele
  • 469
  • 4
  • 10
4
votes
1 answer

Return Result> in a match

I have a set of types that implements a given trait, I want to get a concrete type object from a string name, strangely it works when my match returns Box but doesn't when I wrap it in a Result. Given this trait and types: trait Shape { …
Terseus
  • 2,082
  • 15
  • 22
4
votes
1 answer

expected trait object `dyn Responsability`, found type parameter `T`

I am trying to implement a responsability chain in Rust: link to playground use std::error::Error; struct Query { query: String, } struct Response { response: u64, } trait Responsability { fn take(&self, iterator:…
greg
  • 3,354
  • 1
  • 24
  • 35
4
votes
1 answer

Why is `dyn Trait` not Sized?

In a Rust tutorial about memory layout of different types, it talks about trait objects. However, as it shows, the part of trait object that lives on the stack has constant size: one word for pointer to the value, and another word for the pointer to…
zombiesauce
  • 1,009
  • 1
  • 7
  • 22
4
votes
1 answer

How can I use Rc::clone while casting to a trait object?

The Rust book says that it's idiomatic to use Rc::clone(&x) rather than x.clone() with Rc values, so that it's obvious that this is not your typical clone. I'm all for this, but I'm having trouble applying the theory in practice. I want to clone a…
Ian Henry
  • 22,255
  • 4
  • 50
  • 61
4
votes
1 answer

How do you convert a Box to a Rc?

I have a function which receives a Box and needs to convert that into an Rc to share read-only ownership within the thread. With a Box of some T: Sized, we can do Rc::new(*my_box), but unfortunately that doesn't work for…
NanoWizard
  • 2,104
  • 1
  • 21
  • 34
4
votes
1 answer

How to define a recursive trait bound in Rust?

First, I know I can use Box if I want to define a recursive structure. For example, struct LinkNode { next: Option> } impl LinkNode{ fn get_next(&self) -> Option>{ None } fn append_next(&mut self,…
Forsworn
  • 112
  • 1
  • 10
4
votes
1 answer

How do I cast Rc> to Rc>?

I am trying to cast Rc> to Rc> (Data implements Interface) but it's impossible in a generic method: use std::cell::RefCell; use std::rc::Rc; trait Interface { fn pouet(&self); } struct Data {} impl…
4
votes
1 answer

Why can't I push into a Vec of dyn Trait unless I use a temporary variable?

This is my code: use std::rc::{Rc, Weak}; use std::cell::RefCell; trait Trait {} fn push(e: E) { let mut v: Vec>>> = Vec::new(); // let x = Rc::new(RefCell::new(Box::new(e))); // v.push(x); //…
Nick
  • 10,309
  • 21
  • 97
  • 201
4
votes
1 answer

How to coerce a Vec of structs to a Vec of trait objects?

Trying to create a DB struct that is a HashMap of vectors. Each Vec contains Box. use std::collections::HashMap; trait Model { fn id(&self) -> i32; } struct User; struct Message; impl Model for User { fn id(&self) -> i32 { 4…
birwin93
  • 93
  • 6
4
votes
2 answers

What is the cited problem with using generic type parameters in trait objects?

I am reading Object Safety Is Required for Trait Objects and I don't understand the problem with generic type parameters. The same is true of generic type parameters that are filled in with concrete type parameters when the trait is used: the…
Neo
  • 3,534
  • 2
  • 20
  • 32
4
votes
2 answers

Generics plus dynamic dispatch

Consider the case where I have a function make_numbers which should create a string of random numbers, but where I want to decide at runtime (user input) what kind of random number generator should be used. To make it even more difficult, let's…
mrspl
  • 481
  • 1
  • 5
  • 10
4
votes
3 answers

How to use non-'static trait objects with associated types?

I have this type: struct Wrap(Vec); I want to implement std::ops::Index and return references to trait objects. This was my first attempt (Playground): use std::ops::Index; use std::fmt::Display; impl Index for Wrap where …
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
4
votes
1 answer

Is there a way to determine the offsets of each of the trait methods in the VTable?

I thought I could try more or less build a trait object from scratch without using the impl blocks. To elaborate: trait SomeTrait { fn fn_1(&self); fn fn_2(&self, a: i64); fn fn_3(&self, a: i64, b: i64); } struct TraitObject { data:…
Chase Walden
  • 1,252
  • 1
  • 14
  • 31
1 2
3
12 13