summaryrefslogtreecommitdiffstats
path: root/headers/src/convert.rs
blob: c076cfd8624d6ed6ac2c4718db7cee468f0905e3 (plain)
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
//TODO potentially move HeaderTryFrom to `mail-headers`
use error::ComponentCreationError;

//TODO replace with std TryFrom once it is stable
// (either a hard replace, or a soft replace which implements HeaderTryFrom if TryFrom exist)
/// Workaround for `TryFrom`,`TryInto` not being stable.
pub trait HeaderTryFrom<T>: Sized {
    fn try_from(val: T) -> Result<Self, ComponentCreationError>;
}

/// Workaround for `TryFrom`,`TryInto` not being stable.
pub trait HeaderTryInto<T>: Sized {
    fn try_into(self) -> Result<T, ComponentCreationError>;
}

impl<F, T> HeaderTryInto<T> for F
where
    T: HeaderTryFrom<F>,
{
    fn try_into(self) -> Result<T, ComponentCreationError> {
        T::try_from(self)
    }
}

impl<T> HeaderTryFrom<T> for T {
    fn try_from(val: T) -> Result<Self, ComponentCreationError> {
        Ok(val)
    }
}

// It is not possible to auto-implement HeaderTryFrom for From/Into as
// this will make new HeaderTryFrom implementations outside of this care
// nearly impossible making the trait partially useless
//
//impl<T, F> HeaderTryFrom<F> for T where F: Into<T> {
//    fn try_from(val: F) -> Result<T, Error> {
//        Ok( val.into() )
//    }
//}