@@ -95,9 +95,11 @@ pub unsafe fn to_enum<T: Enum>(v: u8) -> Option<T> {
95
95
if v >= T :: max_value ( ) {
96
96
None
97
97
} else {
98
- let mut target: T = std:: mem:: zeroed ( ) ;
99
- std:: ptr:: copy_nonoverlapping ( & v as * const u8 , & mut target as * mut T as * mut u8 , 1 ) ;
100
- Some ( target)
98
+ unsafe {
99
+ let mut target: T = std:: mem:: zeroed ( ) ;
100
+ std:: ptr:: copy_nonoverlapping ( & v as * const u8 , & mut target as * mut T as * mut u8 , 1 ) ;
101
+ Some ( target)
102
+ }
101
103
}
102
104
}
103
105
@@ -106,20 +108,22 @@ pub unsafe fn to_enum<T: Enum>(v: u8) -> Option<T> {
106
108
/// This method is safe to call if the length of v is sizeof(T)
107
109
/// and the bit pattern described by v is a valid state for T
108
110
pub unsafe fn to_pod < T : Pod > ( v : & [ u8 ] ) -> T {
109
- let mut target: T = std:: mem:: zeroed ( ) ;
110
- std:: ptr:: copy_nonoverlapping (
111
- v. as_ptr ( ) ,
112
- & mut target as * mut T as * mut u8 ,
113
- std:: mem:: size_of :: < T > ( ) ,
114
- ) ;
115
- target
111
+ unsafe {
112
+ let mut target: T = std:: mem:: zeroed ( ) ;
113
+ std:: ptr:: copy_nonoverlapping (
114
+ v. as_ptr ( ) ,
115
+ & mut target as * mut T as * mut u8 ,
116
+ std:: mem:: size_of :: < T > ( ) ,
117
+ ) ;
118
+ target
119
+ }
116
120
}
117
121
118
122
/// # Safety
119
123
///
120
124
/// Should only be called if s is at least of size sizeof(S::B)
121
125
pub unsafe fn to_struct < ' a , S : StructIn < ' a > + ' a > ( s : & [ u8 ] ) -> S {
122
- S :: new ( & * ( s as * const [ u8 ] as * const S :: B ) )
126
+ unsafe { S :: new ( & * ( s as * const [ u8 ] as * const S :: B ) ) }
123
127
}
124
128
125
129
pub fn to_bool ( v : u8 ) -> bool {
@@ -130,18 +134,22 @@ pub fn to_bool(v: u8) -> bool {
130
134
///
131
135
/// Should only be called when v.len() >= 6
132
136
pub unsafe fn to_u48 ( v : & [ u8 ] ) -> u64 {
133
- let mut out: u64 = 0 ;
134
- std:: ptr:: copy_nonoverlapping ( v. as_ptr ( ) , & mut out as * mut u64 as * mut u8 , 6 ) ;
135
- out
137
+ unsafe {
138
+ let mut out: u64 = 0 ;
139
+ std:: ptr:: copy_nonoverlapping ( v. as_ptr ( ) , & mut out as * mut u64 as * mut u8 , 6 ) ;
140
+ out
141
+ }
136
142
}
137
143
138
144
/// # Safety
139
145
///
140
146
/// Should only be called when v.len() >= 6
141
147
pub unsafe fn to_u48_usize ( v : & [ u8 ] ) -> Result < usize > {
142
- match std:: convert:: TryFrom :: try_from ( to_u48 ( v) ) {
143
- Ok ( v) => Ok ( v) ,
144
- Err ( _) => Err ( Error :: Overflow ( ) ) ,
148
+ unsafe {
149
+ match std:: convert:: TryFrom :: try_from ( to_u48 ( v) ) {
150
+ Ok ( v) => Ok ( v) ,
151
+ Err ( _) => Err ( Error :: Overflow ( ) ) ,
152
+ }
145
153
}
146
154
}
147
155
@@ -1254,8 +1262,10 @@ impl<'a> ArenaSlice<'a> {
1254
1262
/// This is safe as long as o is within the arena, the pointer
1255
1263
/// will be valid util the arena, resizes or is dropped.
1256
1264
unsafe fn data ( & self , o : usize ) -> * mut u8 {
1257
- let data = & mut * self . arena . data . get ( ) ;
1258
- data. as_mut_ptr ( ) . add ( self . offset + o)
1265
+ unsafe {
1266
+ let data = & mut * self . arena . data . get ( ) ;
1267
+ data. as_mut_ptr ( ) . add ( self . offset + o)
1268
+ }
1259
1269
}
1260
1270
1261
1271
fn check_offset ( & self , o : usize , size : usize ) {
@@ -1276,11 +1286,13 @@ impl<'a> ArenaSlice<'a> {
1276
1286
///
1277
1287
/// This is safe if offset..offset+sizeof<T> is within the arena
1278
1288
pub unsafe fn set_pod_unsafe < T : Copy + std:: fmt:: Debug > ( & mut self , offset : usize , value : & T ) {
1279
- std:: ptr:: copy_nonoverlapping (
1280
- value as * const T as * const u8 ,
1281
- self . data ( offset) ,
1282
- std:: mem:: size_of :: < T > ( ) ,
1283
- ) ;
1289
+ unsafe {
1290
+ std:: ptr:: copy_nonoverlapping (
1291
+ value as * const T as * const u8 ,
1292
+ self . data ( offset) ,
1293
+ std:: mem:: size_of :: < T > ( ) ,
1294
+ ) ;
1295
+ }
1284
1296
}
1285
1297
1286
1298
pub fn set_pod < T : Copy + std:: fmt:: Debug > ( & mut self , offset : usize , value : & T ) {
@@ -1292,11 +1304,13 @@ impl<'a> ArenaSlice<'a> {
1292
1304
///
1293
1305
/// This is safe if offset is within the arena and bit < 8
1294
1306
pub unsafe fn set_bit_unsafe ( & mut self , offset : usize , bit : usize , value : bool ) {
1295
- let d = self . data ( offset) ;
1296
- if value {
1297
- * d |= 1 << bit;
1298
- } else {
1299
- * d &= !( 1 << bit) ;
1307
+ unsafe {
1308
+ let d = self . data ( offset) ;
1309
+ if value {
1310
+ * d |= 1 << bit;
1311
+ } else {
1312
+ * d &= !( 1 << bit) ;
1313
+ }
1300
1314
}
1301
1315
}
1302
1316
@@ -1310,7 +1324,7 @@ impl<'a> ArenaSlice<'a> {
1310
1324
///
1311
1325
/// This is safe if offset is within the arena
1312
1326
pub unsafe fn set_bool_unsafe ( & mut self , offset : usize , value : bool ) {
1313
- * self . data ( offset) = if value { 1 } else { 0 }
1327
+ unsafe { * self . data ( offset) = if value { 1 } else { 0 } }
1314
1328
}
1315
1329
1316
1330
pub fn set_bool ( & mut self , offset : usize , value : bool ) {
@@ -1322,7 +1336,9 @@ impl<'a> ArenaSlice<'a> {
1322
1336
///
1323
1337
/// This is safe if offset,offset+6 is in the arena, and value is less than 1 << 42
1324
1338
pub unsafe fn set_u48_unsafe ( & mut self , offset : usize , value : u64 ) {
1325
- std:: ptr:: copy_nonoverlapping ( & value as * const u64 as * const u8 , self . data ( offset) , 6 ) ;
1339
+ unsafe {
1340
+ std:: ptr:: copy_nonoverlapping ( & value as * const u64 as * const u8 , self . data ( offset) , 6 ) ;
1341
+ }
1326
1342
}
1327
1343
1328
1344
pub fn set_u48 ( & mut self , offset : usize , value : u64 ) {
@@ -1335,11 +1351,15 @@ impl<'a> ArenaSlice<'a> {
1335
1351
///
1336
1352
/// This is safe if std::mem::size_of::<T>() == 1 and offset is within the arena
1337
1353
pub unsafe fn set_enum_unsafe < T : Enum > ( & mut self , offset : usize , value : Option < T > ) {
1338
- match value {
1339
- Some ( vv) => {
1340
- std:: ptr:: copy_nonoverlapping ( & vv as * const T as * const u8 , self . data ( offset) , 1 )
1354
+ unsafe {
1355
+ match value {
1356
+ Some ( vv) => std:: ptr:: copy_nonoverlapping (
1357
+ & vv as * const T as * const u8 ,
1358
+ self . data ( offset) ,
1359
+ 1 ,
1360
+ ) ,
1361
+ None => * self . data ( offset) = 255 ,
1341
1362
}
1342
- None => * self . data ( offset) = 255 ,
1343
1363
}
1344
1364
}
1345
1365
0 commit comments