File: deref_mut.md

package info (click to toggle)
rust-derive-more-impl 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: makefile: 2
file content (132 lines) | stat: -rw-r--r-- 2,714 bytes parent folder | download
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# What `#[derive(DerefMut)]` generates

Deriving `Deref` only works for a single field of a struct.
Furthermore it requires that the type also implements `Deref`, so usually
`Deref` should also be derived.
The resulting implementation of `Deref` will allow you to mutably dereference
the struct its member directly.

1. Dereferencing to the field, i.e. like if your type was a reference type.
2. Doing a dereference on the field, for when the field itself is a reference
   type like `&mut` and `Box`.

With `#[deref_mut]` or `#[deref_mut(ignore)]` it's possible to indicate the
field that you want to derive `DerefMut` for.




## Example usage

```rust
# use derive_more::{Deref, DerefMut};
#
#[derive(Deref, DerefMut)]
struct Num {
    num: i32,
}

#[derive(Deref, DerefMut)]
#[deref(forward)]
#[deref_mut(forward)]
struct MyBoxedInt(Box<i32>);

// You can specify the field you want to derive DerefMut for
#[derive(Deref, DerefMut)]
struct CoolVec {
    cool: bool,
    #[deref]
    #[deref_mut]
    vec: Vec<i32>,
}

let mut num = Num{num: 123};
let mut boxed = MyBoxedInt(Box::new(123));
let mut cool_vec = CoolVec{cool: true, vec: vec![123]};
*num += 123;
assert_eq!(246, *num);
*boxed += 1000;
assert_eq!(1123, *boxed);
cool_vec.push(456);
assert_eq!(vec![123, 456], *cool_vec);
```




## Structs

When deriving a non-forwarded `Deref` for a struct:

```rust
# use derive_more::{Deref, DerefMut};
#
#[derive(Deref, DerefMut)]
struct CoolVec {
    cool: bool,
    #[deref]
    #[deref_mut]
    vec: Vec<i32>,
}
```

Code like this will be generated:

```rust
# use ::core::ops::Deref;
# struct CoolVec {
#     cool: bool,
#     vec: Vec<i32>,
# }
# impl Deref for CoolVec {
#     type Target = Vec<i32>;
#     #[inline]
#     fn deref(&self) -> &Self::Target {
#         &self.vec
#     }
# }
impl derive_more::DerefMut for CoolVec {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.vec
    }
}
```

When deriving `DerefMut` for a tuple struct with one field:

```rust
# use derive_more::{Deref, DerefMut};
#
#[derive(Deref, DerefMut)]
#[deref(forward)]
#[deref_mut(forward)]
struct MyBoxedInt(Box<i32>);
```

When deriving a forwarded `DerefMut` for a struct:

```rust
# use ::core::ops::Deref;
# struct MyBoxedInt(Box<i32>);
# impl Deref for MyBoxedInt {
#     type Target = <Box<i32> as Deref>::Target;
#     #[inline]
#     fn deref(&self) -> &Self::Target {
#         <Box<i32> as Deref>::deref(&self.0)
#     }
# }
impl derive_more::DerefMut for MyBoxedInt {
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        <Box<i32> as derive_more::DerefMut>::deref_mut(&mut self.0)
    }
}
```




## Enums

Deriving `DerefMut` is not supported for enums.