]> git.proxmox.com Git - rustc.git/blob - src/librustc_data_structures/owning_ref/tests.rs
New upstream version 1.37.0+dfsg1
[rustc.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| unsafe { x.as_ref() }.unwrap().borrow_mut());
278 assert_eq!(*handle, 2);
279 *handle = 3;
280 assert_eq!(*handle, 3);
281 }
282
283 #[test]
284 fn try_owning_handle_ok() {
285 use std::cell::RefCell;
286 let cell = Rc::new(RefCell::new(2));
287 let cell_ref = RcRef::new(cell);
288 let mut handle = OwningHandle::try_new::<_, ()>(cell_ref, |x| {
289 Ok(unsafe {
290 x.as_ref()
291 }.unwrap().borrow_mut())
292 }).unwrap();
293 assert_eq!(*handle, 2);
294 *handle = 3;
295 assert_eq!(*handle, 3);
296 }
297
298 #[test]
299 fn try_owning_handle_err() {
300 use std::cell::RefCell;
301 let cell = Rc::new(RefCell::new(2));
302 let cell_ref = RcRef::new(cell);
303 let handle = OwningHandle::try_new::<_, ()>(cell_ref, |x| {
304 if false {
305 return Ok(unsafe {
306 x.as_ref()
307 }.unwrap().borrow_mut())
308 }
309 Err(())
310 });
311 assert!(handle.is_err());
312 }
313
314 #[test]
315 fn nested() {
316 use std::cell::RefCell;
317 use std::sync::{Arc, RwLock};
318
319 let result = {
320 let complex = Rc::new(RefCell::new(Arc::new(RwLock::new("someString"))));
321 let curr = RcRef::new(complex);
322 let curr = OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
323 let mut curr = OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().try_write().unwrap());
324 assert_eq!(*curr, "someString");
325 *curr = "someOtherString";
326 curr
327 };
328 assert_eq!(*result, "someOtherString");
329 }
330
331 #[test]
332 fn owning_handle_safe() {
333 use std::cell::RefCell;
334 let cell = Rc::new(RefCell::new(2));
335 let cell_ref = RcRef::new(cell);
336 let handle = OwningHandle::new(cell_ref);
337 assert_eq!(*handle, 2);
338 }
339
340 #[test]
341 fn owning_handle_mut_safe() {
342 use std::cell::RefCell;
343 let cell = Rc::new(RefCell::new(2));
344 let cell_ref = RcRef::new(cell);
345 let mut handle = OwningHandle::new_mut(cell_ref);
346 assert_eq!(*handle, 2);
347 *handle = 3;
348 assert_eq!(*handle, 3);
349 }
350
351 #[test]
352 fn owning_handle_safe_2() {
353 let result = {
354 let complex = Rc::new(RefCell::new(Arc::new(RwLock::new("someString"))));
355 let curr = RcRef::new(complex);
356 let curr = OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
357 let mut curr = OwningHandle::new_with_fn(curr, |x| unsafe { x.as_ref() }.unwrap().try_write().unwrap());
358 assert_eq!(*curr, "someString");
359 *curr = "someOtherString";
360 curr
361 };
362 assert_eq!(*result, "someOtherString");
363 }
364 }
365
366 mod owning_ref_mut {
367 use super::super::{OwningRefMut, BoxRefMut, Erased, ErasedBoxRefMut};
368 use super::super::BoxRef;
369 use std::cmp::{PartialEq, Ord, PartialOrd, Ordering};
370 use std::hash::{Hash, Hasher};
371 use std::collections::hash_map::DefaultHasher;
372 use std::collections::HashMap;
373
374 #[derive(Debug, PartialEq)]
375 struct Example(u32, String, [u8; 3]);
376 fn example() -> Example {
377 Example(42, "hello world".to_string(), [1, 2, 3])
378 }
379
380 #[test]
381 fn new_deref() {
382 let or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
383 assert_eq!(&*or, &());
384 }
385
386 #[test]
387 fn new_deref_mut() {
388 let mut or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
389 assert_eq!(&mut *or, &mut ());
390 }
391
392 #[test]
393 fn mutate() {
394 let mut or: OwningRefMut<Box<usize>, usize> = OwningRefMut::new(Box::new(0));
395 assert_eq!(&*or, &0);
396 *or = 1;
397 assert_eq!(&*or, &1);
398 }
399
400 #[test]
401 fn into() {
402 let or: OwningRefMut<Box<()>, ()> = Box::new(()).into();
403 assert_eq!(&*or, &());
404 }
405
406 #[test]
407 fn map_offset_ref() {
408 let or: BoxRefMut<Example> = Box::new(example()).into();
409 let or: BoxRef<_, u32> = or.map(|x| &mut x.0);
410 assert_eq!(&*or, &42);
411
412 let or: BoxRefMut<Example> = Box::new(example()).into();
413 let or: BoxRef<_, u8> = or.map(|x| &mut x.2[1]);
414 assert_eq!(&*or, &2);
415 }
416
417 #[test]
418 fn map_heap_ref() {
419 let or: BoxRefMut<Example> = Box::new(example()).into();
420 let or: BoxRef<_, str> = or.map(|x| &mut x.1[..5]);
421 assert_eq!(&*or, "hello");
422 }
423
424 #[test]
425 fn map_static_ref() {
426 let or: BoxRefMut<()> = Box::new(()).into();
427 let or: BoxRef<_, str> = or.map(|_| "hello");
428 assert_eq!(&*or, "hello");
429 }
430
431 #[test]
432 fn map_mut_offset_ref() {
433 let or: BoxRefMut<Example> = Box::new(example()).into();
434 let or: BoxRefMut<_, u32> = or.map_mut(|x| &mut x.0);
435 assert_eq!(&*or, &42);
436
437 let or: BoxRefMut<Example> = Box::new(example()).into();
438 let or: BoxRefMut<_, u8> = or.map_mut(|x| &mut x.2[1]);
439 assert_eq!(&*or, &2);
440 }
441
442 #[test]
443 fn map_mut_heap_ref() {
444 let or: BoxRefMut<Example> = Box::new(example()).into();
445 let or: BoxRefMut<_, str> = or.map_mut(|x| &mut x.1[..5]);
446 assert_eq!(&*or, "hello");
447 }
448
449 #[test]
450 fn map_mut_static_ref() {
451 static mut MUT_S: [u8; 5] = *b"hello";
452
453 let mut_s: &'static mut [u8] = unsafe { &mut MUT_S };
454
455 let or: BoxRefMut<()> = Box::new(()).into();
456 let or: BoxRefMut<_, [u8]> = or.map_mut(move |_| mut_s);
457 assert_eq!(&*or, b"hello");
458 }
459
460 #[test]
461 fn map_mut_chained() {
462 let or: BoxRefMut<String> = Box::new(example().1).into();
463 let or: BoxRefMut<_, str> = or.map_mut(|x| &mut x[1..5]);
464 let or: BoxRefMut<_, str> = or.map_mut(|x| &mut x[..2]);
465 assert_eq!(&*or, "el");
466 }
467
468 #[test]
469 fn map_chained_inference() {
470 let or = BoxRefMut::new(Box::new(example().1))
471 .map_mut(|x| &mut x[..5])
472 .map_mut(|x| &mut x[1..3]);
473 assert_eq!(&*or, "el");
474 }
475
476 #[test]
477 fn try_map_mut() {
478 let or: BoxRefMut<String> = Box::new(example().1).into();
479 let or: Result<BoxRefMut<_, str>, ()> = or.try_map_mut(|x| Ok(&mut x[1..5]));
480 assert_eq!(&*or.unwrap(), "ello");
481
482 let or: BoxRefMut<String> = Box::new(example().1).into();
483 let or: Result<BoxRefMut<_, str>, ()> = or.try_map_mut(|_| Err(()));
484 assert!(or.is_err());
485 }
486
487 #[test]
488 fn owner() {
489 let or: BoxRefMut<String> = Box::new(example().1).into();
490 let or = or.map_mut(|x| &mut x[..5]);
491 assert_eq!(&*or, "hello");
492 assert_eq!(&**or.owner(), "hello world");
493 }
494
495 #[test]
496 fn into_inner() {
497 let or: BoxRefMut<String> = Box::new(example().1).into();
498 let or = or.map_mut(|x| &mut x[..5]);
499 assert_eq!(&*or, "hello");
500 let s = *or.into_inner();
501 assert_eq!(&s, "hello world");
502 }
503
504 #[test]
505 fn fmt_debug() {
506 let or: BoxRefMut<String> = Box::new(example().1).into();
507 let or = or.map_mut(|x| &mut x[..5]);
508 let s = format!("{:?}", or);
509 assert_eq!(&s,
510 "OwningRefMut { owner: \"hello world\", reference: \"hello\" }");
511 }
512
513 #[test]
514 fn erased_owner() {
515 let o1: BoxRefMut<Example, str> = BoxRefMut::new(Box::new(example()))
516 .map_mut(|x| &mut x.1[..]);
517
518 let o2: BoxRefMut<String, str> = BoxRefMut::new(Box::new(example().1))
519 .map_mut(|x| &mut x[..]);
520
521 let os: Vec<ErasedBoxRefMut<str>> = vec![o1.erase_owner(), o2.erase_owner()];
522 assert!(os.iter().all(|e| &e[..] == "hello world"));
523 }
524
525 #[test]
526 fn raii_locks() {
527 use super::super::RefMutRefMut;
528 use std::cell::RefCell;
529 use super::super::{MutexGuardRefMut, RwLockWriteGuardRefMut};
530 use std::sync::{Mutex, RwLock};
531
532 {
533 let a = RefCell::new(1);
534 let a = {
535 let a = RefMutRefMut::new(a.borrow_mut());
536 assert_eq!(*a, 1);
537 a
538 };
539 assert_eq!(*a, 1);
540 drop(a);
541 }
542 {
543 let a = Mutex::new(1);
544 let a = {
545 let a = MutexGuardRefMut::new(a.lock().unwrap());
546 assert_eq!(*a, 1);
547 a
548 };
549 assert_eq!(*a, 1);
550 drop(a);
551 }
552 {
553 let a = RwLock::new(1);
554 let a = {
555 let a = RwLockWriteGuardRefMut::new(a.write().unwrap());
556 assert_eq!(*a, 1);
557 a
558 };
559 assert_eq!(*a, 1);
560 drop(a);
561 }
562 }
563
564 #[test]
565 fn eq() {
566 let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
567 let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
568 assert_eq!(or1.eq(&or2), true);
569 }
570
571 #[test]
572 fn cmp() {
573 let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
574 let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![4, 5, 6].into_boxed_slice());
575 assert_eq!(or1.cmp(&or2), Ordering::Less);
576 }
577
578 #[test]
579 fn partial_cmp() {
580 let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![4, 5, 6].into_boxed_slice());
581 let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
582 assert_eq!(or1.partial_cmp(&or2), Some(Ordering::Greater));
583 }
584
585 #[test]
586 fn hash() {
587 let mut h1 = DefaultHasher::new();
588 let mut h2 = DefaultHasher::new();
589
590 let or1: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
591 let or2: BoxRefMut<[u8]> = BoxRefMut::new(vec![1, 2, 3].into_boxed_slice());
592
593 or1.hash(&mut h1);
594 or2.hash(&mut h2);
595
596 assert_eq!(h1.finish(), h2.finish());
597 }
598
599 #[test]
600 fn borrow() {
601 let mut hash = HashMap::new();
602 let key1 = BoxRefMut::<String>::new(Box::new("foo".to_string())).map(|s| &s[..]);
603 let key2 = BoxRefMut::<String>::new(Box::new("bar".to_string())).map(|s| &s[..]);
604
605 hash.insert(key1, 42);
606 hash.insert(key2, 23);
607
608 assert_eq!(hash.get("foo"), Some(&42));
609 assert_eq!(hash.get("bar"), Some(&23));
610 }
611
612 #[test]
613 fn total_erase() {
614 let a: OwningRefMut<Vec<u8>, [u8]>
615 = OwningRefMut::new(vec![]).map_mut(|x| &mut x[..]);
616 let b: OwningRefMut<Box<[u8]>, [u8]>
617 = OwningRefMut::new(vec![].into_boxed_slice()).map_mut(|x| &mut x[..]);
618
619 let c: OwningRefMut<Box<Vec<u8>>, [u8]> = unsafe {a.map_owner(Box::new)};
620 let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = unsafe {b.map_owner(Box::new)};
621
622 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
623 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
624 }
625
626 #[test]
627 fn total_erase_box() {
628 let a: OwningRefMut<Vec<u8>, [u8]>
629 = OwningRefMut::new(vec![]).map_mut(|x| &mut x[..]);
630 let b: OwningRefMut<Box<[u8]>, [u8]>
631 = OwningRefMut::new(vec![].into_boxed_slice()).map_mut(|x| &mut x[..]);
632
633 let c: OwningRefMut<Box<Vec<u8>>, [u8]> = a.map_owner_box();
634 let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
635
636 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
637 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
638 }
639
640 #[test]
641 fn try_map1() {
642 use std::any::Any;
643
644 let x = Box::new(123_i32);
645 let y: Box<dyn Any> = x;
646
647 assert!(OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_ok());
648 }
649
650 #[test]
651 fn try_map2() {
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_err());
658 }
659
660 #[test]
661 fn try_map3() {
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(|x| x.downcast_ref::<i32>().ok_or(())).is_ok());
668 }
669
670 #[test]
671 fn try_map4() {
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_err());
678 }
679
680 #[test]
681 fn into_owning_ref() {
682 use super::super::BoxRef;
683
684 let or: BoxRefMut<()> = Box::new(()).into();
685 let or: BoxRef<()> = or.into();
686 assert_eq!(&*or, &());
687 }
688
689 struct Foo {
690 u: u32,
691 }
692 struct Bar {
693 f: Foo,
694 }
695
696 #[test]
697 fn ref_mut() {
698 use std::cell::RefCell;
699
700 let a = RefCell::new(Bar { f: Foo { u: 42 } });
701 let mut b = OwningRefMut::new(a.borrow_mut());
702 assert_eq!(b.f.u, 42);
703 b.f.u = 43;
704 let mut c = b.map_mut(|x| &mut x.f);
705 assert_eq!(c.u, 43);
706 c.u = 44;
707 let mut d = c.map_mut(|x| &mut x.u);
708 assert_eq!(*d, 44);
709 *d = 45;
710 assert_eq!(*d, 45);
711 }
712 }