Skip to content

Commit 4b0e9bf

Browse files
Merge #203
203: A few small patches r=cuviper a=sshilovsky Co-authored-by: Sergei Shilovsky <sshilovsky@gmail.com>
2 parents ccd5bbf + 6959b8a commit 4b0e9bf

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fn main() {
1818

1919
ac.emit_expression_cfg("1u32.reverse_bits()", "has_reverse_bits");
2020
ac.emit_expression_cfg("1u32.trailing_ones()", "has_leading_trailing_ones");
21+
ac.emit_expression_cfg("{ let mut x = 1; x += &2; }", "has_int_assignop_ref");
2122

2223
autocfg::rerun_path("build.rs");
2324
}

src/lib.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub trait Num: PartialEq + Zero + One + NumOps {
9595
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>;
9696
}
9797

98-
/// The trait for types implementing basic numeric operations
98+
/// Generic trait for types implementing basic numeric operations
9999
///
100100
/// This is automatically implemented for types which implement the operators.
101101
pub trait NumOps<Rhs = Self, Output = Self>:
@@ -123,14 +123,16 @@ impl<T, Rhs, Output> NumOps<Rhs, Output> for T where
123123
pub trait NumRef: Num + for<'r> NumOps<&'r Self> {}
124124
impl<T> NumRef for T where T: Num + for<'r> NumOps<&'r T> {}
125125

126-
/// The trait for references which implement numeric operations, taking the
126+
/// The trait for `Num` references which implement numeric operations, taking the
127127
/// second operand either by value or by reference.
128128
///
129-
/// This is automatically implemented for types which implement the operators.
129+
/// This is automatically implemented for all types which implement the operators. It covers
130+
/// every type implementing the operations though, regardless of it being a reference or
131+
/// related to `Num`.
130132
pub trait RefNum<Base>: NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base> {}
131133
impl<T, Base> RefNum<Base> for T where T: NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base> {}
132134

133-
/// The trait for types implementing numeric assignment operators (like `+=`).
135+
/// Generic trait for types implementing numeric assignment operators (like `+=`).
134136
///
135137
/// This is automatically implemented for types which implement the operators.
136138
pub trait NumAssignOps<Rhs = Self>:
@@ -175,11 +177,7 @@ int_trait_impl!(Num for u128 i128);
175177

176178
impl<T: Num> Num for Wrapping<T>
177179
where
178-
Wrapping<T>: Add<Output = Wrapping<T>>
179-
+ Sub<Output = Wrapping<T>>
180-
+ Mul<Output = Wrapping<T>>
181-
+ Div<Output = Wrapping<T>>
182-
+ Rem<Output = Wrapping<T>>,
180+
Wrapping<T>: NumOps,
183181
{
184182
type FromStrRadixErr = T::FromStrRadixErr;
185183
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
@@ -626,5 +624,16 @@ fn check_numassign_ops() {
626624
assert_eq!(compute(1, 2), 1)
627625
}
628626

629-
// TODO test `NumAssignRef`, but even the standard numeric types don't
630-
// implement this yet. (see rust pr41336)
627+
#[cfg(has_int_assignop_ref)]
628+
#[test]
629+
fn check_numassignref_ops() {
630+
fn compute<T: NumAssignRef + Copy>(mut x: T, y: &T) -> T {
631+
x *= y;
632+
x /= y;
633+
x %= y;
634+
x += y;
635+
x -= y;
636+
x
637+
}
638+
assert_eq!(compute(1, &2), 1)
639+
}

0 commit comments

Comments
 (0)