@@ -10,41 +10,44 @@ mod ref_with_flag {
10
10
/// If you're the kind of programmer who's never met a pointer whose
11
11
/// 2⁰-bit you didn't want to steal, well, now you can do it safely!
12
12
/// ("But it's not nearly as exciting this way...")
13
- pub struct RefWithFlag < ' a , T : ' a > {
13
+ pub struct RefWithFlag < ' a , T > {
14
14
ptr_and_bit : usize ,
15
15
behaves_like : PhantomData < & ' a T > // occupies no space
16
16
}
17
17
18
18
impl < ' a , T : ' a > RefWithFlag < ' a , T > {
19
- pub fn new ( ptr : & ' a T , bit : bool ) -> RefWithFlag < T > {
19
+ pub fn new ( ptr : & ' a T , flag : bool ) -> RefWithFlag < T > {
20
20
assert ! ( align_of:: <T >( ) % 2 == 0 ) ;
21
21
RefWithFlag {
22
- ptr_and_bit : ptr as * const T as usize | bit as usize ,
22
+ ptr_and_bit : ptr as * const T as usize | flag as usize ,
23
23
behaves_like : PhantomData
24
24
}
25
25
}
26
26
27
- pub fn as_ref ( & self ) -> & ' a T {
28
- let ptr = ( self . ptr_and_bit & !1 ) as * const T ;
27
+ pub fn get_ref ( & self ) -> & ' a T {
29
28
unsafe {
29
+ let ptr = ( self . ptr_and_bit & !1 ) as * const T ;
30
30
& * ptr
31
31
}
32
32
}
33
33
34
- pub fn as_bool ( & self ) -> bool {
34
+ pub fn get_flag ( & self ) -> bool {
35
35
self . ptr_and_bit & 1 != 0
36
36
}
37
37
}
38
38
}
39
39
40
+ #[ cfg( test) ]
40
41
mod ref_with_flag_tests {
42
+ use super :: ref_with_flag;
43
+
41
44
#[ test]
42
45
fn use_ref_with_flag ( ) {
43
46
use ref_with_flag:: RefWithFlag ;
44
47
45
48
let vec = vec ! [ 10 , 20 , 30 ] ;
46
- let pab = RefWithFlag :: new ( & vec, true ) ;
47
- assert_eq ! ( pab . as_ref ( ) [ 1 ] , 20 ) ;
48
- assert_eq ! ( pab . as_bool ( ) , true ) ;
49
+ let flagged = RefWithFlag :: new ( & vec, true ) ;
50
+ assert_eq ! ( flagged . get_ref ( ) [ 1 ] , 20 ) ;
51
+ assert_eq ! ( flagged . get_flag ( ) , true ) ;
49
52
}
50
53
}
0 commit comments