Skip to content

Commit 612c4a9

Browse files
committed
Impl Integer methods for Wrapping
Wrapping<T> now implements: count_ones, count_zeros, leading_zeros, trailing_zeros, rotate_left, rotate_right, swap_bytes, from_be, from_le, to_be, to_le, and pow where T is: u8, u16, u32, u64, usize, i8, i16, i32, i64, or isize. Docs were written for all these methods, as well as examples. The examples mirror the ones on u8, u16, etc... for consistency. Closes rust-lang#32463
1 parent 5508b27 commit 612c4a9

File tree

1 file changed

+299
-0
lines changed

1 file changed

+299
-0
lines changed

src/libcore/num/wrapping.rs

+299
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,307 @@ macro_rules! wrapping_impl {
317317
}
318318
forward_ref_unop! { impl Neg, neg for Wrapping<$t>,
319319
#[stable(feature = "wrapping_ref", since = "1.14.0")] }
320+
320321
)*)
321322
}
322323

323324
wrapping_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
324325

326+
macro_rules! wrapping_int_impl {
327+
($($t:ty)*) => ($(
328+
impl Wrapping<$t> {
329+
/// Returns the number of ones in the binary representation of
330+
/// `self`.
331+
///
332+
/// # Examples
333+
///
334+
/// Basic usage:
335+
///
336+
/// ```
337+
/// #![feature(wrapping_int_impl)]
338+
/// use std::num::Wrapping;
339+
///
340+
/// let n: Wrapping<i8> = Wrapping(-0b1000_0000);
341+
///
342+
/// assert_eq!(n.count_ones(), 1);
343+
/// ```
344+
#[inline]
345+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
346+
pub fn count_ones(self) -> u32 {
347+
self.0.count_ones()
348+
}
349+
350+
/// Returns the number of zeros in the binary representation of
351+
/// `self`.
352+
///
353+
/// # Examples
354+
///
355+
/// Basic usage:
356+
///
357+
/// ```
358+
/// #![feature(wrapping_int_impl)]
359+
/// use std::num::Wrapping;
360+
///
361+
/// let n: Wrapping<i8> = Wrapping(-0b1000_0000);
362+
///
363+
/// assert_eq!(n.count_zeros(), 7);
364+
/// ```
365+
#[inline]
366+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
367+
pub fn count_zeros(self) -> u32 {
368+
self.0.count_zeros()
369+
}
370+
371+
/// Returns the number of leading zeros in the binary representation
372+
/// of `self`.
373+
///
374+
/// # Examples
375+
///
376+
/// Basic usage:
377+
///
378+
/// ```
379+
/// #![feature(wrapping_int_impl)]
380+
/// use std::num::Wrapping;
381+
///
382+
/// let n: Wrapping<i16> = Wrapping(-1);
383+
///
384+
/// assert_eq!(n.leading_zeros(), 0);
385+
/// ```
386+
#[inline]
387+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
388+
pub fn leading_zeros(self) -> u32 {
389+
self.0.leading_zeros()
390+
}
391+
392+
/// Returns the number of trailing zeros in the binary representation
393+
/// of `self`.
394+
///
395+
/// # Examples
396+
///
397+
/// Basic usage:
398+
///
399+
/// ```
400+
/// #![feature(wrapping_int_impl)]
401+
/// use std::num::Wrapping;
402+
///
403+
/// let n: Wrapping<i8> = Wrapping(-4);
404+
///
405+
/// assert_eq!(n.trailing_zeros(), 2);
406+
/// ```
407+
#[inline]
408+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
409+
pub fn trailing_zeros(self) -> u32 {
410+
self.0.trailing_zeros()
411+
}
412+
413+
/// Shifts the bits to the left by a specified amount, `n`,
414+
/// wrapping the truncated bits to the end of the resulting
415+
/// integer.
416+
///
417+
/// Please note this isn't the same operation as `>>`!
418+
///
419+
/// # Examples
420+
///
421+
/// Basic usage:
422+
///
423+
/// ```
424+
/// #![feature(wrapping_int_impl)]
425+
/// use std::num::Wrapping;
426+
///
427+
/// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
428+
/// let m: Wrapping<i64> = Wrapping(-0x76543210FEDCBA99);
429+
///
430+
/// assert_eq!(n.rotate_left(32), m);
431+
/// ```
432+
#[inline]
433+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
434+
pub fn rotate_left(self, n: u32) -> Self {
435+
Wrapping(self.0.rotate_left(n))
436+
}
437+
438+
/// Shifts the bits to the right by a specified amount, `n`,
439+
/// wrapping the truncated bits to the beginning of the resulting
440+
/// integer.
441+
///
442+
/// Please note this isn't the same operation as `<<`!
443+
///
444+
/// # Examples
445+
///
446+
/// Basic usage:
447+
///
448+
/// ```
449+
/// #![feature(wrapping_int_impl)]
450+
/// use std::num::Wrapping;
451+
///
452+
/// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
453+
/// let m: Wrapping<i64> = Wrapping(-0xFEDCBA987654322);
454+
///
455+
/// assert_eq!(n.rotate_right(4), m);
456+
/// ```
457+
#[inline]
458+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
459+
pub fn rotate_right(self, n: u32) -> Self {
460+
Wrapping(self.0.rotate_right(n))
461+
}
462+
463+
/// Reverses the byte order of the integer.
464+
///
465+
/// # Examples
466+
///
467+
/// Basic usage:
468+
///
469+
/// ```
470+
/// #![feature(wrapping_int_impl)]
471+
/// use std::num::Wrapping;
472+
///
473+
/// let n: Wrapping<i16> = Wrapping(0b0000000_01010101);
474+
/// assert_eq!(n, Wrapping(85));
475+
///
476+
/// let m = n.swap_bytes();
477+
///
478+
/// assert_eq!(m, Wrapping(0b01010101_00000000));
479+
/// assert_eq!(m, Wrapping(21760));
480+
/// ```
481+
#[inline]
482+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
483+
pub fn swap_bytes(self) -> Self {
484+
Wrapping(self.0.swap_bytes())
485+
}
486+
487+
/// Converts an integer from big endian to the target's endianness.
488+
///
489+
/// On big endian this is a no-op. On little endian the bytes are
490+
/// swapped.
491+
///
492+
/// # Examples
493+
///
494+
/// Basic usage:
495+
///
496+
/// ```
497+
/// #![feature(wrapping_int_impl)]
498+
/// use std::num::Wrapping;
499+
///
500+
/// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
501+
///
502+
/// if cfg!(target_endian = "big") {
503+
/// assert_eq!(Wrapping::<i64>::from_be(n), n);
504+
/// } else {
505+
/// assert_eq!(Wrapping::<i64>::from_be(n), n.swap_bytes());
506+
/// }
507+
/// ```
508+
#[inline]
509+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
510+
pub fn from_be(x: Self) -> Self {
511+
Wrapping(<$t>::from_be(x.0))
512+
}
513+
514+
/// Converts an integer from little endian to the target's endianness.
515+
///
516+
/// On little endian this is a no-op. On big endian the bytes are
517+
/// swapped.
518+
///
519+
/// # Examples
520+
///
521+
/// Basic usage:
522+
///
523+
/// ```
524+
/// #![feature(wrapping_int_impl)]
525+
/// use std::num::Wrapping;
526+
///
527+
/// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
528+
///
529+
/// if cfg!(target_endian = "little") {
530+
/// assert_eq!(Wrapping::<i64>::from_le(n), n);
531+
/// } else {
532+
/// assert_eq!(Wrapping::<i64>::from_le(n), n.swap_bytes());
533+
/// }
534+
/// ```
535+
#[inline]
536+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
537+
pub fn from_le(x: Self) -> Self {
538+
Wrapping(<$t>::from_le(x.0))
539+
}
540+
541+
/// Converts `self` to big endian from the target's endianness.
542+
///
543+
/// On big endian this is a no-op. On little endian the bytes are
544+
/// swapped.
545+
///
546+
/// # Examples
547+
///
548+
/// Basic usage:
549+
///
550+
/// ```
551+
/// #![feature(wrapping_int_impl)]
552+
/// use std::num::Wrapping;
553+
///
554+
/// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
555+
///
556+
/// if cfg!(target_endian = "big") {
557+
/// assert_eq!(n.to_be(), n);
558+
/// } else {
559+
/// assert_eq!(n.to_be(), n.swap_bytes());
560+
/// }
561+
/// ```
562+
#[inline]
563+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
564+
pub fn to_be(self) -> Self {
565+
Wrapping(self.0.to_be())
566+
}
567+
568+
/// Converts `self` to little endian from the target's endianness.
569+
///
570+
/// On little endian this is a no-op. On big endian the bytes are
571+
/// swapped.
572+
///
573+
/// # Examples
574+
///
575+
/// Basic usage:
576+
///
577+
/// ```
578+
/// #![feature(wrapping_int_impl)]
579+
/// use std::num::Wrapping;
580+
///
581+
/// let n: Wrapping<i64> = Wrapping(0x0123456789ABCDEF);
582+
///
583+
/// if cfg!(target_endian = "little") {
584+
/// assert_eq!(n.to_le(), n);
585+
/// } else {
586+
/// assert_eq!(n.to_le(), n.swap_bytes());
587+
/// }
588+
/// ```
589+
#[inline]
590+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
591+
pub fn to_le(self) -> Self {
592+
Wrapping(self.0.to_le())
593+
}
594+
595+
/// Raises self to the power of `exp`, using exponentiation by
596+
/// squaring.
597+
///
598+
/// # Examples
599+
///
600+
/// Basic usage:
601+
///
602+
/// ```
603+
/// #![feature(wrapping_int_impl)]
604+
/// use std::num::Wrapping;
605+
///
606+
/// let x: Wrapping<i32> = Wrapping(2); // or any other integer type
607+
///
608+
/// assert_eq!(x.pow(4), Wrapping(16));
609+
#[inline]
610+
#[unstable(feature = "wrapping_int_impl", issue = "32463")]
611+
pub fn pow(self, exp: u32) -> Self {
612+
Wrapping(self.0.pow(exp))
613+
}
614+
}
615+
)*)
616+
}
617+
618+
wrapping_int_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
619+
620+
325621
mod shift_max {
326622
#![allow(non_upper_case_globals)]
327623

@@ -355,3 +651,6 @@ mod shift_max {
355651
pub const u64: u32 = i64;
356652
pub use self::platform::usize;
357653
}
654+
655+
656+

0 commit comments

Comments
 (0)