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
|
#![allow(dead_code)]
#[macro_use]
extern crate rental;
use std::cell::RefCell;
use std::rc::Rc;
pub struct Foo {
i: i32,
d: Rc<RefCell<String>>,
}
pub struct Bar<'a> {
foo: &'a Foo,
d: Rc<RefCell<String>>,
}
pub struct Baz<'a: 'b, 'b> {
bar: &'b Bar<'a>,
d: Rc<RefCell<String>>,
}
pub struct Qux<'a: 'b, 'b: 'c, 'c> {
baz: &'c Baz<'a, 'b>,
d: Rc<RefCell<String>>,
}
pub struct Xyzzy<'a: 'b, 'b: 'c, 'c: 'd, 'd> {
qux: &'d Qux<'a, 'b, 'c>,
d: Rc<RefCell<String>>,
}
impl Foo {
pub fn borrow<'a>(&'a self) -> Bar<'a> { Bar { foo: self, d: self.d.clone() } }
}
impl Drop for Foo {
fn drop(&mut self) {
self.d.borrow_mut().push_str("Foo");
}
}
impl<'a> Bar<'a> {
pub fn borrow<'b>(&'b self) -> Baz<'a, 'b> { Baz { bar: self, d: self.d.clone() } }
}
impl<'a> Drop for Bar<'a> {
fn drop(&mut self) {
self.d.borrow_mut().push_str("Bar");
}
}
impl<'a: 'b, 'b> Baz<'a, 'b> {
pub fn borrow<'c>(&'c self) -> Qux<'a, 'b, 'c> { Qux { baz: self, d: self.d.clone() } }
}
impl<'a: 'b, 'b> Drop for Baz<'a, 'b> {
fn drop(&mut self) {
self.d.borrow_mut().push_str("Baz");
}
}
impl<'a: 'b, 'b: 'c, 'c> Qux<'a, 'b, 'c> {
pub fn borrow<'d>(&'d self) -> Xyzzy<'a, 'b, 'c, 'd> { Xyzzy { qux: self, d: self.d.clone() } }
}
impl<'a: 'b, 'b: 'c, 'c> Drop for Qux<'a, 'b, 'c> {
fn drop(&mut self) {
self.d.borrow_mut().push_str("Qux");
}
}
impl<'a: 'b, 'b: 'c, 'c: 'd, 'd> Drop for Xyzzy<'a, 'b, 'c, 'd> {
fn drop(&mut self) {
self.d.borrow_mut().push_str("Xyzzy");
}
}
rental! {
pub mod rentals {
use super::*;
#[rental]
pub struct DropTestRent {
foo: Box<Foo>,
bar: Box<Bar<'foo>>,
baz: Box<Baz<'foo, 'bar>>,
qux: Box<Qux<'foo, 'bar, 'baz>>,
xyzzy: Xyzzy<'foo, 'bar, 'baz, 'qux>,
}
}
}
#[test]
fn drop_order() {
let d = Rc::new(RefCell::new(String::new()));
{
let foo = Foo { i: 5, d: d.clone() };
let _ = rentals::DropTestRent::new(
Box::new(foo),
|foo| Box::new(foo.borrow()),
|bar, _| Box::new(bar.borrow()),
|baz, _, _| Box::new(baz.borrow()),
|qux, _, _, _| qux.borrow()
);
}
assert_eq!(*d.borrow(), "XyzzyQuxBazBarFoo");
let d = Rc::new(RefCell::new(String::new()));
{
let foo = Foo { i: 5, d: d.clone() };
let r = rentals::DropTestRent::new(
Box::new(foo),
|foo| Box::new(foo.borrow()),
|bar, _| Box::new(bar.borrow()),
|baz, _, _| Box::new(baz.borrow()),
|qux, _, _, _| qux.borrow()
);
let _head = r.into_head();
}
assert_eq!(*d.borrow(), "XyzzyQuxBazBarFoo");
}
|