The "patching" word is kind of inaccurate but I have no better words, and this is not an actual problem in Rust (but in C#, it does.)
I just want to know whether it is possible to overcome such problem in case it happened with Rust.
Since C# has a very poor .ToString() support, I want to write a .ToDetailedString() manually, which
- Calls
.ToDetailedString()for some class that I want to know its parameter rather than just its default className - Calls
.ToString()as a fall-back to ensure we could got a string rather than an exception.
The problem is that, for all complex type that may contains the type I'm interested in, I have to implement a .ToDetailedString() for them, otherwise, since .ToString() call won't calling .ToDetailedString(), a fall-back .ToString() method won't show the detail of the interested data anymore.
In Rust, things seems very similar(except we already have a defailed output (with :?)), but I'm still curious about that, is it possible to write something that allow to modify an original trait Foo::bar that:
- calling a
.modified_bar()if it is implemented - calling a
.bar()as a fallback, and - if more .bar() is called within the fallback path, calling
.modified_bar()instead if it is implemented.
It might be possible if we could "patching" a trait:
trait BaseTrait {
fn foo(&self);
}
struct Patcher : patch BaseTrait {
// in case we want to send extra parameter
data: i32
}
impl Patcher for Ty for BaseTrait {
fn foo(&self, &base) {
base.foo(); // calling original foo
self.patch(|&mut this|{
base.foo() // calling base.foo, but in case Patcher call is available, calling the patched one.
});
}
}
Such patch might helpful for controlling the ident.