Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: How to have one code for dyn, impl and T?
Hi all! I'm here with a question, which can be expressed in "how to not repeat code".
I'll assume with all know a little about Generics, Impl and dyn (with Box), the main issue is that we can use the same code using the three of them, I'll continue with Generics and Box because are the easier to use as examples:
trait A<T> {}
trait B {}
fn fn_T<T, W>(j: W)
where
W: A<T>,
{
todo!()
}
fn fn_Box(j: Box<dyn A<Box<dyn B>>>) {
todo!()
}
The first thing you need to notice is that both functions, fn_T
and fn_Box
can have exactly the same code in the body, and all this thread is about that, when the code in both are the same.
We could also add this to the examples, a lot simpler:
trait A {}
fn fn_T<T: A>(j: Vec<T>) {
todo!()
}
fn fn_Box(j: Vec<Box<dyn A>>) {
todo!()
}
Well, we have several ways to define this functions, lets start with the differences:
A
, can all of them be different typesWith this in mind we could think that Box
is a lot better! but there is a trick...
Actually Box<dyn A>
can't be optimized, as we know, Box will request at first to all the data already exists on memory before doing any operation, because do not know where the data is, this causes that all operations using Box can not be optimized by the compiler.
While if we use Generics, the compiler can do a lot of better job making optimizations on the code! faster code, and loose some flexibility on the input. If we are able to use other types that can even skip Heap generics are very very powerful in performance, a lot more than Box
.
Lets do not go in the wrong side, this is not about how better or bad can be this alternatives, nor how can we implement them, the key point of what I wrote, is that each solution have its place, each one will have its advantages and disadvantages, and there can be other solutions, going back a little, the issue here is that we need to choose (Or I don't know how to mix both), if declare a function as a Box
or Generics, and we choose in our needs.
We can also even implement the algorithm two times using in one function Box
and in other Generics, which have the issue of duplicate all the code.
From what we have checked, in general Generics are fast, but more limited than Box
, so in functionality we can think of Generics as a subset of Box
, which relying on the title, trying to avoid duplicate title, would be nice be able to do something like this:
This really makes me think, because I don't know any type which based on Generics we could derive to Generics or Dynamic Dispatch using the input type that is known at compile time, yes we can know all this at compile time! so we could choose the best function to call with this!
The idea would be able to avoid duplicate code, and have one code which can be sent to different options, I don't know if this is solved, maybe there is already something and I don't know it.
Thx!
2 posts - 2 participants
🏷️ rust_feed