183 questions
3
votes
1
answer
83
views
How can I use trait objects and monomorphism together
I run into this same problem with a different flavor over and over and I'm not sure entirely how to describe it, so I'll simply give an example and explain how I'm stuck.
I'm working on an ECS system ...
3
votes
2
answers
116
views
Conditional compilation affecting type parameter
I am writing a Rust crate where I have a struct whose field implements a trait, and I'd like to provide a feature where:
#[cfg(not(feature = "dyn"))]: struct field is T, statically ...
1
vote
0
answers
163
views
Can "Go to Definition" go to a concrete class in VSCode / Python (Pylance) during debugging?
I define an attribute of type MyAbstractClass (abstract class), which takes a value of a concrete subclass MyConcreteClass. When I hit "Go to Definition" in VSCode at poke in self.item.poke()...
3
votes
2
answers
219
views
Dynamic Dispatch, Kotlin
I have this code
open class Shape {
open fun number(m: Shape) = "1"
open fun number(m: Rectangle) = "2"
}
class Rectangle : Shape() {
override fun number(m: Shape) = &...
0
votes
1
answer
57
views
Mutable borrow on extended trait
I have a vector of dynamic dispatch references that implement an extended trait. I would also like to be able to pass them in to a function that accepts the base trait:
use std::borrow::BorrowMut;
...
0
votes
1
answer
599
views
How to pass closure to dyn trait object
I want a dyn reference to a trait object, on which I have a method, taking a closure as argument:
trait DynTrait {
fn dyn_method(&mut self, closure: impl FnMut(&str) + 'static);
}
// ...
...
4
votes
2
answers
376
views
How do you select a struct based on a string in Rust?
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 ...
1
vote
1
answer
100
views
std::bind behaves differently when passed a pointer vs reference
The goal is to get a functor to the Base::Print.
#include <functional>
#include <iostream>
using namespace std;
class Base {
public:
virtual void Print() {
cout << "...
3
votes
1
answer
3k
views
for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically [duplicate]
I'm on the nightly version
#![feature(async_fn_in_trait)]
use std::sync::Arc;
trait IUserRepository {
async fn find_by_email(&self, email: &str) -> bool;
}
struct Container {
...
4
votes
1
answer
455
views
How to virtual functions and vtables work?
While the C++ standard leaves the implementation of virtual dispatch up to the compiler, there are only 3 major compilers (gcc, clang and msvc) at this point.
How do they implement virtual dispatch, ...
0
votes
2
answers
181
views
Trait object as associated type of a trait object
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 ...
1
vote
1
answer
111
views
Can I have a collection over trait objects where their associated types implement a shared trait?
I know that generally you can't have something like Vec<dyn Iterator>> because of course you need to specify the associated type.
Now, I can have Vec<Box<dyn Iterator<Item=i32>>...
1
vote
1
answer
150
views
Design patterns without the box
Rust beginner here. I have a number of algorithms that are almost identical but, at the final step, they all aggregate the results in slightly differently ways. Let's say the Algorithm does the ...
3
votes
4
answers
1k
views
Dynamic dispatch based on Enum value
Lets say I'm trying to write multiple handlers for multiple message types.
enum MESSAGE_TYPE { TYPE_ZERO, TYPE_ONE, TYPE_TWO, TYPE_THREE, TYPE_FOUR };
One solution might be
void handler_for_type_one(....
0
votes
0
answers
43
views
Calling enum_dispatch'd instances of structs from main, outside of crate or file [duplicate]
If run without changes, test_method() is called correctly for both variants in the function get_test_vector():
//! handler.rs
use enum_dispatch::enum_dispatch;
#[enum_dispatch]
trait CommonTrait {
...
1
vote
1
answer
154
views
Can late-binding in C++ be done without the need for heap-memory when using composition?
late-binding or runtime-polymorphism needs 2 things: a base pointer and a virtual method.
class FrameWorkClassBase {
public:
virtual void method() = 0;
};
class FrameWorkClassDerived: public ...
0
votes
1
answer
85
views
If CLOS had a compile-time dispatch, what would happen to this code snippet?
I am reading the Wikipedia article about CLOS.
It says that:
This dispatch mechanism works at runtime. Adding or removing methods thus may lead to changed effective methods (even when the generic ...
2
votes
0
answers
191
views
Is there a way to reduce garbage collection and/or dynamic dispatch for Julia Flux gradient calls?
Does anyone know if there's any way to reduce the amount of gc and/or dynamic dispatch for a gradient call in flux? I've tried using FastClosures.jl, as well as wrapping loss into a callable struct to ...
1
vote
1
answer
224
views
How can I write this Rust variation on strategy pattern in which a given struct owns a strategy that modifies the original struct?
Apologies for the convoluted title!
I'm new to rust, and I'm trying to solve a very specific problem. None of the solutions I've tried so far seem to work nicely within the language bounds.
I have a ...
2
votes
1
answer
2k
views
Trait Bound is not satisfied for Result<(), Box<(dyn SomeTrait + 'static)>>
use once_cell::sync::OnceCell;
pub trait SomeTrait {}
pub struct Impl1 {}
impl SomeTrait for Impl1 {}
pub static GLOBAL_THING: OnceCell<Box<dyn SomeTrait>> = OnceCell::new();
pub fn ...
4
votes
1
answer
2k
views
How to access fields of dyn structs?
Recently I've been working on learning advanced Rust. As part of that, I'm learning to use dynamic dispatch.
In my tinkering I've run across a bit of an issue. For some reason, I can't seem to access ...
3
votes
1
answer
136
views
When do I use a predefined class wide access type vs. an anonymous access to a class wide type as a parameter?
I encountered the use of predefined class wide access types as the main parameter to a subprogram many times in our code base and would like to know whether it's a flaw in the design or if it has some ...
0
votes
0
answers
21
views
C++: calling inherited virtual method using derived class pointer (pointing to derived class object) is resulting in compilation error [duplicate]
Following code results in compilation error.
error: no matching function for call to ‘D::print(int)’
Error is at following line
d_ptr->print(5);
#include<iostream>
using std::cout;
...
0
votes
1
answer
550
views
How to implement Object Oriented Dynamic Dispatch in LLVM?
I am trying to make a toy compiler for an object oriented language which supports dynamic dispatch and simple inheritance. Meaning any class Child that extends a parent can be used whenever a Parent ...
4
votes
1
answer
459
views
Swift: dynamic dispatch with protocol and subclass
Please consider the following Swift 5 code:
protocol P: class {
func call_foo()
func foo()
func call_bar()
func bar()
}
extension P {
func call_foo() { foo() }
func foo() { ...