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

Rollup of 19 pull requests #35857

Merged
merged 43 commits into from
Aug 20, 2016
Merged
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
377ae44
explicitly show how iterating over `..` fails
matthew-piziak Aug 15, 2016
c186da7
RangeFull for-loop iteration fails because of IntoIterator
matthew-piziak Aug 16, 2016
dcee93a
replace Add example with something more evocative of addition
matthew-piziak Aug 16, 2016
da1f773
rustdoc: remove the `!` from macro URLs and titles
nrc Aug 3, 2016
e6cc4c5
Fix links
nrc Aug 15, 2016
0dc13ee
updated E0395 to new error format
clementmiao Aug 18, 2016
dae1406
updated E0396 to new error format
clementmiao Aug 18, 2016
6976991
Fix tiny spelling mistake in book
ErikUggeldahl Aug 18, 2016
c2b6f72
Add a few doc examples for `std::ffi::OsStr`.
frewsxcv Aug 18, 2016
06302cb
Fix minor typo
cantino Aug 18, 2016
9563f14
demonstrate `RHS != Self` use cases for `Mul` and `Div`
matthew-piziak Aug 18, 2016
469b7fd
split example into three sections with explanation
matthew-piziak Aug 18, 2016
301401e
Redirect
nrc Aug 16, 2016
161cb36
Update error message for E0084
pliniker Aug 18, 2016
a516dbb
note that calling drop() explicitly is a compiler error
matthew-piziak Aug 16, 2016
6c66eaa
replace `AddAssign` example with something more evocative of addition
matthew-piziak Aug 18, 2016
ffbb860
Add workaround to detect correct compiler version
Aug 18, 2016
39f318b
Update error format for E0232
mlayne Aug 18, 2016
2128d31
Fix label messages for E0133
wdv4758h Aug 19, 2016
06147ac
replace `Not` example with something more evocative
matthew-piziak Aug 19, 2016
c0eccb1
replace `Neg` example with something more evocative of negation
matthew-piziak Aug 19, 2016
3b64cf6
Update E0428 to new format
trixnz Aug 19, 2016
54d0acd
wording fixes in error messages
Aug 19, 2016
d791aa1
Fix broken tests for E0428
trixnz Aug 20, 2016
d5595d1
Rollup merge of #35234 - nrc:rustdoc-macros, r=steveklabnik
Aug 20, 2016
2d32a69
Rollup merge of #35701 - matthew-piziak:rangefull-example-error, r=st…
Aug 20, 2016
ed9a18a
Rollup merge of #35709 - matthew-piziak:add-trait-example, r=Guillaum…
Aug 20, 2016
ba8611e
Rollup merge of #35710 - matthew-piziak:explicit-drop, r=steveklabnik
Aug 20, 2016
54a4199
Rollup merge of #35775 - frewsxcv:os-str-doc-examples, r=GuillaumeGomez
Aug 20, 2016
7c843c4
Rollup merge of #35778 - clementmiao:E0395_new_error_format, r=jonath…
Aug 20, 2016
69612f0
Rollup merge of #35780 - clementmiao:E0396_new_err_format, r=jonathan…
Aug 20, 2016
f4b123f
Rollup merge of #35781 - ErikUggeldahl:spellingfix, r=apasel422
Aug 20, 2016
ad17e0c
Rollup merge of #35794 - cantino:fix-typo, r=apasel422
Aug 20, 2016
a36ff81
Rollup merge of #35800 - matthew-piziak:mul-div-examples, r=steveklabnik
Aug 20, 2016
15277eb
Rollup merge of #35804 - pliniker:master, r=jonathandturner
Aug 20, 2016
0155eb1
Rollup merge of #35806 - matthew-piziak:addassign-example, r=stevekla…
Aug 20, 2016
a361cdb
Rollup merge of #35811 - jonathandturner:fix_rustbuild_version_test, …
Aug 20, 2016
b9c0a8c
Rollup merge of #35812 - mlayne:E0232, r=jonathandturner
Aug 20, 2016
f17ff3a
Rollup merge of #35818 - wdv4758h:E0133-label, r=Aatch
Aug 20, 2016
cc431a0
Rollup merge of #35827 - matthew-piziak:neg-example, r=steveklabnik
Aug 20, 2016
0d69b88
Rollup merge of #35830 - matthew-piziak:not-example, r=steveklabnik
Aug 20, 2016
b22352f
Rollup merge of #35831 - trixnz:error-428, r=jonathandturner
Aug 20, 2016
9072861
Rollup merge of #35839 - jonathandturner:error_touchup, r=Aatch
Aug 20, 2016
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
Prev Previous commit
Next Next commit
demonstrate RHS != Self use cases for Mul and Div
Vector-scalar multipication is a good usecase for this. Thanks #rust!
  • Loading branch information
matthew-piziak committed Aug 18, 2016
commit 9563f14eb5d77d992f7cde5db227f8c83351427b
62 changes: 62 additions & 0 deletions src/libcore/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,37 @@ sub_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
/// Foo * Foo;
/// }
/// ```
///
/// Note that `RHS = Self` by default, but this is not mandatory. Here is an
/// implementation which enables multiplication of vectors by scalars, as is
/// done in linear algebra.
///
/// ```
/// use std::ops::Mul;
///
/// struct Scalar {value: usize};
///
/// #[derive(Debug)]
/// struct Vector {value: Vec<usize>};
///
/// impl Mul<Vector> for Scalar {
/// type Output = Vector;
///
/// fn mul(self, rhs: Vector) -> Vector {
/// Vector {value: rhs.value.iter().map(|v| self.value * v).collect()}
/// }
/// }
///
/// impl PartialEq<Vector> for Vector {
/// fn eq(&self, other: &Self) -> bool {
/// self.value == other.value
/// }
/// }
///
/// let scalar = Scalar{value: 3};
/// let vector = Vector{value: vec![2, 4, 6]};
/// assert_eq!(scalar * vector, Vector{value: vec![6, 12, 18]});
/// ```
#[lang = "mul"]
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Mul<RHS=Self> {
Expand Down Expand Up @@ -349,6 +380,37 @@ mul_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
/// Foo / Foo;
/// }
/// ```
///
/// Note that `RHS = Self` by default, but this is not mandatory. Here is an
/// implementation which enables division of vectors by scalars, as is done in
/// linear algebra.
///
/// ```
/// use std::ops::Div;
///
/// struct Scalar {value: f32};
///
/// #[derive(Debug)]
/// struct Vector {value: Vec<f32>};
///
/// impl Div<Scalar> for Vector {
/// type Output = Vector;
///
/// fn div(self, rhs: Scalar) -> Vector {
/// Vector {value: self.value.iter().map(|v| v / rhs.value).collect()}
/// }
/// }
///
/// impl PartialEq<Vector> for Vector {
/// fn eq(&self, other: &Self) -> bool {
/// self.value == other.value
/// }
/// }
///
/// let scalar = Scalar{value: 2f32};
/// let vector = Vector{value: vec![2f32, 4f32, 6f32]};
/// assert_eq!(vector / scalar, Vector{value: vec![1f32, 2f32, 3f32]});
/// ```
#[lang = "div"]
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Div<RHS=Self> {
Expand Down