Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::fmt::Display;
- trait W {
- fn write(&mut self, s: &str);
- fn write_fmt<F: Display>(&mut self, f: &F) where Self: Sized {
- let s = f.to_string();
- self.write(&s);
- }
- }
- trait Combo: W {
- fn write_fmt_erased(&mut self, f: &Display);
- }
- impl<T: ?Sized + W> W for Box<T> {
- fn write(&mut self, s: &str) {
- (**self).write(s);
- }
- }
- struct Moo;
- impl W for Moo {
- fn write(&mut self, s: &str) {
- println!("moo: {}", s);
- }
- fn write_fmt<F: Display>(&mut self, f: &F) {
- println!("MOOOOOOO: {}", f);
- }
- }
- impl Combo for Moo {
- fn write_fmt_erased(&mut self, f: &Display) {
- println!("erased ->");
- self.write_fmt(&f);
- }
- }
- struct Cage {
- w: Box<Combo>,
- }
- impl W for Cage {
- fn write(&mut self, s: &str) {
- println!("caged!");
- self.w.write(s);
- }
- fn write_fmt<F: Display>(&mut self, f: &F) {
- println!("caged!");
- self.w.write_fmt_erased(f);
- }
- }
- fn main() {
- let mut moo = Moo;
- moo.write("hello");
- moo.write_fmt(&"sup");
- let mut cage = Cage { w: Box::new(moo) };
- cage.write("hello");
- cage.write_fmt(&"sup");
- }
Add Comment
Please, Sign In to add comment