|
13 | 13 | //! This module contains functions for querying the size and alignment of
|
14 | 14 | //! types, initializing and manipulating memory.
|
15 | 15 |
|
16 |
| -use clone::Clone; |
17 |
| -use ptr; |
18 | 16 | use intrinsics;
|
19 |
| -use intrinsics::{bswap16, bswap32, bswap64}; |
| 17 | +use num::Int; |
| 18 | +use ptr; |
20 | 19 |
|
21 | 20 | pub use intrinsics::transmute;
|
22 | 21 |
|
@@ -170,155 +169,6 @@ pub unsafe fn move_val_init<T>(dst: &mut T, src: T) {
|
170 | 169 | ptr::write(dst, src)
|
171 | 170 | }
|
172 | 171 |
|
173 |
| -/// A type that can have its bytes re-ordered. |
174 |
| -pub trait ByteOrder: Clone { |
175 |
| - /// Reverses the byte order of the value. |
176 |
| - /// |
177 |
| - /// # Example |
178 |
| - /// |
179 |
| - /// ```rust |
180 |
| - /// use std::mem::ByteOrder; |
181 |
| - /// |
182 |
| - /// let n = 0x0123456789ABCDEFu64; |
183 |
| - /// let m = 0xEFCDAB8967452301u64; |
184 |
| - /// |
185 |
| - /// assert_eq!(n.swap_bytes(), m); |
186 |
| - /// ``` |
187 |
| - fn swap_bytes(&self) -> Self; |
188 |
| - |
189 |
| - /// Convert a value from big endian to the target's endianness. |
190 |
| - /// |
191 |
| - /// On big endian this is a no-op. On little endian the bytes are swapped. |
192 |
| - /// |
193 |
| - /// # Example |
194 |
| - /// |
195 |
| - /// ```rust |
196 |
| - /// use std::mem::ByteOrder; |
197 |
| - /// |
198 |
| - /// let n = 0x0123456789ABCDEFu64; |
199 |
| - /// |
200 |
| - /// if cfg!(target_endian = "big") { |
201 |
| - /// assert_eq!(ByteOrder::from_big_endian(n), n) |
202 |
| - /// } else { |
203 |
| - /// assert_eq!(ByteOrder::from_big_endian(n), n.swap_bytes()) |
204 |
| - /// } |
205 |
| - /// ``` |
206 |
| - #[inline] |
207 |
| - fn from_big_endian(x: Self) -> Self { |
208 |
| - if cfg!(target_endian = "big") { x } else { x.swap_bytes() } |
209 |
| - } |
210 |
| - |
211 |
| - /// Convert a value from little endian to the target's endianness. |
212 |
| - /// |
213 |
| - /// On little endian this is a no-op. On big endian the bytes are swapped. |
214 |
| - /// |
215 |
| - /// # Example |
216 |
| - /// |
217 |
| - /// ```rust |
218 |
| - /// use std::mem::ByteOrder; |
219 |
| - /// |
220 |
| - /// let n = 0x0123456789ABCDEFu64; |
221 |
| - /// |
222 |
| - /// if cfg!(target_endian = "little") { |
223 |
| - /// assert_eq!(ByteOrder::from_little_endian(n), n) |
224 |
| - /// } else { |
225 |
| - /// assert_eq!(ByteOrder::from_little_endian(n), n.swap_bytes()) |
226 |
| - /// } |
227 |
| - /// ``` |
228 |
| - #[inline] |
229 |
| - fn from_little_endian(x: Self) -> Self { |
230 |
| - if cfg!(target_endian = "little") { x } else { x.swap_bytes() } |
231 |
| - } |
232 |
| - |
233 |
| - /// Convert the value to big endian from the target's endianness. |
234 |
| - /// |
235 |
| - /// On big endian this is a no-op. On little endian the bytes are swapped. |
236 |
| - /// |
237 |
| - /// # Example |
238 |
| - /// |
239 |
| - /// ```rust |
240 |
| - /// use std::mem::ByteOrder; |
241 |
| - /// |
242 |
| - /// let n = 0x0123456789ABCDEFu64; |
243 |
| - /// |
244 |
| - /// if cfg!(target_endian = "big") { |
245 |
| - /// assert_eq!(n.to_big_endian(), n) |
246 |
| - /// } else { |
247 |
| - /// assert_eq!(n.to_big_endian(), n.swap_bytes()) |
248 |
| - /// } |
249 |
| - /// ``` |
250 |
| - #[inline] |
251 |
| - fn to_big_endian(&self) -> Self { |
252 |
| - if cfg!(target_endian = "big") { self.clone() } else { self.swap_bytes() } |
253 |
| - } |
254 |
| - |
255 |
| - /// Convert the value to little endian from the target's endianness. |
256 |
| - /// |
257 |
| - /// On little endian this is a no-op. On big endian the bytes are swapped. |
258 |
| - /// |
259 |
| - /// # Example |
260 |
| - /// |
261 |
| - /// ```rust |
262 |
| - /// use std::mem::ByteOrder; |
263 |
| - /// |
264 |
| - /// let n = 0x0123456789ABCDEFu64; |
265 |
| - /// |
266 |
| - /// if cfg!(target_endian = "little") { |
267 |
| - /// assert_eq!(n.to_little_endian(), n) |
268 |
| - /// } else { |
269 |
| - /// assert_eq!(n.to_little_endian(), n.swap_bytes()) |
270 |
| - /// } |
271 |
| - /// ``` |
272 |
| - #[inline] |
273 |
| - fn to_little_endian(&self) -> Self { |
274 |
| - if cfg!(target_endian = "little") { self.clone() } else { self.swap_bytes() } |
275 |
| - } |
276 |
| -} |
277 |
| - |
278 |
| -impl ByteOrder for u8 { |
279 |
| - #[inline] |
280 |
| - fn swap_bytes(&self) -> u8 { |
281 |
| - *self // swapping a single byte does nothing |
282 |
| - } |
283 |
| -} |
284 |
| - |
285 |
| -impl ByteOrder for u16 { |
286 |
| - #[inline] |
287 |
| - fn swap_bytes(&self) -> u16 { |
288 |
| - unsafe { intrinsics::bswap16(*self) } |
289 |
| - } |
290 |
| -} |
291 |
| - |
292 |
| -impl ByteOrder for u32 { |
293 |
| - #[inline] |
294 |
| - fn swap_bytes(&self) -> u32 { |
295 |
| - unsafe { intrinsics::bswap32(*self) } |
296 |
| - } |
297 |
| -} |
298 |
| - |
299 |
| -impl ByteOrder for u64 { |
300 |
| - #[inline] |
301 |
| - fn swap_bytes(&self) -> u64 { |
302 |
| - unsafe { intrinsics::bswap64(*self) } |
303 |
| - } |
304 |
| -} |
305 |
| - |
306 |
| -#[cfg(target_word_size = "32")] |
307 |
| -impl ByteOrder for uint { |
308 |
| - #[inline] |
309 |
| - fn swap_bytes(&self) -> uint { |
310 |
| - (*self as u32).swap_bytes() as uint |
311 |
| - } |
312 |
| -} |
313 |
| - |
314 |
| -#[cfg(target_word_size = "64")] |
315 |
| -impl ByteOrder for uint { |
316 |
| - #[inline] |
317 |
| - fn swap_bytes(&self) -> uint { |
318 |
| - (*self as u64).swap_bytes() as uint |
319 |
| - } |
320 |
| -} |
321 |
| - |
322 | 172 | /// Convert an u16 to little endian from the target's endianness.
|
323 | 173 | ///
|
324 | 174 | /// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
@@ -366,42 +216,42 @@ pub fn to_be64(x: u64) -> u64 { x.to_big_endian() }
|
366 | 216 | /// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
367 | 217 | #[inline]
|
368 | 218 | #[stable]
|
369 |
| -pub fn from_le16(x: u16) -> u16 { ByteOrder::from_little_endian(x) } |
| 219 | +pub fn from_le16(x: u16) -> u16 { Int::from_little_endian(x) } |
370 | 220 |
|
371 | 221 | /// Convert an u32 from little endian to the target's endianness.
|
372 | 222 | ///
|
373 | 223 | /// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
374 | 224 | #[inline]
|
375 | 225 | #[stable]
|
376 |
| -pub fn from_le32(x: u32) -> u32 { ByteOrder::from_little_endian(x) } |
| 226 | +pub fn from_le32(x: u32) -> u32 { Int::from_little_endian(x) } |
377 | 227 |
|
378 | 228 | /// Convert an u64 from little endian to the target's endianness.
|
379 | 229 | ///
|
380 | 230 | /// On little endian, this is a no-op. On big endian, the bytes are swapped.
|
381 | 231 | #[inline]
|
382 | 232 | #[stable]
|
383 |
| -pub fn from_le64(x: u64) -> u64 { ByteOrder::from_little_endian(x) } |
| 233 | +pub fn from_le64(x: u64) -> u64 { Int::from_little_endian(x) } |
384 | 234 |
|
385 | 235 | /// Convert an u16 from big endian to the target's endianness.
|
386 | 236 | ///
|
387 | 237 | /// On big endian, this is a no-op. On little endian, the bytes are swapped.
|
388 | 238 | #[inline]
|
389 | 239 | #[stable]
|
390 |
| -pub fn from_be16(x: u16) -> u16 { ByteOrder::from_big_endian(x) } |
| 240 | +pub fn from_be16(x: u16) -> u16 { Int::from_big_endian(x) } |
391 | 241 |
|
392 | 242 | /// Convert an u32 from big endian to the target's endianness.
|
393 | 243 | ///
|
394 | 244 | /// On big endian, this is a no-op. On little endian, the bytes are swapped.
|
395 | 245 | #[inline]
|
396 | 246 | #[stable]
|
397 |
| -pub fn from_be32(x: u32) -> u32 { ByteOrder::from_big_endian(x) } |
| 247 | +pub fn from_be32(x: u32) -> u32 { Int::from_big_endian(x) } |
398 | 248 |
|
399 | 249 | /// Convert an u64 from big endian to the target's endianness.
|
400 | 250 | ///
|
401 | 251 | /// On big endian, this is a no-op. On little endian, the bytes are swapped.
|
402 | 252 | #[inline]
|
403 | 253 | #[stable]
|
404 |
| -pub fn from_be64(x: u64) -> u64 { ByteOrder::from_big_endian(x) } |
| 254 | +pub fn from_be64(x: u64) -> u64 { Int::from_big_endian(x) } |
405 | 255 |
|
406 | 256 | /// Swap the values at two mutable locations of the same type, without
|
407 | 257 | /// deinitialising or copying either one.
|
@@ -642,60 +492,6 @@ mod tests {
|
642 | 492 | assert!(Vec::from_slice([76u8]) == transmute("L".to_string()));
|
643 | 493 | }
|
644 | 494 | }
|
645 |
| - |
646 |
| - macro_rules! test_byte_order { |
647 |
| - ($T:ident) => { |
648 |
| - mod $T { |
649 |
| - use mem::ByteOrder; |
650 |
| - |
651 |
| - static A: $T = 0b0101100; |
652 |
| - static B: $T = 0b0100001; |
653 |
| - static C: $T = 0b1111001; |
654 |
| - |
655 |
| - static _0: $T = 0; |
656 |
| - static _1: $T = !0; |
657 |
| - |
658 |
| - #[test] |
659 |
| - fn test_swap_bytes() { |
660 |
| - assert_eq!(A.swap_bytes().swap_bytes(), A); |
661 |
| - assert_eq!(B.swap_bytes().swap_bytes(), B); |
662 |
| - assert_eq!(C.swap_bytes().swap_bytes(), C); |
663 |
| - |
664 |
| - // Swapping these should make no difference |
665 |
| - assert_eq!(_0.swap_bytes(), _0); |
666 |
| - assert_eq!(_1.swap_bytes(), _1); |
667 |
| - } |
668 |
| - |
669 |
| - #[test] |
670 |
| - fn test_little_endian() { |
671 |
| - assert_eq!(ByteOrder::from_little_endian(A.to_little_endian()), A); |
672 |
| - assert_eq!(ByteOrder::from_little_endian(B.to_little_endian()), B); |
673 |
| - assert_eq!(ByteOrder::from_little_endian(C.to_little_endian()), C); |
674 |
| - assert_eq!(ByteOrder::from_little_endian(_0), _0); |
675 |
| - assert_eq!(ByteOrder::from_little_endian(_1), _1); |
676 |
| - assert_eq!(_0.to_little_endian(), _0); |
677 |
| - assert_eq!(_1.to_little_endian(), _1); |
678 |
| - } |
679 |
| - |
680 |
| - #[test] |
681 |
| - fn test_big_endian() { |
682 |
| - assert_eq!(ByteOrder::from_big_endian(A.to_big_endian()), A); |
683 |
| - assert_eq!(ByteOrder::from_big_endian(B.to_big_endian()), B); |
684 |
| - assert_eq!(ByteOrder::from_big_endian(C.to_big_endian()), C); |
685 |
| - assert_eq!(ByteOrder::from_big_endian(_0), _0); |
686 |
| - assert_eq!(ByteOrder::from_big_endian(_1), _1); |
687 |
| - assert_eq!(_0.to_big_endian(), _0); |
688 |
| - assert_eq!(_1.to_big_endian(), _1); |
689 |
| - } |
690 |
| - } |
691 |
| - } |
692 |
| - } |
693 |
| - |
694 |
| - test_byte_order!(u8) |
695 |
| - test_byte_order!(u16) |
696 |
| - test_byte_order!(u32) |
697 |
| - test_byte_order!(u64) |
698 |
| - test_byte_order!(uint) |
699 | 495 | }
|
700 | 496 |
|
701 | 497 | // FIXME #13642 (these benchmarks should be in another place)
|
|
0 commit comments