Skip to content

Commit

Permalink
Attempt to resolve doc testing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Slabinski committed Nov 3, 2020
1 parent a7db7c3 commit 7e60fe1
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions doc/borrow_mut.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ 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
# use core::borrow::Borrow;
# use core::borrow::BorrowMut;
# #[macro_use] extern crate derive_more;
# fn main(){}
#[derive(BorrowMut)]
#[derive(Borrow, BorrowMut)]
struct MyWrapper(String);
```

Generates:

```rust
# 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 {
Expand All @@ -37,7 +42,8 @@ implements all `BorrowMut` for all types that `Vec<i32>` implements `BorrowMut`
```rust
# use core::borrow::BorrowMut;
# #[macro_use] extern crate derive_more;
#[derive(BorrowMut)]
#[derive(Borrow, BorrowMut)]
#[borrow(forward)]
#[borrow_mut(forward)]
struct SingleFieldForward(Vec<i32>);

Expand All @@ -51,6 +57,7 @@ fn main() {
This generates:

```rust
# #[derive(Borrow)]
# struct SingleFieldForward(Vec<i32>);
impl<__BorrowMutT: ?::core::marker::Sized> ::core::borrow::BorrowMut<__BorrowMutT> for SingleFieldForward
where
Expand All @@ -72,13 +79,16 @@ An implementation will be generated for each indicated field.
You can also exclude a specific field by using `#[borrow_mut(ignore)]`.

```rust
# use core::borrow::Borrow;
# use core::borrow::BorrowMut;
# #[macro_use] extern crate derive_more;
# fn main(){}
#[derive(BorrowMut)]
#[derive(Borrow, BorrowMut)]
struct MyWrapper {
#[borrow]
#[borrow_mut]
name: String,
#[borrow]
#[borrow_mut]
num: i32,
valid: bool,
Expand All @@ -90,7 +100,9 @@ struct MyWrapper {
Generates:

```
# use core::borrow::Borrow;
# use core::borrow::BorrowMut;
# #[derive(Borrow)]
# struct MyWrapper {
# name: String,
# num: i32,
Expand All @@ -113,6 +125,7 @@ Note that `BorrowMut<T>` may only be implemented once for any given type `T`. Th
mark more than one field of the same type with `#[borrow_mut]` will result in a compilation error.

```compile_fail
# use core::borrow::Borrow;
# use core::borrow::BorrowMut;
# #[macro_use] extern crate derive_more;
# fn main(){}
Expand Down

0 comments on commit 7e60fe1

Please sign in to comment.