blob: 3ef3b94810de8052c1e562551c6a38ca5445a53f (
plain)
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
|
#[macro_use]
extern crate rental;
pub struct Foo {
i: i32,
}
rental! {
mod rentals {
use super::*;
#[rental(map_suffix = "T")]
pub struct SimpleRef<T: 'static> {
foo: Box<Foo>,
fr: &'foo T,
}
#[rental_mut(map_suffix = "T")]
pub struct SimpleMut<T: 'static> {
foo: Box<Foo>,
fr: &'foo mut T,
}
}
}
#[test]
fn map() {
let foo = Foo { i: 5 };
let sr = rentals::SimpleRef::new(Box::new(foo), |foo| foo);
let sr = sr.map(|fr| &fr.i);
assert_eq!(sr.rent(|ir| **ir), 5);
let foo = Foo { i: 12 };
let sm = rentals::SimpleMut::new(Box::new(foo), |foo| foo);
let sm = sm.map(|fr| &mut fr.i);
assert_eq!(sm.rent(|ir| **ir), 12);
}
|