Skip to content

Commit cb303c1

Browse files
committed
episode 14
1 parent 97c4248 commit cb303c1

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[workspace]
2-
members = ["./ownership-and-borrowing", "options-and-results", "errors", "iterators", "strings", "boxing"]
2+
members = ["./ownership-and-borrowing", "options-and-results", "errors", "iterators", "strings", "boxing", "conversions"]

conversions/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "conversions"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

conversions/src/main.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#![allow(unused)]
2+
3+
struct A;
4+
struct B;
5+
struct C;
6+
7+
impl From<B> for A {
8+
fn from(_: B) -> Self {
9+
A
10+
}
11+
}
12+
impl From<C> for A {
13+
fn from(_: C) -> Self {
14+
A
15+
}
16+
}
17+
18+
fn foo(a: impl Into<A>) {
19+
let a = a.into();
20+
}
21+
fn foo_b(a: B) {
22+
foo_inner(a.into())
23+
}
24+
fn foo_c(a: C) {
25+
foo_inner(a.into())
26+
}
27+
fn foo_inner(a: A) {
28+
// millions of lines
29+
}
30+
31+
enum Z {
32+
A,
33+
ParsedC,
34+
}
35+
36+
impl From<A> for Z {
37+
fn from(_: A) -> Self {
38+
Z::A
39+
}
40+
}
41+
42+
impl TryFrom<C> for Z {
43+
type Error = ();
44+
45+
fn try_from(value: C) -> Result<Self, Self::Error> {
46+
Err(())
47+
}
48+
}
49+
50+
fn main() {
51+
let a: A = B.into();
52+
let a = Into::<A>::into(B);
53+
let a = A::from(B);
54+
let a = A::from(C);
55+
let a: A = C.into();
56+
57+
let x: u32 = "8".parse().unwrap();
58+
let z: Z = A.into();
59+
let z: Z = A.try_into().unwrap();
60+
let z: Z = C.try_into().unwrap();
61+
62+
foo(B);
63+
foo(C);
64+
foo(A);
65+
}

0 commit comments

Comments
 (0)