]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_data_structures/src/owning_ref/tests.rs
Merge commit '8da837185714cefbb261e93e9846afb11c1dc60e' into sync-rustfmt-subtree
[rust.git] / compiler / rustc_data_structures / src / owning_ref / tests.rs
1 mod owning_ref {
2     use super::super::OwningRef;
3     use super::super::{BoxRef, Erased, ErasedBoxRef, RcRef};
4     use std::cmp::{Ord, Ordering, PartialEq, PartialOrd};
5     use std::collections::hash_map::DefaultHasher;
6     use std::collections::HashMap;
7     use std::hash::{Hash, Hasher};
8     use std::rc::Rc;
9
10     #[derive(Debug, PartialEq)]
11     struct Example(u32, String, [u8; 3]);
12     fn example() -> Example {
13         Example(42, "hello world".to_string(), [1, 2, 3])
14     }
15
16     #[test]
17     fn new_deref() {
18         let or: OwningRef<Box<()>, ()> = OwningRef::new(Box::new(()));
19         assert_eq!(&*or, &());
20     }
21
22     #[test]
23     fn into() {
24         let or: OwningRef<Box<()>, ()> = Box::new(()).into();
25         assert_eq!(&*or, &());
26     }
27
28     #[test]
29     fn map_offset_ref() {
30         let or: BoxRef<Example> = Box::new(example()).into();
31         let or: BoxRef<_, u32> = or.map(|x| &x.0);
32         assert_eq!(&*or, &42);
33
34         let or: BoxRef<Example> = Box::new(example()).into();
35         let or: BoxRef<_, u8> = or.map(|x| &x.2[1]);
36         assert_eq!(&*or, &2);
37     }
38
39     #[test]
40     fn map_heap_ref() {
41         let or: BoxRef<Example> = Box::new(example()).into();
42         let or: BoxRef<_, str> = or.map(|x| &x.1[..5]);
43         assert_eq!(&*or, "hello");
44     }
45
46     #[test]
47     fn map_static_ref() {
48         let or: BoxRef<()> = Box::new(()).into();
49         let or: BoxRef<_, str> = or.map(|_| "hello");
50         assert_eq!(&*or, "hello");
51     }
52
53     #[test]
54     fn map_chained() {
55         let or: BoxRef<String> = Box::new(example().1).into();
56         let or: BoxRef<_, str> = or.map(|x| &x[1..5]);
57         let or: BoxRef<_, str> = or.map(|x| &x[..2]);
58         assert_eq!(&*or, "el");
59     }
60
61     #[test]
62     fn map_chained_inference() {
63         let or = BoxRef::new(Box::new(example().1)).map(|x| &x[..5]).map(|x| &x[1..3]);
64         assert_eq!(&*or, "el");
65     }
66
67     #[test]
68     fn owner() {
69         let or: BoxRef<String> = Box::new(example().1).into();
70         let or = or.map(|x| &x[..5]);
71         assert_eq!(&*or, "hello");
72         assert_eq!(&**or.owner(), "hello world");
73     }
74
75     #[test]
76     fn into_inner() {
77         let or: BoxRef<String> = Box::new(example().1).into();
78         let or = or.map(|x| &x[..5]);
79         assert_eq!(&*or, "hello");
80         let s = *or.into_inner();
81         assert_eq!(&s, "hello world");
82     }
83
84     #[test]
85     fn fmt_debug() {
86         let or: BoxRef<String> = Box::new(example().1).into();
87         let or = or.map(|x| &x[..5]);
88         let s = format!("{:?}", or);
89         assert_eq!(&s, "OwningRef { owner: \"hello world\", reference: \"hello\" }");
90     }
91
92     #[test]
93     fn erased_owner() {
94         let o1: BoxRef<Example, str> = BoxRef::new(Box::new(example())).map(|x| &x.1[..]);
95
96         let o2: BoxRef<String, str> = BoxRef::new(Box::new(example().1)).map(|x| &x[..]);
97
98         let os: Vec<ErasedBoxRef<str>> = vec![o1.erase_owner(), o2.erase_owner()];
99         assert!(os.iter().all(|e| &e[..] == "hello world"));
100     }
101
102     #[test]
103     fn raii_locks() {
104         use super::super::{MutexGuardRef, RwLockReadGuardRef, RwLockWriteGuardRef};
105         use super::super::{RefMutRef, RefRef};
106         use std::cell::RefCell;
107         use std::sync::{Mutex, RwLock};
108
109         {
110             let a = RefCell::new(1);
111             let a = {
112                 let a = RefRef::new(a.borrow());
113                 assert_eq!(*a, 1);
114                 a
115             };
116             assert_eq!(*a, 1);
117             drop(a);
118         }
119         {
120             let a = RefCell::new(1);
121             let a = {
122                 let a = RefMutRef::new(a.borrow_mut());
123                 assert_eq!(*a, 1);
124                 a
125             };
126             assert_eq!(*a, 1);
127             drop(a);
128         }
129         {
130             let a = Mutex::new(1);
131             let a = {
132                 let a = MutexGuardRef::new(a.lock().unwrap());
133                 assert_eq!(*a, 1);
134                 a
135             };
136             assert_eq!(*a, 1);
137             drop(a);
138         }
139         {
140             let a = RwLock::new(1);
141             let a = {
142                 let a = RwLockReadGuardRef::new(a.read().unwrap());
143                 assert_eq!(*a, 1);
144                 a
145             };
146             assert_eq!(*a, 1);
147             drop(a);
148         }
149         {
150             let a = RwLock::new(1);
151             let a = {
152                 let a = RwLockWriteGuardRef::new(a.write().unwrap());
153                 assert_eq!(*a, 1);
154                 a
155             };
156             assert_eq!(*a, 1);
157             drop(a);
158         }
159     }
160
161     #[test]
162     fn eq() {
163         let or1: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
164         let or2: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
165         assert_eq!(or1.eq(&or2), true);
166     }
167
168     #[test]
169     fn cmp() {
170         let or1: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
171         let or2: BoxRef<[u8]> = BoxRef::new(vec![4, 5, 6].into_boxed_slice());
172         assert_eq!(or1.cmp(&or2), Ordering::Less);
173     }
174
175     #[test]
176     fn partial_cmp() {
177         let or1: BoxRef<[u8]> = BoxRef::new(vec![4, 5, 6].into_boxed_slice());
178         let or2: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
179         assert_eq!(or1.partial_cmp(&or2), Some(Ordering::Greater));
180     }
181
182     #[test]
183     fn hash() {
184         let mut h1 = DefaultHasher::new();
185         let mut h2 = DefaultHasher::new();
186
187         let or1: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
188         let or2: BoxRef<[u8]> = BoxRef::new(vec![1, 2, 3].into_boxed_slice());
189
190         or1.hash(&mut h1);
191         or2.hash(&mut h2);
192
193         assert_eq!(h1.finish(), h2.finish());
194     }
195
196     #[test]
197     fn borrow() {
198         let mut hash = HashMap::new();
199         let key = RcRef::<String>::new(Rc::new("foo-bar".to_string())).map(|s| &s[..]);
200
201         hash.insert(key.clone().map(|s| &s[..3]), 42);
202         hash.insert(key.clone().map(|s| &s[4..]), 23);
203
204         assert_eq!(hash.get("foo"), Some(&42));
205         assert_eq!(hash.get("bar"), Some(&23));
206     }
207
208     #[test]
209     fn total_erase() {
210         let a: OwningRef<Vec<u8>, [u8]> = OwningRef::new(vec![]).map(|x| &x[..]);
211         let b: OwningRef<Box<[u8]>, [u8]> =
212             OwningRef::new(vec![].into_boxed_slice()).map(|x| &x[..]);
213
214         let c: OwningRef<Rc<Vec<u8>>, [u8]> = unsafe { a.map_owner(Rc::new) };
215         let d: OwningRef<Rc<Box<[u8]>>, [u8]> = unsafe { b.map_owner(Rc::new) };
216
217         let e: OwningRef<Rc<dyn Erased>, [u8]> = c.erase_owner();
218         let f: OwningRef<Rc<dyn Erased>, [u8]> = d.erase_owner();
219
220         let _g = e.clone();
221         let _h = f.clone();
222     }
223
224     #[test]
225     fn total_erase_box() {
226         let a: OwningRef<Vec<u8>, [u8]> = OwningRef::new(vec![]).map(|x| &x[..]);
227         let b: OwningRef<Box<[u8]>, [u8]> =
228             OwningRef::new(vec![].into_boxed_slice()).map(|x| &x[..]);
229
230         let c: OwningRef<Box<Vec<u8>>, [u8]> = a.map_owner_box();
231         let d: OwningRef<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
232
233         let _e: OwningRef<Box<dyn Erased>, [u8]> = c.erase_owner();
234         let _f: OwningRef<Box<dyn Erased>, [u8]> = d.erase_owner();
235     }
236
237     #[test]
238     fn try_map1() {
239         use std::any::Any;
240
241         let x = Box::new(123_i32);
242         let y: Box<dyn Any> = x;
243
244         assert!(OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_ok());
245     }
246
247     #[test]
248     fn try_map2() {
249         use std::any::Any;
250
251         let x = Box::new(123_i32);
252         let y: Box<dyn Any> = x;
253
254         assert!(!OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_err());
255     }
256 }
257
258 mod owning_handle {
259     use super::super::OwningHandle;
260     use super::super::RcRef;
261     use std::cell::RefCell;
262     use std::rc::Rc;
263     use std::sync::Arc;
264     use std::sync::RwLock;
265
266     #[test]
267     fn owning_handle() {
268         use std::cell::RefCell;
269         let cell = Rc::new(RefCell::new(2));
270         let cell_ref = RcRef::new(cell);
271         let mut handle =
272             OwningHandle::new_with_fn(cell_ref, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
273         assert_eq!(*handle, 2);
274         *handle = 3;
275         assert_eq!(*handle, 3);
276     }
277
278     #[test]
279     fn try_owning_handle_ok() {
280         use std::cell::RefCell;
281         let cell = Rc::new(RefCell::new(2));
282         let cell_ref = RcRef::new(cell);
283         let mut handle = OwningHandle::try_new::<_, ()>(cell_ref, |x| {
284             Ok(unsafe { x.as_ref() }.unwrap().borrow_mut())
285         })
286         .unwrap();
287         assert_eq!(*handle, 2);
288         *handle = 3;
289         assert_eq!(*handle, 3);
290     }
291
292     #[test]
293     fn try_owning_handle_err() {
294         use std::cell::RefCell;
295         let cell = Rc::new(RefCell::new(2));
296         let cell_ref = RcRef::new(cell);
297         let handle = OwningHandle::try_new::<_, ()>(cell_ref, |x| {
298             if false {
299                 return Ok(unsafe { x.as_ref() }.unwrap().borrow_mut());
300             }
301             Err(())
302         });
303         assert!(handle.is_err());
304     }
305
306     #[test]
307     fn nested() {
308         use std::cell::RefCell;
309         use std::sync::{Arc, RwLock};
310
311         let result = {
312             let complex = Rc::new(RefCell::new(Arc::new(RwLock::new("someString"))));
313             let curr = RcRef::new(complex);
314             let curr =
315                 OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
316             let mut curr = OwningHandle::new_with_fn(curr, |x| {
317                 unsafe { x.as_ref() }.unwrap().try_write().unwrap()
318             });
319             assert_eq!(*curr, "someString");
320             *curr = "someOtherString";
321             curr
322         };
323         assert_eq!(*result, "someOtherString");
324     }
325
326     #[test]
327     fn owning_handle_safe() {
328         use std::cell::RefCell;
329         let cell = Rc::new(RefCell::new(2));
330         let cell_ref = RcRef::new(cell);
331         let handle = OwningHandle::new(cell_ref);
332         assert_eq!(*handle, 2);
333     }
334
335     #[test]
336     fn owning_handle_mut_safe() {
337         use std::cell::RefCell;
338         let cell = Rc::new(RefCell::new(2));
339         let cell_ref = RcRef::new(cell);
340         let mut handle = OwningHandle::new_mut(cell_ref);
341         assert_eq!(*handle, 2);
342         *handle = 3;
343         assert_eq!(*handle, 3);
344     }
345
346     #[test]
347     fn owning_handle_safe_2() {
348         let result = {
349             let complex = Rc::new(RefCell::new(Arc::new(RwLock::new("someString"))));
350             let curr = RcRef::new(complex);
351             let curr =
352                 OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
353             let mut curr = OwningHandle::new_with_fn(curr, |x| {
354                 unsafe { x.as_ref() }.unwrap().try_write().unwrap()
355             });
356             assert_eq!(*curr, "someString");
357             *curr = "someOtherString";
358             curr
359         };
360         assert_eq!(*result, "someOtherString");
361     }
362 }
363
364 mod owning_ref_mut {
365     use super::super::BoxRef;
366     use super::super::{BoxRefMut, Erased, ErasedBoxRefMut, OwningRefMut};
367     use std::cmp::{Ord, Ordering, PartialEq, PartialOrd};
368     use std::collections::hash_map::DefaultHasher;
369     use std::collections::HashMap;
370     use std::hash::{Hash, Hasher};
371
372     #[derive(Debug, PartialEq)]
373     struct Example(u32, String, [u8; 3]);
374     fn example() -> Example {
375         Example(42, "hello world".to_string(), [1, 2, 3])
376     }
377
378     #[test]
379     fn new_deref() {
380         let or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
381         assert_eq!(&*or, &());
382     }
383
384     #[test]
385     fn new_deref_mut() {
386         let mut or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
387         assert_eq!(&mut *or, &mut ());
388     }
389
390     #[test]
391     fn mutate() {
392         let mut or: OwningRefMut<Box<usize>, usize> = OwningRefMut::new(Box::new(0));
393         assert_eq!(&*or, &0);
394         *or = 1;
395         assert_eq!(&*or, &1);
396     }
397
398     #[test]
399     fn into() {
400         let or: OwningRefMut<Box<()>, ()> = Box::new(()).into();
401         assert_eq!(&*or, &());
402     }
403
404     #[test]
405     fn map_offset_ref() {
406         let or: BoxRefMut<Example> = Box::new(example()).into();
407         let or: BoxRef<_, u32> = or.map(|x| &mut x.0);
408         assert_eq!(&*or, &42);
409
410         let or: BoxRefMut<Example> = Box::new(example()).into();
411         let or: BoxRef<_, u8> = or.map(|x| &mut x.2[1]);
412         assert_eq!(&*or, &2);
413     }
414
415     #[test]
416     fn map_heap_ref() {
417         let or: BoxRefMut<Example> = Box::new(example()).into();
418         let or: BoxRef<_, str> = or.map(|x| &mut x.1[..5]);
419         assert_eq!(&*or, "hello");
420     }
421
422     #[test]
423     fn map_static_ref() {
424         let or: BoxRefMut<()> = Box::new(()).into();
425         let or: BoxRef<_, str> = or.map(|_| "hello");
426         assert_eq!(&*or, "hello");
427     }
428
429     #[test]
430     fn map_mut_offset_ref() {
431         let or: BoxRefMut<Example> = Box::new(example()).into();
432         let or: BoxRefMut<_, u32> = or.map_mut(|x| &mut x.0);
433         assert_eq!(&*or, &42);
434
435         let or: BoxRefMut<Example> = Box::new(example()).into();
436         let or: BoxRefMut<_, u8> = or.map_mut(|x| &mut x.2[1]);
437         assert_eq!(&*or, &2);
438     }
439
440     #[test]
441     fn map_mut_heap_ref() {
442         let or: BoxRefMut<Example> = Box::new(example()).into();
443         let or: BoxRefMut<_, str> = or.map_mut(|x| &mut x.1[..5]);
444         assert_eq!(&*or, "hello");
445     }
446
447     #[test]
448     fn map_mut_static_ref() {
449         static mut MUT_S: [u8; 5] = *b"hello";
450
451         let mut_s: &'static mut [u8] = unsafe { &mut MUT_S };
452
453         let or: BoxRefMut<()> = Box::new(()).into();
454         let or: BoxRefMut<_, [u8]> = or.map_mut(move |_| mut_s);
455         assert_eq!(&*or, b"hello");
456     }
457
458     #[test]
459     fn map_mut_chained() {
460         let or: BoxRefMut<String> = Box::new(example().1).into();
461         let or: BoxRefMut<_, str> = or.map_mut(|x| &mut x[1..5]);
462         let or: BoxRefMut<_, str> = or.map_mut(|x| &mut x[..2]);
463         assert_eq!(&*or, "el");
464     }
465
466     #[test]
467     fn map_chained_inference() {
468         let or = BoxRefMut::new(Box::new(example().1))
469             .map_mut(|x| &mut x[..5])
470             .map_mut(|x| &mut x[1..3]);
471         assert_eq!(&*or, "el");
472     }
473
474     #[test]
475     fn try_map_mut() {
476         let or: BoxRefMut<String> = Box::new(example().1).into();
477         let or: Result<BoxRefMut<_, str>, ()> = or.try_map_mut(|x| Ok(&mut x[1..5]));
478         assert_eq!(&*or.unwrap(), "ello");
479
480         let or: BoxRefMut<String> = Box::new(example().1).into();
481         let or: Result<BoxRefMut<_, str>, ()> = or.try_map_mut(|_| Err(()));
482         assert!(or.is_err());
483     }
484
485     #[test]
486     fn owner() {
487         let or: BoxRefMut<String> = Box::new(example().1).into();
488         let or = or.map_mut(|x| &mut x[..5]);
489         assert_eq!(&*or, "hello");
490         assert_eq!(&**or.owner(), "hello world");
491     }
492
493     #[test]
494     fn into_inner() {
495         let or: BoxRefMut<String> = Box::new(example().1).into();
496         let or = or.map_mut(|x| &mut x[..5]);
497         assert_eq!(&*or, "hello");
498         let s = *or.into_inner();
499         assert_eq!(&s, "hello world");
500     }
501
502     #[test]
503     fn fmt_debug() {
504         let or: BoxRefMut<String> = Box::new(example().1).into();
505         let or = or.map_mut(|x| &mut x[..5]);
506         let s = format!("{:?}", or);
507         assert_eq!(&s, "OwningRefMut { owner: \"hello world\", reference: \"hello\" }");
508     }
509
510     #[test]
511     fn erased_owner() {
512         let o1: BoxRefMut<Example, str> =
513             BoxRefMut::new(Box::new(example())).map_mut(|x| &mut x.1[..]);
514
515         let o2: BoxRefMut<String, str> =
516             BoxRefMut::new(Box::new(example().1)).map_mut(|x| &mut x[..]);
517
518         let os: Vec<ErasedBoxRefMut<str>> = vec![o1.erase_owner(), o2.erase_owner()];
519         assert!(os.iter().all(|e| &e[..] == "hello world"));
520     }
521
522     #[test]
523     fn raii_locks() {
524         use super::super::RefMutRefMut;
525         use super::super::{MutexGuardRefMut, RwLockWriteGuardRefMut};
526         use std::cell::RefCell;
527         use std::sync::{Mutex, RwLock};
528
529         {
530             let a = RefCell::new(1);
531             let a = {
532                 let a = RefMutRefMut::new(a.borrow_mut());
533                 assert_eq!(*a, 1);
534                 a
535             };
536             assert_eq!(*a, 1);
537             drop(a);
538         }
539         {
540             let a = Mutex::new(1);
541             let a = {
542                 let a = MutexGuardRefMut::new(a.lock().unwrap());
543                 assert_eq!(*a, 1);
544                 a
545             };
546             assert_eq!(*a, 1);
547             drop(a);
548         }
549         {
550             let a = RwLock::new(1);
551             let a = {
552                 let a = RwLockWriteGuardRefMut::new(a.write().unwrap());
553                 assert_eq!(*a, 1);
554                 a
555             };
556             assert_eq!(*a, 1);
557             drop(a);
558         }
559     }
560
561     #[test]
562     fn eq() {
563         let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
564         let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
565         assert_eq!(or1.eq(&or2), true);
566     }
567
568     #[test]
569     fn cmp() {
570         let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
571         let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![4, 5, 6].into_boxed_slice());
572         assert_eq!(or1.cmp(&or2), Ordering::Less);
573     }
574
575     #[test]
576     fn partial_cmp() {
577         let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![4, 5, 6].into_boxed_slice());
578         let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
579         assert_eq!(or1.partial_cmp(&or2), Some(Ordering::Greater));
580     }
581
582     #[test]
583     fn hash() {
584         let mut h1 = DefaultHasher::new();
585         let mut h2 = DefaultHasher::new();
586
587         let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
588         let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
589
590         or1.hash(&mut h1);
591         or2.hash(&mut h2);
592
593         assert_eq!(h1.finish(), h2.finish());
594     }
595
596     #[test]
597     fn borrow() {
598         let mut hash = HashMap::new();
599         let key1 = BoxRefMut::<String>::new(Box::new("foo".to_string())).map(|s| &s[..]);
600         let key2 = BoxRefMut::<String>::new(Box::new("bar".to_string())).map(|s| &s[..]);
601
602         hash.insert(key1, 42);
603         hash.insert(key2, 23);
604
605         assert_eq!(hash.get("foo"), Some(&42));
606         assert_eq!(hash.get("bar"), Some(&23));
607     }
608
609     #[test]
610     fn total_erase() {
611         let a: OwningRefMut<Vec<u8>, [u8]> = OwningRefMut::new(vec![]).map_mut(|x| &mut x[..]);
612         let b: OwningRefMut<Box<[u8]>, [u8]> =
613             OwningRefMut::new(vec![].into_boxed_slice()).map_mut(|x| &mut x[..]);
614
615         let c: OwningRefMut<Box<Vec<u8>>, [u8]> = unsafe { a.map_owner(Box::new) };
616         let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = unsafe { b.map_owner(Box::new) };
617
618         let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
619         let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
620     }
621
622     #[test]
623     fn total_erase_box() {
624         let a: OwningRefMut<Vec<u8>, [u8]> = OwningRefMut::new(vec![]).map_mut(|x| &mut x[..]);
625         let b: OwningRefMut<Box<[u8]>, [u8]> =
626             OwningRefMut::new(vec![].into_boxed_slice()).map_mut(|x| &mut x[..]);
627
628         let c: OwningRefMut<Box<Vec<u8>>, [u8]> = a.map_owner_box();
629         let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
630
631         let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
632         let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
633     }
634
635     #[test]
636     fn try_map1() {
637         use std::any::Any;
638
639         let x = Box::new(123_i32);
640         let y: Box<dyn Any> = x;
641
642         assert!(OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_ok());
643     }
644
645     #[test]
646     fn try_map2() {
647         use std::any::Any;
648
649         let x = Box::new(123_i32);
650         let y: Box<dyn Any> = x;
651
652         assert!(!OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_err());
653     }
654
655     #[test]
656     fn try_map3() {
657         use std::any::Any;
658
659         let x = Box::new(123_i32);
660         let y: Box<dyn Any> = x;
661
662         assert!(OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_ok());
663     }
664
665     #[test]
666     fn try_map4() {
667         use std::any::Any;
668
669         let x = Box::new(123_i32);
670         let y: Box<dyn Any> = x;
671
672         assert!(!OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_err());
673     }
674
675     #[test]
676     fn into_owning_ref() {
677         use super::super::BoxRef;
678
679         let or: BoxRefMut<()> = Box::new(()).into();
680         let or: BoxRef<()> = or.into();
681         assert_eq!(&*or, &());
682     }
683
684     struct Foo {
685         u: u32,
686     }
687     struct Bar {
688         f: Foo,
689     }
690
691     #[test]
692     fn ref_mut() {
693         use std::cell::RefCell;
694
695         let a = RefCell::new(Bar { f: Foo { u: 42 } });
696         let mut b = OwningRefMut::new(a.borrow_mut());
697         assert_eq!(b.f.u, 42);
698         b.f.u = 43;
699         let mut c = b.map_mut(|x| &mut x.f);
700         assert_eq!(c.u, 43);
701         c.u = 44;
702         let mut d = c.map_mut(|x| &mut x.u);
703         assert_eq!(*d, 44);
704         *d = 45;
705         assert_eq!(*d, 45);
706     }
707 }