@@ -5,23 +5,28 @@ corresponding to one of the fields of the decorated type.
55This allows types which contain some ` T ` to be passed anywhere that an
66` BorrowMut<T> ` is accepted.
77
8+ Note that ` BorrowMut<T> ` expects the type to also implement ` Borrow<T> ` .
9+
810# Newtypes and Structs with One Field
911
1012When ` BorrowMut ` is derived for a newtype or struct with one field, a single
1113implementation is generated to expose the underlying field.
1214
1315``` rust
16+ # use core :: borrow :: Borrow ;
1417# use core :: borrow :: BorrowMut ;
1518# #[macro_use] extern crate derive_more;
1619# fn main (){}
17- #[derive(BorrowMut )]
20+ #[derive(Borrow , BorrowMut )]
1821struct MyWrapper (String );
1922```
2023
2124Generates:
2225
2326``` rust
27+ # use core :: borrow :: Borrow ;
2428# use core :: borrow :: BorrowMut ;
29+ # #[derive(Borrow )]
2530# struct MyWrapper (String );
2631impl BorrowMut <String > for MyWrapper {
2732 fn borrow_mut (& mut self ) -> & mut String {
@@ -37,7 +42,8 @@ implements all `BorrowMut` for all types that `Vec<i32>` implements `BorrowMut`
3742``` rust
3843# use core :: borrow :: BorrowMut ;
3944# #[macro_use] extern crate derive_more;
40- #[derive(BorrowMut )]
45+ #[derive(Borrow , BorrowMut )]
46+ #[borrow(forward)]
4147#[borrow_mut(forward)]
4248struct SingleFieldForward (Vec <i32 >);
4349
@@ -51,6 +57,7 @@ fn main() {
5157This generates:
5258
5359``` rust
60+ # #[derive(Borrow )]
5461# struct SingleFieldForward (Vec <i32 >);
5562impl <__BorrowMutT: ? :: core :: marker :: Sized > :: core :: borrow :: BorrowMut <__BorrowMutT> for SingleFieldForward
5663where
@@ -72,13 +79,16 @@ An implementation will be generated for each indicated field.
7279You can also exclude a specific field by using ` #[borrow_mut(ignore)] ` .
7380
7481``` rust
82+ # use core :: borrow :: Borrow ;
7583# use core :: borrow :: BorrowMut ;
7684# #[macro_use] extern crate derive_more;
7785# fn main (){}
78- #[derive(BorrowMut )]
86+ #[derive(Borrow , BorrowMut )]
7987struct MyWrapper {
88+ #[borrow]
8089 #[borrow_mut]
8190 name : String ,
91+ #[borrow]
8292 #[borrow_mut]
8393 num : i32 ,
8494 valid : bool ,
@@ -90,7 +100,9 @@ struct MyWrapper {
90100Generates:
91101
92102```
103+ # use core::borrow::Borrow;
93104# use core::borrow::BorrowMut;
105+ # #[derive(Borrow)]
94106# struct MyWrapper {
95107# name: String,
96108# num: i32,
@@ -113,6 +125,7 @@ Note that `BorrowMut<T>` may only be implemented once for any given type `T`. Th
113125mark more than one field of the same type with ` #[borrow_mut] ` will result in a compilation error.
114126
115127``` compile_fail
128+ # use core::borrow::Borrow;
116129# use core::borrow::BorrowMut;
117130# #[macro_use] extern crate derive_more;
118131# fn main(){}
0 commit comments