@@ -42,15 +42,15 @@ use style_traits::{CssWriter, ToCss};
42
42
#[ derive( Clone , Copy ) ]
43
43
#[ repr( C ) ]
44
44
pub struct LengthVariant {
45
- tag : u32 ,
45
+ tag : u8 ,
46
46
length : Length ,
47
47
}
48
48
49
49
#[ doc( hidden) ]
50
50
#[ derive( Clone , Copy ) ]
51
51
#[ repr( C ) ]
52
52
pub struct PercentageVariant {
53
- tag : u32 ,
53
+ tag : u8 ,
54
54
percentage : Percentage ,
55
55
}
56
56
@@ -61,7 +61,7 @@ pub struct PercentageVariant {
61
61
#[ repr( C ) ]
62
62
#[ cfg( target_pointer_width = "32" ) ]
63
63
pub struct CalcVariant {
64
- tag : u32 ,
64
+ tag : u8 ,
65
65
ptr : * mut CalcLengthPercentage ,
66
66
}
67
67
@@ -70,7 +70,7 @@ pub struct CalcVariant {
70
70
#[ repr( C ) ]
71
71
#[ cfg( target_pointer_width = "64" ) ]
72
72
pub struct CalcVariant {
73
- ptr : * mut CalcLengthPercentage ,
73
+ ptr : usize , // In little-endian byte order
74
74
}
75
75
76
76
// `CalcLengthPercentage` is `Send + Sync` as asserted below.
@@ -81,7 +81,7 @@ unsafe impl Sync for CalcVariant {}
81
81
#[ derive( Clone , Copy ) ]
82
82
#[ repr( C ) ]
83
83
pub struct TagVariant {
84
- tag : u32 ,
84
+ tag : u8 ,
85
85
}
86
86
87
87
/// A `<length-percentage>` value. This can be either a `<length>`, a
@@ -114,17 +114,17 @@ pub union LengthPercentageUnion {
114
114
115
115
impl LengthPercentageUnion {
116
116
#[ doc( hidden) ] // Need to be public so that cbindgen generates it.
117
- pub const TAG_CALC : u32 = 0 ;
117
+ pub const TAG_CALC : u8 = 0 ;
118
118
#[ doc( hidden) ]
119
- pub const TAG_LENGTH : u32 = 1 ;
119
+ pub const TAG_LENGTH : u8 = 1 ;
120
120
#[ doc( hidden) ]
121
- pub const TAG_PERCENTAGE : u32 = 2 ;
121
+ pub const TAG_PERCENTAGE : u8 = 2 ;
122
122
#[ doc( hidden) ]
123
- pub const TAG_MASK : u32 = 0b11 ;
123
+ pub const TAG_MASK : u8 = 0b11 ;
124
124
}
125
125
126
126
#[ derive( Clone , Copy , Debug , PartialEq ) ]
127
- #[ repr( u32 ) ]
127
+ #[ repr( u8 ) ]
128
128
enum Tag {
129
129
Calc = LengthPercentageUnion :: TAG_CALC ,
130
130
Length = LengthPercentageUnion :: TAG_LENGTH ,
@@ -147,7 +147,7 @@ unsafe fn static_assert() {
147
147
impl Drop for LengthPercentage {
148
148
fn drop ( & mut self ) {
149
149
if self . tag ( ) == Tag :: Calc {
150
- let _ = unsafe { Box :: from_raw ( self . 0 . calc . ptr ) } ;
150
+ let _ = unsafe { Box :: from_raw ( self . calc_ptr ( ) ) } ;
151
151
}
152
152
}
153
153
}
@@ -236,13 +236,22 @@ impl LengthPercentage {
236
236
/// checking.
237
237
fn new_calc_unchecked ( calc : Box < CalcLengthPercentage > ) -> Self {
238
238
let ptr = Box :: into_raw ( calc) ;
239
- let calc = Self ( LengthPercentageUnion {
240
- calc : CalcVariant {
241
- #[ cfg( target_pointer_width = "32" ) ]
242
- tag : LengthPercentageUnion :: TAG_CALC ,
243
- ptr,
244
- } ,
245
- } ) ;
239
+
240
+ #[ cfg( target_pointer_width = "32" ) ]
241
+ let calc = CalcVariant {
242
+ tag : LengthPercentageUnion :: TAG_CALC ,
243
+ ptr,
244
+ } ;
245
+
246
+ #[ cfg( target_pointer_width = "64" ) ]
247
+ let calc = CalcVariant {
248
+ #[ cfg( target_endian = "little" ) ]
249
+ ptr : ptr as usize ,
250
+ #[ cfg( target_endian = "big" ) ]
251
+ ptr : ( ptr as usize ) . swap_bytes ( ) ,
252
+ } ;
253
+
254
+ let calc = Self ( LengthPercentageUnion { calc } ) ;
246
255
debug_assert_eq ! ( calc. tag( ) , Tag :: Calc ) ;
247
256
calc
248
257
}
@@ -261,13 +270,25 @@ impl LengthPercentage {
261
270
fn unpack < ' a > ( & ' a self ) -> Unpacked < ' a > {
262
271
unsafe {
263
272
match self . tag ( ) {
264
- Tag :: Calc => Unpacked :: Calc ( & * self . 0 . calc . ptr ) ,
273
+ Tag :: Calc => Unpacked :: Calc ( & * self . calc_ptr ( ) ) ,
265
274
Tag :: Length => Unpacked :: Length ( self . 0 . length . length ) ,
266
275
Tag :: Percentage => Unpacked :: Percentage ( self . 0 . percentage . percentage ) ,
267
276
}
268
277
}
269
278
}
270
279
280
+ #[ inline]
281
+ unsafe fn calc_ptr ( & self ) -> * mut CalcLengthPercentage {
282
+ #[ cfg( not( all( target_endian = "big" , target_pointer_width = "64" ) ) ) ]
283
+ {
284
+ self . 0 . calc . ptr as * mut _
285
+ }
286
+ #[ cfg( all( target_endian = "big" , target_pointer_width = "64" ) ) ]
287
+ {
288
+ self . 0 . calc . ptr . swap_bytes ( ) as * mut _
289
+ }
290
+ }
291
+
271
292
#[ inline]
272
293
fn to_serializable ( & self ) -> Serializable {
273
294
match self . unpack ( ) {
0 commit comments