Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
[Feature Request] Dynamic dispatch should be allows as long as the plot is accepting the same data points #121
Comments
|
Hmm, this is something unsupported yet. You just spotted the root cause of this. The One way to address that (not only in Rust but also other programming languages) is using dynamic dispatch, which is, in Rust term, a trait object. However, Plotters doesn't have a trait for those two types, so the compiler don't know how to do that. One work around is to use static dispatch instead (use template function). Hope this is helpful, and dynamic dispatch on charts with same data type should be supported. Thanks again to come up with this problem, hopefully Plotters will support dynamic dispatch i the future release. Thanks for trying Plotters and hope you enjoy both the crate and Rust language. I am changing this issue to a feature request if you don't mind. Cheers! |
|
Many thanks for the reply! My solution so far was complete doubling of the plotting code, as you can see here: Not really nice, but good to know that my limited Rust experience was not the sole reason. And yes, I really enjoy Rust as well as plotters. |
Yep, you got that. To make it nicer, you can make duplicated code reusable with a template function. That is very often used trick in both Rust and C++ I believe. Since in C++ it's also not recommended to frequently use dynamic dispatch, or AKA virtual method functions. |
If the chart is defined to accept same data type, even though the axis is using different types of decorator, those chart types should be cvt into a trait object.
====
Original issue (one of valid use case)
First, many thanks for providing this great library!
Learning Rust for 2 weeks now and not really grasping the type system yet, this may be more of a Rust question than a plotters question.
I try to create a chart context with optional log-scaled y axis:
Now, my problem is that this doesn't compile due to:
When I use the condition in
build_rangedI get a type mismatch between
std::ops::Rangeandplotters::coord::logarithmic::LogRange.I understand that I probably have to specify an explicit type for
cc, probably with somedyn Traitin ChartContext's generics. However, due to my still very limited Rust experience, I can't get it to work.Many thanks for your help!