forked from JelteF/derive_more
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add stubs * document new feature * expand as_ref impl * expand as_mut impl * address ci issues
- Loading branch information
1 parent
6d1dbe3
commit d7a03c9
Showing
9 changed files
with
431 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
% What #[derive(AsMut)] generates | ||
|
||
Deriving `AsMut` generates one or more implementations of `AsMut`, each corresponding to one of the | ||
fields of the decorated type. This allows types which contain some `T` to be passed anywhere that an | ||
`AsMut<T>` is accepted. | ||
|
||
# Newtypes and Structs with One Field | ||
|
||
When `AsMut` is derived for a newtype or struct with one field, a single implementation is generated | ||
to expose the underlying field. | ||
|
||
```rust | ||
#[derive(AsMut)] | ||
struct MyWrapper(String); | ||
|
||
|
||
|
||
// Generates: | ||
|
||
impl AsMut<String> for MyWrapper { | ||
fn as_mut(&mut self) -> &mut String { | ||
&mut self.0 | ||
} | ||
} | ||
``` | ||
|
||
# Structs with Multiple Fields | ||
|
||
When `AsMut` is derived for a struct with more than one field (including tuple structs), you must | ||
also mark one or more fields with the `#[as_mut]` attribute. An implementation will be generated for | ||
each indicated field. | ||
|
||
```rust | ||
#[derive(AsMut)] | ||
struct MyWrapper { | ||
#[as_mut] | ||
name: String, | ||
#[as_mut] | ||
path: Path, | ||
valid: bool, | ||
} | ||
|
||
|
||
|
||
// Generates: | ||
|
||
impl AsMut<String> for MyWrapper { | ||
fn as_mut(&mut self) -> &mut String { | ||
&mut self.name | ||
} | ||
} | ||
|
||
impl AsMut<Path> for MyWrapper { | ||
fn as_mut(&mut self) -> &mut Path { | ||
&mut self.path | ||
} | ||
} | ||
``` | ||
|
||
Note that `AsMut<T>` may only be implemented once for any given type `T`. This means any attempt to | ||
mark more than one field of the same type with `#[as_mut]` will result in a compilation error. | ||
|
||
```rust | ||
// Error! Conflicting implementations of AsMut<String> | ||
#[derive(AsMut)] | ||
struct MyWrapper { | ||
#[as_mut] | ||
str1: String, | ||
#[as_mut] | ||
str2: String, | ||
} | ||
``` | ||
|
||
# Enums | ||
|
||
Deriving `AsMut` for enums is not supported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
% What #[derive(AsRef)] generates | ||
|
||
Deriving `AsRef` generates one or more implementations of `AsRef`, each corresponding to one of the | ||
fields of the decorated type. This allows types which contain some `T` to be passed anywhere that an | ||
`AsRef<T>` is accepted. | ||
|
||
# Newtypes and Structs with One Field | ||
|
||
When `AsRef` is derived for a newtype or struct with one field, a single implementation is generated | ||
to expose the underlying field. | ||
|
||
```rust | ||
#[derive(AsRef)] | ||
struct MyWrapper(String); | ||
|
||
|
||
|
||
// Generates: | ||
|
||
impl AsRef<String> for MyWrapper { | ||
fn as_ref(&self) -> &String { | ||
&self.0 | ||
} | ||
} | ||
``` | ||
|
||
# Structs with Multiple Fields | ||
|
||
When `AsRef` is derived for a struct with more than one field (including tuple structs), you must | ||
also mark one or more fields with the `#[as_ref]` attribute. An implementation will be generated for | ||
each indicated field. | ||
|
||
```rust | ||
#[derive(AsRef)] | ||
struct MyWrapper { | ||
#[as_ref] | ||
name: String, | ||
#[as_ref] | ||
path: Path, | ||
valid: bool, | ||
} | ||
|
||
|
||
|
||
// Generates: | ||
|
||
impl AsRef<String> for MyWrapper { | ||
fn as_ref(&self) -> &String { | ||
&self.name | ||
} | ||
} | ||
|
||
impl AsRef<Path> for MyWrapper { | ||
fn as_ref(&self) -> &Path { | ||
&self.path | ||
} | ||
} | ||
``` | ||
|
||
Note that `AsRef<T>` may only be implemented once for any given type `T`. This means any attempt to | ||
mark more than one field of the same type with `#[as_ref]` will result in a compilation error. | ||
|
||
```rust | ||
// Error! Conflicting implementations of AsRef<String> | ||
#[derive(AsRef)] | ||
struct MyWrapper { | ||
#[as_ref] | ||
str1: String, | ||
#[as_ref] | ||
str2: String, | ||
} | ||
``` | ||
|
||
# Enums | ||
|
||
Deriving `AsRef` for enums is not supported. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::DeriveInput; | ||
use crate::utils; | ||
|
||
|
||
pub fn expand(input: &DeriveInput, _: &str) -> TokenStream { | ||
|
||
let input_type = &input.ident; | ||
let (impl_generics, input_generics, where_clause) = input.generics.split_for_impl(); | ||
let (field_type, field_ident) = utils::extract_field_info(&input.data, "as_mut"); | ||
|
||
quote! {#( | ||
impl#impl_generics ::core::convert::AsMut<#field_type> for #input_type#input_generics | ||
#where_clause | ||
{ | ||
#[inline] | ||
fn as_mut(&mut self) -> &mut #field_type { | ||
&mut self.#field_ident | ||
} | ||
} | ||
)*} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::DeriveInput; | ||
use crate::utils; | ||
|
||
|
||
pub fn expand(input: &DeriveInput, _: &str) -> TokenStream { | ||
|
||
let input_type = &input.ident; | ||
let (impl_generics, input_generics, where_clause) = input.generics.split_for_impl(); | ||
let (field_type, field_ident) = utils::extract_field_info(&input.data, "as_ref"); | ||
|
||
quote! {#( | ||
impl#impl_generics ::core::convert::AsRef<#field_type> for #input_type#input_generics | ||
#where_clause | ||
{ | ||
#[inline] | ||
fn as_ref(&self) -> &#field_type { | ||
&self.#field_ident | ||
} | ||
} | ||
)*} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.