-
Notifications
You must be signed in to change notification settings - Fork 123
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
Add Borrow
and BorrowMut
derives
#145
Open
Slabity
wants to merge
9
commits into
JelteF:master
Choose a base branch
from
Slabity:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1fcfee0
Add Borrow and BorrowMut derives
de416ce
Fix README links
e7d220c
Fix incorrect name
c21ddd6
Resolve rustfmt complaints
9c76281
Resolve doc checks
cf82779
Merge from upstream
Slabity 70d2049
Fix doc
Slabity 42f2560
Fix typo
Slabity 3cc737a
Add borrow tests
Slabity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,130 @@ | ||
% What #[derive(Borrow)] generates | ||
|
||
Deriving `Borrow` generates one or more implementations of `Borrow`, each | ||
corresponding to one of the fields of the decorated type. | ||
This allows types which contain some `T` to be passed anywhere that an | ||
`Borrow<T>` is accepted. | ||
|
||
# Newtypes and Structs with One Field | ||
|
||
When `Borrow` is derived for a newtype or struct with one field, a single | ||
implementation is generated to expose the underlying field. | ||
|
||
```rust | ||
# use core::borrow::Borrow; | ||
# #[macro_use] extern crate derive_more; | ||
# fn main(){} | ||
#[derive(Borrow)] | ||
struct MyWrapper(String); | ||
``` | ||
|
||
Generates: | ||
|
||
```rust | ||
# use core::borrow::Borrow; | ||
# struct MyWrapper(String); | ||
impl Borrow<String> for MyWrapper { | ||
fn borrow(&self) -> &String { | ||
&self.0 | ||
} | ||
} | ||
``` | ||
|
||
It's also possible to use the `#[borrow(forward)]` attribute to forward | ||
to the `borrow` implementation of the field. So here `SigleFieldForward` | ||
implements all `Borrow` for all types that `Vec<i32>` implements `Borrow` for. | ||
|
||
```rust | ||
# use core::borrow::Borrow; | ||
# #[macro_use] extern crate derive_more; | ||
#[derive(Borrow)] | ||
#[borrow(forward)] | ||
struct SingleFieldForward(Vec<i32>); | ||
|
||
fn main() { | ||
let item = SingleFieldForward(vec![]); | ||
let _: &[i32] = (&item).borrow(); | ||
} | ||
|
||
``` | ||
|
||
This generates: | ||
|
||
```rust | ||
# struct SingleFieldForward(Vec<i32>); | ||
impl<__BorrowT: ?::core::marker::Sized> ::core::borrow::Borrow<__BorrowT> for SingleFieldForward | ||
where | ||
Vec<i32>: ::core::borrow::Borrow<__BorrowT>, | ||
{ | ||
#[inline] | ||
fn borrow(&self) -> &__BorrowT { | ||
<Vec<i32> as ::core::borrow::Borrow<__BorrowT>>::borrow(&self.0) | ||
} | ||
} | ||
``` | ||
|
||
# Structs with Multiple Fields | ||
|
||
When `Borrow` is derived for a struct with more than one field (including tuple | ||
structs), you must also mark one or more fields with the `#[borrow]` attribute. | ||
An implementation will be generated for each indicated field. | ||
You can also exclude a specific field by using `#[borrow(ignore)]`. | ||
|
||
```rust | ||
# use core::borrow::Borrow; | ||
# #[macro_use] extern crate derive_more; | ||
# fn main(){} | ||
#[derive(Borrow)] | ||
struct MyWrapper { | ||
#[borrow] | ||
name: String, | ||
#[borrow] | ||
num: i32, | ||
valid: bool, | ||
} | ||
|
||
``` | ||
|
||
Generates: | ||
|
||
```rust | ||
# use core::borrow::Borrow; | ||
# struct MyWrapper { | ||
# name: String, | ||
# num: i32, | ||
# valid: bool, | ||
# } | ||
impl Borrow<String> for MyWrapper { | ||
fn borrow(&self) -> &String { | ||
&self.name | ||
} | ||
} | ||
|
||
impl Borrow<i32> for MyWrapper { | ||
fn borrow(&self) -> &i32 { | ||
&self.num | ||
} | ||
} | ||
``` | ||
|
||
Note that `Borrow<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 | ||
`#[borrow]` will result in a compilation error. | ||
|
||
```compile_fail | ||
# use core::borrow::Borrow; | ||
# #[macro_use] extern crate derive_more; | ||
# fn main(){} | ||
// Error! Conflicting implementations of Borrow<String> | ||
#[derive(Borrow)] | ||
struct MyWrapper { | ||
#[borrow] | ||
str1: String, | ||
#[borrow] | ||
str2: String, | ||
} | ||
``` | ||
|
||
# Enums | ||
|
||
Deriving `Borrow` 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,149 @@ | ||
% What #[derive(BorrowMut)] generates | ||
|
||
Deriving `BorrowMut` generates one or more implementations of `BorrowMut`, each | ||
corresponding to one of the fields of the decorated type. | ||
This allows types which contain some `T` to be passed anywhere that an | ||
`BorrowMut<T>` is accepted. | ||
|
||
Note that `BorrowMut<T>` expects the type to also implement `Borrow<T>`. | ||
|
||
# Newtypes and Structs with One Field | ||
|
||
When `BorrowMut` is derived for a newtype or struct with one field, a single | ||
implementation is generated to expose the underlying field. | ||
|
||
```rust | ||
# #[macro_use] extern crate derive_more; | ||
# use core::borrow::Borrow; | ||
# use core::borrow::BorrowMut; | ||
# fn main(){} | ||
#[derive(Borrow, BorrowMut)] | ||
struct MyWrapper(String); | ||
``` | ||
|
||
Generates: | ||
|
||
```rust | ||
# #[macro_use] extern crate derive_more; | ||
# use core::borrow::Borrow; | ||
# use core::borrow::BorrowMut; | ||
# #[derive(Borrow)] | ||
# struct MyWrapper(String); | ||
impl BorrowMut<String> for MyWrapper { | ||
fn borrow_mut(&mut self) -> &mut String { | ||
&mut self.0 | ||
} | ||
} | ||
``` | ||
|
||
It's also possible to use the `#[borrow_mut(forward)]` attribute to forward | ||
to the `borrow_mut` implementation of the field. So here `SigleFieldForward` | ||
implements all `BorrowMut` for all types that `Vec<i32>` implements `BorrowMut` for. | ||
|
||
```rust | ||
# #[macro_use] extern crate derive_more; | ||
# use core::borrow::Borrow; | ||
# use core::borrow::BorrowMut; | ||
#[derive(Borrow, BorrowMut)] | ||
#[borrow(forward)] | ||
#[borrow_mut(forward)] | ||
struct SingleFieldForward(Vec<i32>); | ||
|
||
fn main() { | ||
let mut item = SingleFieldForward(vec![]); | ||
let _: &mut [i32] = (&mut item).borrow_mut(); | ||
} | ||
|
||
``` | ||
|
||
This generates: | ||
|
||
```rust | ||
# #[macro_use] extern crate derive_more; | ||
# #[derive(Borrow)] | ||
# #[borrow(forward)] | ||
# struct SingleFieldForward(Vec<i32>); | ||
impl<__BorrowMutT: ?::core::marker::Sized> ::core::borrow::BorrowMut<__BorrowMutT> for SingleFieldForward | ||
where | ||
Vec<i32>: ::core::borrow::BorrowMut<__BorrowMutT>, | ||
{ | ||
#[inline] | ||
fn borrow_mut(&mut self) -> &mut __BorrowMutT { | ||
<Vec<i32> as ::core::borrow::BorrowMut<__BorrowMutT>>::borrow_mut(&mut self.0) | ||
} | ||
} | ||
``` | ||
|
||
|
||
# Structs with Multiple Fields | ||
|
||
When `BorrowMut` is derived for a struct with more than one field (including tuple | ||
structs), you must also mark one or more fields with the `#[borrow_mut]` attribute. | ||
An implementation will be generated for each indicated field. | ||
You can also exclude a specific field by using `#[borrow_mut(ignore)]`. | ||
|
||
```rust | ||
# #[macro_use] extern crate derive_more; | ||
# use core::borrow::Borrow; | ||
# use core::borrow::BorrowMut; | ||
# fn main(){} | ||
#[derive(Borrow, BorrowMut)] | ||
struct MyWrapper { | ||
#[borrow] | ||
#[borrow_mut] | ||
name: String, | ||
#[borrow] | ||
#[borrow_mut] | ||
num: i32, | ||
valid: bool, | ||
} | ||
|
||
|
||
``` | ||
|
||
Generates: | ||
|
||
``` | ||
# #[macro_use] extern crate derive_more; | ||
# use core::borrow::Borrow; | ||
# use core::borrow::BorrowMut; | ||
# #[derive(Borrow)] | ||
# struct MyWrapper { | ||
# name: String, | ||
# num: i32, | ||
# valid: bool, | ||
# } | ||
impl BorrowMut<String> for MyWrapper { | ||
fn borrow_mut(&mut self) -> &mut String { | ||
&mut self.name | ||
} | ||
} | ||
|
||
impl BorrowMut<i32> for MyWrapper { | ||
fn borrow_mut(&mut self) -> &mut i32 { | ||
&mut self.num | ||
} | ||
} | ||
``` | ||
|
||
Note that `BorrowMut<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 `#[borrow_mut]` will result in a compilation error. | ||
|
||
```compile_fail | ||
# #[macro_use] extern crate derive_more; | ||
# use core::borrow::Borrow; | ||
# use core::borrow::BorrowMut; | ||
# fn main(){} | ||
// Error! Conflicting implementations of BorrowMut<String> | ||
#[derive(BorrowMut)] | ||
struct MyWrapper { | ||
#[borrow_mut] | ||
str1: String, | ||
#[borrow_mut] | ||
str2: String, | ||
} | ||
``` | ||
|
||
# Enums | ||
|
||
Deriving `BorrowMut` for enums is not supported. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed