1 fn one_line_ref() -> i16 {
5 fn basic_ref() -> i16 {
10 fn basic_ref_mut() -> i16 {
16 fn basic_ref_mut_var() -> i16 {
25 fn tuple_ref_mut() -> (i8, i8) {
34 fn match_ref_mut() -> i8 {
37 let opt = Some(&mut t);
39 Some(&mut (ref mut x, ref mut y)) => *x += *y,
46 fn dangling_pointer() -> *const i32 {
47 let b = Box::new((42, 42)); // make it bigger than the alignment, so that there is some "room" after this pointer
52 assert_eq!(one_line_ref(), 1);
53 assert_eq!(basic_ref(), 1);
54 assert_eq!(basic_ref_mut(), 3);
55 assert_eq!(basic_ref_mut_var(), 3);
56 assert_eq!(tuple_ref_mut(), (10, 22));
57 assert_eq!(match_ref_mut(), 42);
59 // Compare even dangling pointers with NULL, and with others in the same allocation, including
61 assert!(dangling_pointer() != std::ptr::null());
62 assert!(match dangling_pointer() as usize { 0 => false, _ => true });
63 let dangling = dangling_pointer();
64 assert!(dangling == dangling);
65 assert!(dangling.wrapping_add(1) != dangling);
66 assert!(dangling.wrapping_sub(1) != dangling);
68 // Compare pointer with BIG integers
69 let dangling = dangling as usize;
70 assert!(dangling != usize::MAX);
71 assert!(dangling != usize::MAX - 1);
72 assert!(dangling != usize::MAX - 2);
73 assert!(dangling != usize::MAX - 3); // this is even 4-aligned, but it still cannot be equal because of the extra "room" after this pointer
74 assert_eq!((usize::MAX - 3) % 4, 0); // just to be sure we got this right
76 // Compare pointer with unaligned integers
77 assert!(dangling != 1usize);
78 assert!(dangling != 2usize);
79 assert!(dangling != 3usize);
80 // 4 is a possible choice! So we cannot compare with that.
81 assert!(dangling != 5usize);
82 assert!(dangling != 6usize);
83 assert!(dangling != 7usize);
85 // Using inequality to do the comparison.
86 assert!(dangling > 0);
87 assert!(dangling > 1);
88 assert!(dangling > 2);
89 assert!(dangling > 3);
90 assert!(dangling >= 4);