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
73
74
75
76
77
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::fmt;

// Unfortunately Rust doesn't allow something like this:
//   impl< A, T: Trait< A > > AnotherTrait for T {}
// It will simply return `unconstrained type parameter` error.
//
// To work around this we create a dummy newtype which will
// artificially constraint the extra parameter.
#[doc(hidden)]
pub struct Newtype< A, T >( T, PhantomData< A > );

impl< A, T > Newtype< A, T > {
    #[doc(hidden)]
    #[inline]
    pub fn unwrap_newtype( self ) -> T {
        self.0
    }
}

#[doc(hidden)]
pub trait IntoNewtype< A >: Sized {
    fn into_newtype( self ) -> Newtype< A, Self >;
}

impl< A, T > IntoNewtype< A > for T {
    #[inline]
    fn into_newtype( self ) -> Newtype< A, Self > {
        Newtype( self, PhantomData )
    }
}

impl< A, T > Deref for Newtype< A, T > {
    type Target = T;

    #[inline]
    fn deref( &self ) -> &Self::Target {
        &self.0
    }
}

impl< A, T > DerefMut for Newtype< A, T > {
    #[inline]
    fn deref_mut( &mut self ) -> &mut Self::Target {
        &mut self.0
    }
}

impl< A, T: Copy > Copy for Newtype< A, T > {}
impl< A, T: Clone > Clone for Newtype< A, T > {
    #[inline]
    fn clone( &self ) -> Self {
        Newtype( self.0.clone(), PhantomData )
    }
}

impl< A, T > AsRef< T > for Newtype< A, T > {
    #[inline]
    fn as_ref( &self ) -> &T {
        &self.0
    }
}

impl< A, T > AsMut< T > for Newtype< A, T > {
    #[inline]
    fn as_mut( &mut self ) -> &mut T {
        &mut self.0
    }
}

impl< A, T: fmt::Debug > fmt::Debug for Newtype< A, T > {
    #[inline]
    fn fmt( &self, formatter: &mut fmt::Formatter ) -> Result< (), fmt::Error > {
        self.0.fmt( formatter )
    }
}