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;
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))
66 assert_eq!(&*or, "el");
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");
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");
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\" }");
96 let o1: BoxRef<Example, str> = BoxRef::new(Box::new(example()))
99 let o2: BoxRef<String, str> = BoxRef::new(Box::new(example().1))
102 let os: Vec<ErasedBoxRef<str>> = vec![o1.erase_owner(), o2.erase_owner()];
103 assert!(os.iter().all(|e| &e[..] == "hello world"));
108 use super::super::{RefRef, RefMutRef};
109 use std::cell::RefCell;
110 use super::super::{MutexGuardRef, RwLockReadGuardRef, RwLockWriteGuardRef};
111 use std::sync::{Mutex, RwLock};
114 let a = RefCell::new(1);
116 let a = RefRef::new(a.borrow());
124 let a = RefCell::new(1);
126 let a = RefMutRef::new(a.borrow_mut());
134 let a = Mutex::new(1);
136 let a = MutexGuardRef::new(a.lock().unwrap());
144 let a = RwLock::new(1);
146 let a = RwLockReadGuardRef::new(a.read().unwrap());
154 let a = RwLock::new(1);
156 let a = RwLockWriteGuardRef::new(a.write().unwrap());
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);
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);
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));
188 let mut h1 = DefaultHasher::new();
189 let mut h2 = DefaultHasher::new();
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());
197 assert_eq!(h1.finish(), h2.finish());
202 let mut hash = HashMap::new();
203 let key = RcRef::<String>::new(Rc::new("foo-bar".to_string())).map(|s| &s[..]);
205 hash.insert(key.clone().map(|s| &s[..3]), 42);
206 hash.insert(key.clone().map(|s| &s[4..]), 23);
208 assert_eq!(hash.get("foo"), Some(&42));
209 assert_eq!(hash.get("bar"), Some(&23));
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[..]);
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)};
222 let e: OwningRef<Rc<dyn Erased>, [u8]> = c.erase_owner();
223 let f: OwningRef<Rc<dyn Erased>, [u8]> = d.erase_owner();
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[..]);
236 let c: OwningRef<Box<Vec<u8>>, [u8]> = a.map_owner_box();
237 let d: OwningRef<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
239 let _e: OwningRef<Box<dyn Erased>, [u8]> = c.erase_owner();
240 let _f: OwningRef<Box<dyn Erased>, [u8]> = d.erase_owner();
247 let x = Box::new(123_i32);
248 let y: Box<dyn Any> = x;
250 assert!(OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_ok());
257 let x = Box::new(123_i32);
258 let y: Box<dyn Any> = x;
260 assert!(!OwningRef::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_err());
265 use super::super::OwningHandle;
266 use super::super::RcRef;
268 use std::cell::RefCell;
270 use std::sync::RwLock;
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);
280 assert_eq!(*handle, 3);
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| {
291 }.unwrap().borrow_mut())
293 assert_eq!(*handle, 2);
295 assert_eq!(*handle, 3);
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| {
307 }.unwrap().borrow_mut())
311 assert!(handle.is_err());
316 use std::cell::RefCell;
317 use std::sync::{Arc, RwLock};
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";
328 assert_eq!(*result, "someOtherString");
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);
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);
348 assert_eq!(*handle, 3);
352 fn owning_handle_safe_2() {
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";
362 assert_eq!(*result, "someOtherString");
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;
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])
382 let or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
383 assert_eq!(&*or, &());
388 let mut or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
389 assert_eq!(&mut *or, &mut ());
394 let mut or: OwningRefMut<Box<usize>, usize> = OwningRefMut::new(Box::new(0));
395 assert_eq!(&*or, &0);
397 assert_eq!(&*or, &1);
402 let or: OwningRefMut<Box<()>, ()> = Box::new(()).into();
403 assert_eq!(&*or, &());
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);
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);
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");
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");
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);
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);
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");
450 fn map_mut_static_ref() {
451 static mut MUT_S: [u8; 5] = *b"hello";
453 let mut_s: &'static mut [u8] = unsafe { &mut MUT_S };
455 let or: BoxRefMut<()> = Box::new(()).into();
456 let or: BoxRefMut<_, [u8]> = or.map_mut(move |_| mut_s);
457 assert_eq!(&*or, b"hello");
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");
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");
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");
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());
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");
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");
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);
510 "OwningRefMut { owner: \"hello world\", reference: \"hello\" }");
515 let o1: BoxRefMut<Example, str> = BoxRefMut::new(Box::new(example()))
516 .map_mut(|x| &mut x.1[..]);
518 let o2: BoxRefMut<String, str> = BoxRefMut::new(Box::new(example().1))
519 .map_mut(|x| &mut x[..]);
521 let os: Vec<ErasedBoxRefMut<str>> = vec![o1.erase_owner(), o2.erase_owner()];
522 assert!(os.iter().all(|e| &e[..] == "hello world"));
527 use super::super::RefMutRefMut;
528 use std::cell::RefCell;
529 use super::super::{MutexGuardRefMut, RwLockWriteGuardRefMut};
530 use std::sync::{Mutex, RwLock};
533 let a = RefCell::new(1);
535 let a = RefMutRefMut::new(a.borrow_mut());
543 let a = Mutex::new(1);
545 let a = MutexGuardRefMut::new(a.lock().unwrap());
553 let a = RwLock::new(1);
555 let a = RwLockWriteGuardRefMut::new(a.write().unwrap());
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);
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);
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));
587 let mut h1 = DefaultHasher::new();
588 let mut h2 = DefaultHasher::new();
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());
596 assert_eq!(h1.finish(), h2.finish());
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[..]);
605 hash.insert(key1, 42);
606 hash.insert(key2, 23);
608 assert_eq!(hash.get("foo"), Some(&42));
609 assert_eq!(hash.get("bar"), Some(&23));
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[..]);
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)};
622 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
623 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
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[..]);
633 let c: OwningRefMut<Box<Vec<u8>>, [u8]> = a.map_owner_box();
634 let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
636 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
637 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
644 let x = Box::new(123_i32);
645 let y: Box<dyn Any> = x;
647 assert!(OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_ok());
654 let x = Box::new(123_i32);
655 let y: Box<dyn Any> = x;
657 assert!(!OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_err());
664 let x = Box::new(123_i32);
665 let y: Box<dyn Any> = x;
667 assert!(OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_ok());
674 let x = Box::new(123_i32);
675 let y: Box<dyn Any> = x;
677 assert!(!OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_err());
681 fn into_owning_ref() {
682 use super::super::BoxRef;
684 let or: BoxRefMut<()> = Box::new(()).into();
685 let or: BoxRef<()> = or.into();
686 assert_eq!(&*or, &());
698 use std::cell::RefCell;
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);
704 let mut c = b.map_mut(|x| &mut x.f);
707 let mut d = c.map_mut(|x| &mut x.u);