]> git.lizzy.rs Git - rust.git/blob - tests/ui/fallible_impl_from.rs
Change to `TryFrom`
[rust.git] / tests / ui / fallible_impl_from.rs
1 #![deny(fallible_impl_from)]
2
3 // docs example
4 struct Foo(i32);
5 impl From<String> for Foo {
6     fn from(s: String) -> Self {
7         Foo(s.parse().unwrap())
8     }
9 }
10
11
12 struct Valid(Vec<u8>);
13
14 impl<'a> From<&'a str> for Valid {
15     fn from(s: &'a str) -> Valid {
16         Valid(s.to_owned().into_bytes())
17     }
18 }
19 impl From<usize> for Valid {
20     fn from(i: usize) -> Valid {
21         Valid(Vec::with_capacity(i))
22     }
23 }
24
25
26 struct Invalid;
27
28 impl From<usize> for Invalid {
29     fn from(i: usize) -> Invalid {
30         if i != 42 {
31             panic!();
32         }
33         Invalid
34     }
35 }
36
37 impl From<Option<String>> for Invalid {
38     fn from(s: Option<String>) -> Invalid {
39         let s = s.unwrap();
40         if !s.is_empty() {
41             panic!(42);
42         } else if s.parse::<u32>().unwrap() != 42 {
43             panic!("{:?}", s);
44         }
45         Invalid
46     }
47 }
48
49 trait ProjStrTrait {
50     type ProjString;
51 }
52 impl<T> ProjStrTrait for Box<T> {
53     type ProjString = String;
54 }
55 impl<'a> From<&'a mut <Box<u32> as ProjStrTrait>::ProjString> for Invalid {
56     fn from(s: &'a mut <Box<u32> as ProjStrTrait>::ProjString) -> Invalid {
57         if s.parse::<u32>().ok().unwrap() != 42 {
58             panic!("{:?}", s);
59         }
60         Invalid
61     }
62 }
63
64 fn main() {}