1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::fmt::{self, Display};

use Output;

pub const EMPTY_STR: &'static &'static str = &"";

pub enum OutImpl<'a> {
    Borrow(&'a (Display + 'a)),
    Owned(Box<Display + 'a>),
}

impl<'a, T: Display + 'a> From<&'a T> for Output<'a> {
    fn from(t: &'a T) -> Output<'a> {
        Output(OutImpl::Borrow(t))
    }
}

impl<'a> Output<'a> {
    /// Create an owned value for output
    ///
    /// Usually you should use a reference like `self.into()`, but sometimes
    /// something more complex may be returned using `Output::owned(value)`
    pub fn owned<T: Display + 'a>(t: T) -> Output<'a> {
        Output(OutImpl::Owned(Box::new(t)))
    }
    /// Returns an empty output
    pub fn empty() -> Output<'a> {
        Output(OutImpl::Borrow(EMPTY_STR))
    }
}

impl<'a> fmt::Display for OutImpl<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            OutImpl::Borrow(x) => x.fmt(f),
            OutImpl::Owned(ref x) => x.fmt(f),
        }
    }
}