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