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
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;
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)
}
}
}
}