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| {
278 unsafe { x.as_ref() }.unwrap().borrow_mut()
280 assert_eq!(*handle, 2);
282 assert_eq!(*handle, 3);
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| {
293 }.unwrap().borrow_mut())
295 assert_eq!(*handle, 2);
297 assert_eq!(*handle, 3);
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| {
309 }.unwrap().borrow_mut())
313 assert!(handle.is_err());
318 use std::cell::RefCell;
319 use std::sync::{Arc, RwLock};
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()
327 let mut curr = OwningHandle::new_with_fn(curr, |x| {
328 unsafe { x.as_ref() }.unwrap().try_write().unwrap()
330 assert_eq!(*curr, "someString");
331 *curr = "someOtherString";
334 assert_eq!(*result, "someOtherString");
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);
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);
354 assert_eq!(*handle, 3);
358 fn owning_handle_safe_2() {
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()
365 let mut curr = OwningHandle::new_with_fn(curr, |x| {
366 unsafe { x.as_ref() }.unwrap().try_write().unwrap()
368 assert_eq!(*curr, "someString");
369 *curr = "someOtherString";
372 assert_eq!(*result, "someOtherString");
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;
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])
392 let or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
393 assert_eq!(&*or, &());
398 let mut or: OwningRefMut<Box<()>, ()> = OwningRefMut::new(Box::new(()));
399 assert_eq!(&mut *or, &mut ());
404 let mut or: OwningRefMut<Box<usize>, usize> = OwningRefMut::new(Box::new(0));
405 assert_eq!(&*or, &0);
407 assert_eq!(&*or, &1);
412 let or: OwningRefMut<Box<()>, ()> = Box::new(()).into();
413 assert_eq!(&*or, &());
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);
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);
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");
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");
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);
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);
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");
460 fn map_mut_static_ref() {
461 static mut MUT_S: [u8; 5] = *b"hello";
463 let mut_s: &'static mut [u8] = unsafe { &mut MUT_S };
465 let or: BoxRefMut<()> = Box::new(()).into();
466 let or: BoxRefMut<_, [u8]> = or.map_mut(move |_| mut_s);
467 assert_eq!(&*or, b"hello");
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");
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");
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");
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());
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");
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");
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);
520 "OwningRefMut { owner: \"hello world\", reference: \"hello\" }");
525 let o1: BoxRefMut<Example, str> = BoxRefMut::new(Box::new(example()))
526 .map_mut(|x| &mut x.1[..]);
528 let o2: BoxRefMut<String, str> = BoxRefMut::new(Box::new(example().1))
529 .map_mut(|x| &mut x[..]);
531 let os: Vec<ErasedBoxRefMut<str>> = vec![o1.erase_owner(), o2.erase_owner()];
532 assert!(os.iter().all(|e| &e[..] == "hello world"));
537 use super::super::RefMutRefMut;
538 use std::cell::RefCell;
539 use super::super::{MutexGuardRefMut, RwLockWriteGuardRefMut};
540 use std::sync::{Mutex, RwLock};
543 let a = RefCell::new(1);
545 let a = RefMutRefMut::new(a.borrow_mut());
553 let a = Mutex::new(1);
555 let a = MutexGuardRefMut::new(a.lock().unwrap());
563 let a = RwLock::new(1);
565 let a = RwLockWriteGuardRefMut::new(a.write().unwrap());
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);
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);
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));
597 let mut h1 = DefaultHasher::new();
598 let mut h2 = DefaultHasher::new();
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());
606 assert_eq!(h1.finish(), h2.finish());
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[..]);
615 hash.insert(key1, 42);
616 hash.insert(key2, 23);
618 assert_eq!(hash.get("foo"), Some(&42));
619 assert_eq!(hash.get("bar"), Some(&23));
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[..]);
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)};
632 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
633 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
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[..]);
643 let c: OwningRefMut<Box<Vec<u8>>, [u8]> = a.map_owner_box();
644 let d: OwningRefMut<Box<Box<[u8]>>, [u8]> = b.map_owner_box();
646 let _e: OwningRefMut<Box<dyn Erased>, [u8]> = c.erase_owner();
647 let _f: OwningRefMut<Box<dyn Erased>, [u8]> = d.erase_owner();
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_ok());
664 let x = Box::new(123_i32);
665 let y: Box<dyn Any> = x;
667 assert!(!OwningRefMut::new(y).try_map_mut(|x| x.downcast_mut::<i32>().ok_or(())).is_err());
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_ok());
684 let x = Box::new(123_i32);
685 let y: Box<dyn Any> = x;
687 assert!(!OwningRefMut::new(y).try_map(|x| x.downcast_ref::<i32>().ok_or(())).is_err());
691 fn into_owning_ref() {
692 use super::super::BoxRef;
694 let or: BoxRefMut<()> = Box::new(()).into();
695 let or: BoxRef<()> = or.into();
696 assert_eq!(&*or, &());
708 use std::cell::RefCell;
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);
714 let mut c = b.map_mut(|x| &mut x.f);
717 let mut d = c.map_mut(|x| &mut x.u);