summaryrefslogtreecommitdiffstats
path: root/third_party/rust/rental/tests/target_ty_hack.rs
blob: d98bd0d7b541fb97c3a693438dda2f034a7b7902 (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
43
44
45
46
47
48
49
50
51
52
53
54
#[macro_use]
extern crate rental;


type MyVec<T> = Vec<T>;


rental! {
	pub mod rent_vec_slice {
		use super::*;

		#[rental]
		pub struct OwnedSlice {
			#[target_ty = "[u8]"]
			buffer: MyVec<u8>,
			slice: &'buffer [u8],
		}

		#[rental_mut]
		pub struct OwnedMutSlice {
			#[target_ty = "[u8]"]
			buffer: MyVec<u8>,
			slice: &'buffer mut [u8],
		}
	}
}


#[test]
fn new() {
	let vec = vec![1, 2, 3];
	let _ = rent_vec_slice::OwnedSlice::new(vec, |slice| slice);
}


#[test]
fn read() {
	let vec = vec![1, 2, 3];
	let rvec = rent_vec_slice::OwnedSlice::new(vec, |slice| slice);

	assert_eq!(rvec.rent(|slice| slice[1]), 2);
	assert_eq!(rvec.rent(|slice| slice[1]), rvec.rent(|slice| slice[1]));
}


#[test]
fn write() {
	let vec = vec![1, 2, 3];
	let mut rvec = rent_vec_slice::OwnedMutSlice::new(vec, |slice| slice);

	rvec.rent_mut(|slice| slice[1] = 4);
	assert_eq!(rvec.rent(|slice| slice[1]), 4);
	assert_eq!(rvec.rent(|slice| slice[1]), rvec.rent(|slice| slice[1]));
}