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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use super::*;
use std::borrow::Cow;
use std::{ffi::{OsStr,OsString}, path::*};

pub trait Caption<'w> {
    fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's;
}

impl<'w> Caption<'w> for str {
    fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's {
        Cow::Borrowed(self)
    }
}
impl<'w> Caption<'w> for String {
    fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's {
        Cow::Borrowed(self)
    }
}

impl<'w> Caption<'w> for Path {
    fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's {
        self.to_string_lossy()
    }
}
impl<'w> Caption<'w> for PathBuf {
    fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's {
        self.to_string_lossy()
    }
}

impl<'w> Caption<'w> for OsStr {
    fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's {
        self.to_string_lossy()
    }
}
impl<'w> Caption<'w> for OsString {
    fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's {
        self.to_string_lossy()
    }
}

impl<'w,'l,T> Caption<'w> for &'w T where T: Caption<'l>+?Sized, 'l: 'w {
    fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's {
        (**self).caption()
    }
}
impl<'w,'l,T> Caption<'w> for &'w mut T where T: Caption<'l>+?Sized, 'l: 'w {
    fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's {
        (**self).caption()
    }
}

macro_rules! impl_caption_gen {
    ($t:ty;$($tt:ty);+) => {
        impl_caption_gen!($t);
        impl_caption_gen!($($tt);*);
    };
    ($t:ty) => {
        impl<'w> Caption<'w> for $t {
            fn caption<'s>(&'s self) -> Cow<'s,str> where 'w: 's {
                Cow::Owned(self.to_string())
            }
        }
    }
}

impl_caption_gen!(
    bool;char;
    f32;f64;
    i8;i16;i32;i64;i128;isize;
    u8;u16;u32;u64;u128;usize
);