summaryrefslogtreecommitdiffstats
path: root/third_party/rust/derive_more-impl/doc/mul_assign.md
blob: e5d9225ef391b9ad43de3abfe87798cfd168c888 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# What `#[derive(MulAssign)]` generates

This code is very similar to the code that is generated for `#[derive(Mul)]`.
The difference is that it mutates the existing instance instead of creating a
new one.

You can add the `#[mul_assign(forward)]` attribute if you don't want the same
semantics as `Mul`.
This will instead generate a `MulAssign` implementation with the same semantics
as `AddAssign`.




## Tuple structs

When deriving `MulAssign` for a tuple struct with two fields like this:

```rust
# use derive_more::MulAssign;
#
#[derive(MulAssign)]
struct MyInts(i32, i32);
```

Code like this will be generated:

```rust
# struct MyInts(i32, i32);
impl<__RhsT: ::core::marker::Copy> ::core::ops::MulAssign<__RhsT> for MyInts
    where i32: ::core::ops::MulAssign<__RhsT>
{
    fn mul_assign(&mut self, rhs: __RhsT) {
        self.0.mul_assign(rhs);
        self.1.mul_assign(rhs);
    }
}
```

The behaviour is similar with more or less fields, except for the fact that
`__RhsT` does not need to implement `Copy` when only a single field is present.




## Regular structs

When deriving `MulAssign` for a regular struct with two fields like this:

```rust
# use derive_more::MulAssign;
#
#[derive(MulAssign)]
struct Point2D {
    x: i32,
    y: i32,
}
```

Code like this will be generated:

```rust
# struct Point2D {
#     x: i32,
#     y: i32,
# }
impl<__RhsT: ::core::marker::Copy> ::core::ops::MulAssign<__RhsT> for Point2D
    where i32: ::core::ops::MulAssign<__RhsT>
{
    fn mul_assign(&mut self, rhs: __RhsT) {
        self.x.mul_assign(rhs);
        self.y.mul_assign(rhs);
    }
}
```

The behaviour is again similar with more or less fields, except that `Copy`
doesn't have to be implemented for `__Rhst` when the struct has only a single
field.




## Enums

Deriving `MulAssign` for enums is not (yet) supported.
This has two reason, the first being that deriving `Mul` is also not implemented
for enums yet.
The second reason is the same as for `AddAssign`.
Even if it would be deriving `Mul` was implemented it would likely return a
`Result<EnumType>` instead of an `EnumType`.
Handling the case where it errors would be hard and maybe impossible.