| 1 |
# What `#[derive(AsRef)]` generates |
| 2 |
|
| 3 |
Deriving `AsRef` generates one or more implementations of `AsRef`, each |
| 4 |
corresponding to one of the fields of the decorated type. |
| 5 |
This allows types which contain some `T` to be passed anywhere that an |
| 6 |
`AsRef<T>` is accepted. |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
## Newtypes and Structs with One Field |
| 12 |
|
| 13 |
When `AsRef` is derived for a newtype or struct with one field, a single |
| 14 |
implementation is generated to expose the underlying field. |
| 15 |
|
| 16 |
```rust |
| 17 |
# use derive_more::AsRef; |
| 18 |
# |
| 19 |
#[derive(AsRef)] |
| 20 |
struct MyWrapper(String); |
| 21 |
``` |
| 22 |
|
| 23 |
Generates: |
| 24 |
|
| 25 |
```rust |
| 26 |
# struct MyWrapper(String); |
| 27 |
impl AsRef<String> for MyWrapper { |
| 28 |
fn as_ref(&self) -> &String { |
| 29 |
&self.0 |
| 30 |
} |
| 31 |
} |
| 32 |
``` |
| 33 |
|
| 34 |
It's also possible to use the `#[as_ref(forward)]` attribute to forward |
| 35 |
to the `as_ref` implementation of the field. So here `SingleFieldForward` |
| 36 |
implements all `AsRef` for all types that `Vec<i32>` implements `AsRef` for. |
| 37 |
|
| 38 |
```rust |
| 39 |
# use derive_more::AsRef; |
| 40 |
# |
| 41 |
#[derive(AsRef)] |
| 42 |
#[as_ref(forward)] |
| 43 |
struct SingleFieldForward(Vec<i32>); |
| 44 |
|
| 45 |
let item = SingleFieldForward(vec![]); |
| 46 |
let _: &[i32] = (&item).as_ref(); |
| 47 |
``` |
| 48 |
|
| 49 |
This generates: |
| 50 |
|
| 51 |
```rust |
| 52 |
# struct SingleFieldForward(Vec<i32>); |
| 53 |
impl<__AsRefT: ?::core::marker::Sized> ::core::convert::AsRef<__AsRefT> for SingleFieldForward |
| 54 |
where |
| 55 |
Vec<i32>: ::core::convert::AsRef<__AsRefT>, |
| 56 |
{ |
| 57 |
#[inline] |
| 58 |
fn as_ref(&self) -> &__AsRefT { |
| 59 |
<Vec<i32> as ::core::convert::AsRef<__AsRefT>>::as_ref(&self.0) |
| 60 |
} |
| 61 |
} |
| 62 |
``` |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
## Structs with Multiple Fields |
| 68 |
|
| 69 |
When `AsRef` is derived for a struct with more than one field (including tuple |
| 70 |
structs), you must also mark one or more fields with the `#[as_ref]` attribute. |
| 71 |
An implementation will be generated for each indicated field. |
| 72 |
You can also exclude a specific field by using `#[as_ref(ignore)]`. |
| 73 |
|
| 74 |
```rust |
| 75 |
# use derive_more::AsRef; |
| 76 |
# |
| 77 |
#[derive(AsRef)] |
| 78 |
struct MyWrapper { |
| 79 |
#[as_ref] |
| 80 |
name: String, |
| 81 |
#[as_ref] |
| 82 |
num: i32, |
| 83 |
valid: bool, |
| 84 |
} |
| 85 |
``` |
| 86 |
|
| 87 |
Generates: |
| 88 |
|
| 89 |
```rust |
| 90 |
# struct MyWrapper { |
| 91 |
# name: String, |
| 92 |
# num: i32, |
| 93 |
# valid: bool, |
| 94 |
# } |
| 95 |
impl AsRef<String> for MyWrapper { |
| 96 |
fn as_ref(&self) -> &String { |
| 97 |
&self.name |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
impl AsRef<i32> for MyWrapper { |
| 102 |
fn as_ref(&self) -> &i32 { |
| 103 |
&self.num |
| 104 |
} |
| 105 |
} |
| 106 |
``` |
| 107 |
|
| 108 |
Note that `AsRef<T>` may only be implemented once for any given type `T`. |
| 109 |
This means any attempt to mark more than one field of the same type with |
| 110 |
`#[as_ref]` will result in a compilation error. |
| 111 |
|
| 112 |
```rust,compile_fail |
| 113 |
# use derive_more::AsRef; |
| 114 |
# |
| 115 |
// Error! Conflicting implementations of AsRef<String> |
| 116 |
#[derive(AsRef)] |
| 117 |
struct MyWrapper { |
| 118 |
#[as_ref] |
| 119 |
str1: String, |
| 120 |
#[as_ref] |
| 121 |
str2: String, |
| 122 |
} |
| 123 |
``` |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
## Enums |
| 129 |
|
| 130 |
Deriving `AsRef` for enums is not supported. |