trait-objects Questions
4
Solved
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 c...
Neysa asked 19/12, 2014 at 14:14
3
Solved
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?
I...
Gangue asked 12/12, 2020 at 3:46
2
Solved
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<F>(se...
Tetanus asked 11/4, 2022 at 0:40
1
Solved
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...
Avoidance asked 18/12, 2021 at 9:16
1
Solved
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 exam...
Rains asked 4/12, 2021 at 22:7
1
// main.rs
#[derive(Clone, Copy)]
struct A(f64, f64);
impl<T> Mul<T> for A
where
f64: From<T>,
T: Copy, // f64: Mul<T>,
{
type Output = A;
fn mul(mut self, rhs: T) ->...
Dioptometer asked 3/12, 2021 at 20:13
1
Solved
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 =...
Cobra asked 14/10, 2021 at 16:59
1
Solved
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 on...
Mafala asked 22/9, 2021 at 11:25
1
Solved
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...
Alchemize asked 29/7, 2021 at 18:46
2
Solved
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 t...
Warwick asked 31/5, 2021 at 2:10
1
Solved
The usual assert_eq! macro requires that PartialEq be implemented across a struct - I have a vector of trait objects, Vec<Box<dyn Element>>, where Element is a trait requiring Deb...
Aristate asked 19/3, 2021 at 0:29
1
Solved
Please consider the following example (playground):
struct Animal<'a> {
format: &'a dyn Fn() -> (),
}
impl <'a>Animal<'a> {
pub fn set_formatter(&mut self, _fmt: &am...
Duky asked 31/1, 2021 at 22:49
1
Solved
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 c...
Weatherspoon asked 22/1, 2021 at 12:49
1
I am trying to cast Rc<RefCell<Data>> to Rc<RefCell<dyn Interface>> (Data implements Interface) but it's impossible in a generic method:
use std::cell::RefCell;
use std::rc:...
Isopropyl asked 29/12, 2020 at 16:17
2
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 us...
Tomkin asked 26/11, 2020 at 19:57
1
Solved
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...
Sideburns asked 17/10, 2020 at 23:5
3
Solved
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 diff...
Instead asked 7/8, 2019 at 15:47
2
Solved
use std::sync::Arc;
trait Trait {}
struct TraitImpl {}
impl Trait for TraitImpl {}
fn main() {
let value = TraitImpl {};
let _: Arc<dyn Trait> = Arc::new(value); // compiles
let _: Arc&l...
Windhoek asked 14/5, 2020 at 13:52
1
Solved
I want an owned list of Rust trait objects. I could implement it as Vec<Box<dyn Trait>> but that allocates space on the heap for every trait object. What I’d prefer is a CompactList<...
Trenatrenail asked 18/3, 2020 at 14:43
1
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: ...
Reiff asked 6/9, 2019 at 7:17
2
Solved
This is a follow up question from Rust dynamic cast trait object between different taits. The solution provided there works really well when we use references for trait objects, but this time I am ...
Hendley asked 30/4, 2019 at 19:45
2
Solved
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...
Ji asked 14/1, 2019 at 12:45
2
Solved
I have the following code:
trait Bar {
fn baz(&self, arg: impl AsRef<str>)
where
Self: Sized;
}
struct Foo;
impl Bar for Foo {
fn baz(&self, arg: impl AsRef<str>) {}
}
f...
Wallache asked 13/8, 2018 at 12:13
1
Solved
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) -> Strin...
Tyrothricin asked 20/6, 2018 at 13:26
2
Solved
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...
Blankenship asked 25/4, 2018 at 8:54
1 Next >
© 2022 - 2024 — McMap. All rights reserved.