Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing examples on binary core traits #76615

Merged
merged 1 commit into from
Sep 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 134 additions & 0 deletions library/core/src/ops/bit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ pub trait Not {
type Output;

/// Performs the unary `!` operation.
///
/// # Examples
///
/// ```
/// assert_eq!(!true, false);
/// assert_eq!(!false, true);
/// assert_eq!(!1u8, 254);
/// assert_eq!(!0u8, 255);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn not(self) -> Self::Output;
Expand Down Expand Up @@ -122,6 +131,15 @@ pub trait BitAnd<Rhs = Self> {
type Output;

/// Performs the `&` operation.
///
/// # Examples
///
/// ```
/// assert_eq!(true & false, false);
/// assert_eq!(true & true, true);
/// assert_eq!(5u8 & 1u8, 1);
/// assert_eq!(5u8 & 2u8, 0);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn bitand(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -208,6 +226,15 @@ pub trait BitOr<Rhs = Self> {
type Output;

/// Performs the `|` operation.
///
/// # Examples
///
/// ```
/// assert_eq!(true | false, true);
/// assert_eq!(false | false, false);
/// assert_eq!(5u8 | 1u8, 5);
/// assert_eq!(5u8 | 2u8, 7);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn bitor(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -297,6 +324,15 @@ pub trait BitXor<Rhs = Self> {
type Output;

/// Performs the `^` operation.
///
/// # Examples
///
/// ```
/// assert_eq!(true ^ false, true);
/// assert_eq!(true ^ true, false);
/// assert_eq!(5u8 ^ 1u8, 4);
/// assert_eq!(5u8 ^ 2u8, 7);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn bitxor(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -387,6 +423,13 @@ pub trait Shl<Rhs = Self> {
type Output;

/// Performs the `<<` operation.
///
/// # Examples
///
/// ```
/// assert_eq!(5u8 << 1, 10);
/// assert_eq!(1u8 << 1, 2);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn shl(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -498,6 +541,13 @@ pub trait Shr<Rhs = Self> {
type Output;

/// Performs the `>>` operation.
///
/// # Examples
///
/// ```
/// assert_eq!(5u8 >> 1, 2);
/// assert_eq!(2u8 >> 1, 1);
/// ```
#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
fn shr(self, rhs: Rhs) -> Self::Output;
Expand Down Expand Up @@ -612,6 +662,26 @@ shr_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
)]
pub trait BitAndAssign<Rhs = Self> {
/// Performs the `&=` operation.
///
/// # Examples
///
/// ```
/// let mut x = true;
/// x &= false;
/// assert_eq!(x, false);
///
/// let mut x = true;
/// x &= true;
/// assert_eq!(x, true);
///
/// let mut x: u8 = 5;
/// x &= 1;
/// assert_eq!(x, 1);
///
/// let mut x: u8 = 5;
/// x &= 2;
/// assert_eq!(x, 0);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn bitand_assign(&mut self, rhs: Rhs);
}
Expand Down Expand Up @@ -663,6 +733,26 @@ bitand_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
)]
pub trait BitOrAssign<Rhs = Self> {
/// Performs the `|=` operation.
///
/// # Examples
///
/// ```
/// let mut x = true;
/// x |= false;
/// assert_eq!(x, true);
///
/// let mut x = false;
/// x |= false;
/// assert_eq!(x, false);
///
/// let mut x: u8 = 5;
/// x |= 1;
/// assert_eq!(x, 5);
///
/// let mut x: u8 = 5;
/// x |= 2;
/// assert_eq!(x, 7);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn bitor_assign(&mut self, rhs: Rhs);
}
Expand Down Expand Up @@ -714,6 +804,26 @@ bitor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
)]
pub trait BitXorAssign<Rhs = Self> {
/// Performs the `^=` operation.
///
/// # Examples
///
/// ```
/// let mut x = true;
/// x ^= false;
/// assert_eq!(x, true);
///
/// let mut x = true;
/// x ^= true;
/// assert_eq!(x, false);
///
/// let mut x: u8 = 5;
/// x ^= 1;
/// assert_eq!(x, 4);
///
/// let mut x: u8 = 5;
/// x ^= 2;
/// assert_eq!(x, 7);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn bitxor_assign(&mut self, rhs: Rhs);
}
Expand Down Expand Up @@ -763,6 +873,18 @@ bitxor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
)]
pub trait ShlAssign<Rhs = Self> {
/// Performs the `<<=` operation.
///
/// # Examples
///
/// ```
/// let mut x: u8 = 5;
/// x <<= 1;
/// assert_eq!(x, 10);
///
/// let mut x: u8 = 1;
/// x <<= 1;
/// assert_eq!(x, 2);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn shl_assign(&mut self, rhs: Rhs);
}
Expand Down Expand Up @@ -833,6 +955,18 @@ shl_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
)]
pub trait ShrAssign<Rhs = Self> {
/// Performs the `>>=` operation.
///
/// # Examples
///
/// ```
/// let mut x: u8 = 5;
/// x >>= 1;
/// assert_eq!(x, 2);
///
/// let mut x: u8 = 2;
/// x >>= 1;
/// assert_eq!(x, 1);
/// ```
#[stable(feature = "op_assign_traits", since = "1.8.0")]
fn shr_assign(&mut self, rhs: Rhs);
}
Expand Down