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};
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])
18 let or: OwningRef<Box<()>, ()> = OwningRef::new(Box::new(()));
19 assert_eq!(&*or, &());
24 let or: OwningRef<Box<()>, ()> = Box::new(()).into();
25 assert_eq!(&*or, &());
30 let or: BoxRef<Example> = Box::new(example()).into();
31 let or: BoxRef<_, u32> = or.map(|x| &x.0);
32 assert_eq!(&*or, &42);
34 let or: BoxRef<Example> = Box::new(example()).into();
35 let or: BoxRef<_, u8> = or.map(|x| &x.2[1]);
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");
48 let or: BoxRef<()> = Box::new(()).into();
49 let or: BoxRef<_, str> = or.map(|_| "hello");
50 assert_eq!(&*or, "hello");
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");
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");
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");
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");
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\" }");
94 let o1: BoxRef<Example, str> = BoxRef::new(Box::new(example())).map(|x| &x.1[..]);
96 let o2: BoxRef<String, str> = BoxRef::new(Box::new(example().1)).map(|x| &x[..]);
98 let os: Vec<ErasedBoxRef<str>> = vec![o1.erase_owner(), o2.erase_owner()];
99 assert!(os.iter().all(|e| &e[..] == "hello world"));
104 use super::super::{MutexGuardRef, RwLockReadGuardRef, RwLockWriteGuardRef};
105 use super::super::{RefMutRef, RefRef};
106 use std::cell::RefCell;
107 use std::sync::{Mutex, RwLock};
110 let a = RefCell::new(1);
112 let a = RefRef::new(a.borrow());
120 let a = RefCell::new(1);
122 let a = RefMutRef::new(a.borrow_mut());
130 let a = Mutex::new(1);
132 let a = MutexGuardRef::new(a.lock().unwrap());
140 let a = RwLock::new(1);
142 let a = RwLockReadGuardRef::new(a.read().unwrap());
150 let a = RwLock::new(1);
152 let a = RwLockWriteGuardRef::new(a.write().unwrap());
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);
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);
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));
184 let mut h1 = DefaultHasher::new();
185 let mut h2 = DefaultHasher::new();
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());
193 assert_eq!(h1.finish(), h2.finish());
198 let mut hash = HashMap::new();
199 let key = RcRef::<String>::new(Rc::new("foo-bar".to_string())).map(|s| &s[..]);
201 hash.insert(key.clone().map(|s| &s[..3]), 42);
202 hash.insert(key.clone().map(|s| &s[4..]), 23);
204 assert_eq!(hash.get("foo"), Some(&42));
205 assert_eq!(hash.get("bar"), Some(&23));
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[..]);
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) };
217 let e: OwningRef<Rc<dyn Erased>, [u8]> = c.erase_owner();
218 let f: OwningRef<Rc<dyn Erased>, [u8]> = d.erase_owner();
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[..]);
230 let c: OwningRef<Box<Vec<u8>>, [u8]> = a.map_owner_box();
231 let d: OwningRef<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
233 let _e: OwningRef<Box<dyn Erased>, [u8]> = c.erase_owner();
234 let _f: OwningRef<Box<dyn Erased>, [u8]> = d.erase_owner();
241 let x = Box::new(123_i32);
242 let y: Box<dyn Any> = x;
244 assert!(OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_ok());
251 let x = Box::new(123_i32);
252 let y: Box<dyn Any> = x;
254 assert!(!OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_err());
259 use super::super::OwningHandle;
260 use super::super::RcRef;
261 use std::cell::RefCell;
264 use std::sync::RwLock;
268 use std::cell::RefCell;
269 let cell = Rc::new(RefCell::new(2));
270 let cell_ref = RcRef::new(cell);
272 OwningHandle::new_with_fn(cell_ref, |x| unsafe { x.as_ref() }.unwrap().borrow_mut());
273 assert_eq!(*handle, 2);
275 assert_eq!(*handle, 3);
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())
287 assert_eq!(*handle, 2);
289 assert_eq!(*handle, 3);
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| {
299 return Ok(unsafe { x.as_ref() }.unwrap().borrow_mut());
303 assert!(handle.is_err());
308 use std::cell::RefCell;
309 use std::sync::{Arc, RwLock};
312 let complex = Rc::new(RefCell::new(Arc::new(RwLock::new("someString"))));
313 let curr = RcRef::new(complex);
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()
319 assert_eq!(*curr, "someString");
320 *curr = "someOtherString";
323 assert_eq!(*result, "someOtherString");
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);
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);
343 assert_eq!(*handle, 3);
347 fn owning_handle_safe_2() {
349 let complex = Rc::new(RefCell::new(Arc::new(RwLock::new("someString"))));
350 let curr = RcRef::new(complex);
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()
356 assert_eq!(*curr, "someString");
357 *curr = "someOtherString";
360 assert_eq!(*result, "someOtherString");
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};
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])
380 let or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
381 assert_eq!(&*or, &());
386 let mut or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
387 assert_eq!(&mut *or, &mut ());
392 let mut or: OwningRefMut<Box<usize>, usize> = OwningRefMut::new(Box::new(0));
393 assert_eq!(&*or, &0);
395 assert_eq!(&*or, &1);
400 let or: OwningRefMut<Box<()>, ()> = Box::new(()).into();
401 assert_eq!(&*or, &());
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);
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);
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");
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");
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);
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);
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");
448 fn map_mut_static_ref() {
449 static mut MUT_S: [u8; 5] = *b"hello";
451 let mut_s: &'static mut [u8] = unsafe { &mut MUT_S };
453 let or: BoxRefMut<()> = Box::new(()).into();
454 let or: BoxRefMut<_, [u8]> = or.map_mut(move |_| mut_s);
455 assert_eq!(&*or, b"hello");
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");
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");
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");
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());
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");
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");
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\" }");
512 let o1: BoxRefMut<Example, str> =
513 BoxRefMut::new(Box::new(example())).map_mut(|x| &mut x.1[..]);
515 let o2: BoxRefMut<String, str> =
516 BoxRefMut::new(Box::new(example().1)).map_mut(|x| &mut x[..]);
518 let os: Vec<ErasedBoxRefMut<str>> = vec![o1.erase_owner(), o2.erase_owner()];
519 assert!(os.iter().all(|e| &e[..] == "hello world"));
524 use super::super::RefMutRefMut;
525 use super::super::{MutexGuardRefMut, RwLockWriteGuardRefMut};
526 use std::cell::RefCell;
527 use std::sync::{Mutex, RwLock};
530 let a = RefCell::new(1);
532 let a = RefMutRefMut::new(a.borrow_mut());
540 let a = Mutex::new(1);
542 let a = MutexGuardRefMut::new(a.lock().unwrap());
550 let a = RwLock::new(1);
552 let a = RwLockWriteGuardRefMut::new(a.write().unwrap());
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);
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);
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));
584 let mut h1 = DefaultHasher::new();
585 let mut h2 = DefaultHasher::new();
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());
593 assert_eq!(h1.finish(), h2.finish());
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[..]);
602 hash.insert(key1, 42);
603 hash.insert(key2, 23);
605 assert_eq!(hash.get("foo"), Some(&42));
606 assert_eq!(hash.get("bar"), Some(&23));
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[..]);
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) };
618 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
619 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
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[..]);
628 let c: OwningRefMut<Box<Vec<u8>>, [u8]> = a.map_owner_box();
629 let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
631 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
632 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
639 let x = Box::new(123_i32);
640 let y: Box<dyn Any> = x;
642 assert!(OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_ok());
649 let x = Box::new(123_i32);
650 let y: Box<dyn Any> = x;
652 assert!(!OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_err());
659 let x = Box::new(123_i32);
660 let y: Box<dyn Any> = x;
662 assert!(OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_ok());
669 let x = Box::new(123_i32);
670 let y: Box<dyn Any> = x;
672 assert!(!OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_err());
676 fn into_owning_ref() {
677 use super::super::BoxRef;
679 let or: BoxRefMut<()> = Box::new(()).into();
680 let or: BoxRef<()> = or.into();
681 assert_eq!(&*or, &());
693 use std::cell::RefCell;
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);
699 let mut c = b.map_mut(|x| &mut x.f);
702 let mut d = c.map_mut(|x| &mut x.u);