@@ -8,57 +8,6 @@ use std::ops;
88
99use crate :: Rect ;
1010
11- /// Takes a mutable reference to a container and a function deriving a
12- /// reference into it, and stores both, making it possible to get back the
13- /// reference to the container once the other reference is no longer needed.
14- ///
15- /// This should be consistent with stacked borrow rules, and miri seems to
16- /// accept it at least in simple cases.
17- pub struct BorrowStack < ' a , T : ' a + ?Sized , U : ' a + ?Sized > {
18- container : * mut T ,
19- member : * mut U ,
20- _phantom : std:: marker:: PhantomData < & ' a mut T > ,
21- }
22-
23- unsafe impl < ' a , T : ' a + Send + ?Sized , U : ' a + Send + ?Sized > Send for BorrowStack < ' a , T , U > { }
24-
25- impl < ' a , T : ' a + ?Sized , U : ' a + ?Sized > BorrowStack < ' a , T , U > {
26- pub fn new < F > ( container : & ' a mut T , f : F ) -> Self
27- where
28- F : for < ' b > FnOnce ( & ' b mut T ) -> & ' b mut U ,
29- {
30- let container = container as * mut T ;
31- let member = f ( unsafe { & mut * container } ) as * mut U ;
32- Self {
33- container,
34- member,
35- _phantom : std:: marker:: PhantomData ,
36- }
37- }
38-
39- pub fn member ( & self ) -> & U {
40- unsafe { & * self . member }
41- }
42-
43- pub fn member_mut ( & mut self ) -> & mut U {
44- unsafe { & mut * self . member }
45- }
46-
47- pub fn into_container ( self ) -> & ' a mut T {
48- // SAFETY: Since we consume self and no longer reference member, this
49- // mutable reference is unique.
50- unsafe { & mut * self . container }
51- }
52- }
53-
54- impl < ' a , T : ' a + ?Sized , U : ' a + ?Sized + fmt:: Debug > fmt:: Debug for BorrowStack < ' a , T , U > {
55- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
56- f. debug_struct ( "BorrowStack" )
57- . field ( "member" , & self . member ( ) )
58- . finish_non_exhaustive ( )
59- }
60- }
61-
6211/// Calculates the smallest `Rect` necessary to represent all damaged `Rect`s.
6312pub ( crate ) fn union_damage ( damage : & [ Rect ] ) -> Option < Rect > {
6413 struct Region {
@@ -116,21 +65,3 @@ impl ops::DerefMut for PixelBuffer {
11665 & mut self . 0
11766 }
11867}
119-
120- #[ cfg( test) ]
121- mod tests {
122- use super :: * ;
123-
124- #[ test]
125- fn test_borrowstack_slice_int ( ) {
126- fn f ( mut stack : BorrowStack < [ u32 ] , u32 > ) {
127- assert_eq ! ( * stack. member( ) , 3 ) ;
128- * stack. member_mut ( ) = 42 ;
129- assert_eq ! ( stack. into_container( ) , & [ 1 , 2 , 42 , 4 , 5 ] ) ;
130- }
131-
132- let mut v = vec ! [ 1 , 2 , 3 , 4 , 5 ] ;
133- f ( BorrowStack :: new ( v. as_mut ( ) , |v : & mut [ u32 ] | & mut v[ 2 ] ) ) ;
134- assert_eq ! ( & v, & [ 1 , 2 , 42 , 4 , 5 ] ) ;
135- }
136- }
0 commit comments