Composition operator and pipe forward operator in Rust
use std::ops::Shr;
struct Wrapped<T>(T);
impl<A, B, F> Shr<F> for Wrapped<A>
where
F: FnOnce(A) -> B,
{
type Output = Wrapped<B>;
fn shr(self, f: F) -> Wrapped<B> {
Wrapped(f(self.0))
}
}
fn main() {
let string = Wrapped(1) >> (|x| x + 1) >> (|x| 2 * x) >> (|x: i32| x.to_string());
println!("{}", string.0);
}
// prints `4`
Here's a code of pipeline operator for struct: Wrapped
by overloading an operator, but I need one that can be used for native values that I think as &dyn Any
.
Since I don't understand type system of Rust yet, so I did like
use std::any::Any;
impl<A, B, F: Fn(A) -> B> Shr<F> for &dyn Any {
type Output = &dyn Any;
fn shr(self, f: F) -> &dyn Any {
f(self.0)
}
}
but with obvious errors.
How can I sort this out? Thanks.