-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmutable_manual.rs
83 lines (68 loc) · 1.91 KB
/
mutable_manual.rs
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
trait MyIface {
fn foo(&mut self);
fn bar(&mut self, x: i32);
fn baz(&mut self, y: String, z: Vec<u8>);
}
struct Implementor {}
impl MyIface for Implementor {
fn foo(&mut self) {
dbg!("foo");
}
fn bar(&mut self, x: i32) {
dbg!("bar", x);
}
fn baz(&mut self, y: String, z: Vec<u8>) {
dbg!("baz", y, z);
}
}
// Begin of the part which is supposed to be auto-generated
enum MyIfaceEnum {
Foo,
Bar { x: i32 },
Baz { y: String, z: Vec<u8> },
}
impl MyIfaceEnum {
fn call_mut<I: MyIface>(self, o: &mut I) {
match self {
MyIfaceEnum::Foo => o.foo(),
MyIfaceEnum::Bar { x } => o.bar(x),
MyIfaceEnum::Baz { y, z } => o.baz(y, z),
}
}
}
trait MyIfaceResultified<E> {
fn try_foo(&mut self) -> Result<(), E>;
fn try_bar(&mut self, x: i32) -> Result<(), E>;
fn try_baz(&mut self, y: String, z: Vec<u8>) -> Result<(), E>;
}
impl<R:MyIfaceResultified<std::convert::Infallible>> MyIface for R {
fn foo(&mut self) {
R::try_foo(self).unwrap()
}
fn bar(&mut self, x: i32) {
R::try_bar(self, x).unwrap()
}
fn baz(&mut self, y: String, z: Vec<u8>) {
R::try_baz(self,y,z).unwrap()
}
}
struct MyIfaceProxy<E, F: FnMut(MyIfaceEnum)-> Result<(), E> > (F);
impl<E, F: FnMut(MyIfaceEnum) -> Result<(), E>> MyIfaceResultified<E> for MyIfaceProxy<E, F> {
fn try_foo(&mut self) -> Result<(), E> {
self.0(MyIfaceEnum::Foo)
}
fn try_bar(&mut self, x: i32) -> Result<(), E> {
self.0(MyIfaceEnum::Bar { x })
}
fn try_baz(&mut self, y: String, z: Vec<u8>) -> Result<(), E> {
self.0(MyIfaceEnum::Baz { y, z })
}
}
// End of the part which is supposed to be auto-generated
#[test]
fn test() {
let mut o = Implementor {};
let mut p = MyIfaceProxy::<std::convert::Infallible,_>(|c| Ok(c.call_mut(&mut o)));
p.foo();
p.bar(4);
}