Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=edd0ab90ff3c482b86a21ff9e06277d1
- mod ext {
- use std::fmt::Debug;
- use std::fmt;
- pub trait Trait: Debug {
- fn write_t(&self, _f: &mut fmt::Formatter) -> Result<(), fmt::Error> { Ok(()) }
- }
- #[derive(Debug, Clone, Copy)]
- pub struct T0;
- impl Trait for T0 {
- fn write_t(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- f.write_str("T0")
- }
- }
- #[derive(Debug, Clone, Copy)]
- pub struct T1;
- impl Trait for T1 {
- fn write_t(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- f.write_str("T1")
- }
- }
- #[derive(Debug, Clone, Copy)]
- pub struct Wrap<E: Trait>(pub E);
- }
- mod main {
- use crate::ext;
- use std::fmt;
- impl<T> ext::Trait for Box<T> where T: ext::Trait + ?Sized {
- fn write_t(&self, _f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- // self.write_t(f)
- // TODO
- Ok(())
- }
- }
- pub fn test(flag: bool) {
- let b: Box<dyn ext::Trait> = if flag {
- Box::new(ext::T0)
- } else {
- Box::new(ext::T1)
- };
- let w: ext::Wrap<Box<dyn ext::Trait>> = ext::Wrap(b);
- println!("{:?}", w);
- }
- }
- fn main() {
- main::test(false);
- main::test(true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement