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
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//! impl From, TryFrom
use crate::{Strong, StrongBuf, Validator};
use std::{convert::TryFrom, str::FromStr};

impl<Ctx> TryFrom<String> for StrongBuf<Ctx>
where
    Ctx: Validator
{
    type Error = Ctx::Err;
    fn try_from(s: String) -> Result<Self, Self::Error> { Self::validate(s) }
}

impl<'a, Ctx> TryFrom<&'a str> for &'a Strong<Ctx>
where
    Ctx: Validator
{
    type Error = Ctx::Err;
    fn try_from(s: &'a str) -> Result<Self, Self::Error> { Strong::validate(s) }
}

impl<Ctx> FromStr for StrongBuf<Ctx>
where
    Ctx: Validator
{
    type Err = Ctx::Err;
    fn from_str(s: &str) -> Result<Self, Self::Err> { Self::validate(s.to_string()) }
}

impl<Ctx> AsRef<Strong<Ctx>> for StrongBuf<Ctx>
where
    Ctx: Validator
{
    fn as_ref(&self) -> &Strong<Ctx> { self }
}

fn _impl_from() {
    use std::{borrow::Cow, rc::Rc, sync::Arc};

    /// Needs allocation
    impl<Ctx> From<&Strong<Ctx>> for Box<Strong<Ctx>>
    where
        Ctx: Validator
    {
        fn from(s: &Strong<Ctx>) -> Self {
            let boxed: Box<str> = s.as_str().into();
            let rw = Box::into_raw(boxed) as *mut Strong<Ctx>;
            unsafe { Box::from_raw(rw) }
        }
    }

    impl<Ctx> From<Box<Strong<Ctx>>> for StrongBuf<Ctx>
    where
        Ctx: Validator
    {
        #[inline]
        fn from(boxed: Box<Strong<Ctx>>) -> StrongBuf<Ctx> {
            let rw = Box::into_raw(boxed) as *mut str;
            let inner: Box<str> = unsafe { Box::from_raw(rw) };
            unsafe { StrongBuf::no_validate(String::from(inner)) }
        }
    }

    impl<Ctx> From<StrongBuf<Ctx>> for Box<Strong<Ctx>>
    where
        Ctx: Validator
    {
        #[inline]
        fn from(s: StrongBuf<Ctx>) -> Self { s.into_boxed_strong() }
    }

    impl<Ctx> From<Cow<'_, Strong<Ctx>>> for Box<Strong<Ctx>>
    where
        Ctx: Validator
    {
        #[inline]
        fn from(cow: Cow<'_, Strong<Ctx>>) -> Box<Strong<Ctx>> {
            match cow {
                Cow::Borrowed(s) => Box::from(s),
                Cow::Owned(s) => Box::from(s)
            }
        }
    }

    impl<'a, Ctx> From<&'a Strong<Ctx>> for Cow<'a, Strong<Ctx>>
    where
        Ctx: Validator
    {
        #[inline]
        fn from(s: &'a Strong<Ctx>) -> Cow<'a, Strong<Ctx>> { Cow::Borrowed(s) }
    }

    impl<'a, Ctx> From<StrongBuf<Ctx>> for Cow<'a, Strong<Ctx>>
    where
        Ctx: Validator
    {
        #[inline]
        fn from(s: StrongBuf<Ctx>) -> Cow<'a, Strong<Ctx>> { Cow::Owned(s) }
    }

    impl<Ctx> From<StrongBuf<Ctx>> for Arc<Strong<Ctx>>
    where
        Ctx: Validator
    {
        #[inline]
        fn from(s: StrongBuf<Ctx>) -> Self {
            let a: Arc<str> = Arc::from(s.into_string());
            let rw = Arc::into_raw(a) as *const Strong<Ctx>;
            unsafe { Arc::from_raw(rw) }
        }
    }

    impl<Ctx> From<StrongBuf<Ctx>> for Rc<Strong<Ctx>>
    where
        Ctx: Validator
    {
        #[inline]
        fn from(s: StrongBuf<Ctx>) -> Self {
            let a: Rc<str> = Rc::from(s.into_string());
            let rw = Rc::into_raw(a) as *const Strong<Ctx>;
            unsafe { Rc::from_raw(rw) }
        }
    }

    /// Needs allocation
    impl<Ctx> From<&Strong<Ctx>> for Arc<Strong<Ctx>>
    where
        Ctx: Validator
    {
        fn from(s: &Strong<Ctx>) -> Self {
            let boxed: Arc<str> = s.as_str().into();
            let rw = Arc::into_raw(boxed) as *const Strong<Ctx>;
            unsafe { Arc::from_raw(rw) }
        }
    }

    /// Needs allocation
    impl<Ctx> From<&Strong<Ctx>> for Rc<Strong<Ctx>>
    where
        Ctx: Validator
    {
        fn from(s: &Strong<Ctx>) -> Self {
            let boxed: Rc<str> = s.as_str().into();
            let rw = Rc::into_raw(boxed) as *const Strong<Ctx>;
            unsafe { Rc::from_raw(rw) }
        }
    }

    // Needs allocation
    // 1460: impl<T: ?Sized + AsRef<OsStr>> From<&T> for PathBuf {
    // 1588: impl<'a> From<Cow<'a, Path>> for PathBuf {
}