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
#![no_std]

extern crate void;

pub mod iter;

use void::Void;

pub trait TryClone: Sized {
    type Error;
    fn try_clone(&self) -> Result<Self, Self::Error>;
    fn try_clone_from(&mut self, other: &Self) -> Result<(), Self::Error> {
        other.try_clone().map(|new| *self = new)
    }
}

impl<T: Clone> TryClone for T {
    type Error = Void;

    #[inline(always)]
    fn try_clone(&self) -> Result<Self, Void> { Ok(self.clone()) }

    #[inline(always)]
    fn try_clone_from(&mut self, other: &Self) -> Result<(), Void> {
        Ok(self.clone_from(other))
    }
}

pub trait TryFrom<A>: Sized {
    type Error;
    fn try_from(A) -> Result<Self, Self::Error>;
}

impl<A, T: From<A>> TryFrom<A> for T {
    type Error = Void;

    #[inline(always)]
    fn try_from(a: A) -> Result<Self, Self::Error> { Ok(Self::from(a)) }
}

pub trait TryInto<A> {
    type Error;
    fn try_into(self) -> Result<A, Self::Error>;
}

impl<A: TryFrom<T>, T> TryInto<A> for T {
    type Error = A::Error;

    #[inline(always)]
    fn try_into(self) -> Result<A, Self::Error> { A::try_from(self) }
}