Skip to content

Commit e70412e

Browse files
committed
Explain composition
1 parent 003a389 commit e70412e

File tree

3 files changed

+90
-29
lines changed

3 files changed

+90
-29
lines changed

README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,39 @@ struct OwnedWord {
4242
}
4343

4444
impl borrowme::ToOwned for Word<'_> {
45-
type Owned = OwnedWord;
46-
47-
fn to_owned(&self) -> OwnedWord {
48-
/* .. */
49-
}
45+
/* .. */
5046
}
5147

5248
impl borrowme::Borrow for OwnedWord {
53-
type Target<'a> = Word<'a>;
49+
/* .. */
50+
}
51+
```
52+
53+
By itself this isn't much, but here's the big trick. Types using this crate
54+
can be composed and converted into their borrowed or owned counterparts as
55+
needed:
5456

55-
fn borrow(&self) -> Word<'_> {
56-
/* .. */
57-
}
57+
```rust
58+
use std::collections::HashMap;
59+
60+
#[borrowme]
61+
struct Word<'a> {
62+
text: &'a str,
63+
lang: Option<&'a str>,
64+
examples: Vec<&'a str>,
5865
}
66+
67+
#[borrowme]
68+
struct Dictionary<'a> {
69+
words: HashMap<&'a str, Word<'a>>,
70+
}
71+
72+
let dictionary = Dictionary {
73+
/* .. */
74+
};
75+
76+
let owned_dictionary: OwnedDictionary = borrowme::to_owned(&dictionary);
77+
let dictionary2: Dictionary<'_> = borrowme::borrow(&owned_dictionary);
5978
```
6079

6180
<br>

crates/borrowme/README.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,39 @@ struct OwnedWord {
4242
}
4343

4444
impl borrowme::ToOwned for Word<'_> {
45-
type Owned = OwnedWord;
46-
47-
fn to_owned(&self) -> OwnedWord {
48-
/* .. */
49-
}
45+
/* .. */
5046
}
5147

5248
impl borrowme::Borrow for OwnedWord {
53-
type Target<'a> = Word<'a>;
49+
/* .. */
50+
}
51+
```
52+
53+
By itself this isn't much, but here's the big trick. Types using this crate
54+
can be composed and converted into their borrowed or owned counterparts as
55+
needed:
5456

55-
fn borrow(&self) -> Word<'_> {
56-
/* .. */
57-
}
57+
```rust
58+
use std::collections::HashMap;
59+
60+
#[borrowme]
61+
struct Word<'a> {
62+
text: &'a str,
63+
lang: Option<&'a str>,
64+
examples: Vec<&'a str>,
5865
}
66+
67+
#[borrowme]
68+
struct Dictionary<'a> {
69+
words: HashMap<&'a str, Word<'a>>,
70+
}
71+
72+
let dictionary = Dictionary {
73+
/* .. */
74+
};
75+
76+
let owned_dictionary: OwnedDictionary = borrowme::to_owned(&dictionary);
77+
let dictionary2: Dictionary<'_> = borrowme::borrow(&owned_dictionary);
5978
```
6079

6180
<br>

crates/borrowme/src/lib.rs

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,45 @@
4141
//! }
4242
//!
4343
//! impl borrowme::ToOwned for Word<'_> {
44-
//! type Owned = OwnedWord;
45-
//!
46-
//! fn to_owned(&self) -> OwnedWord {
47-
//! /* .. */
48-
//! # todo!()
49-
//! }
44+
//! /* .. */
45+
//! # type Owned = OwnedWord;
46+
//! # fn to_owned(&self) -> OwnedWord { todo!() }
5047
//! }
5148
//!
5249
//! impl borrowme::Borrow for OwnedWord {
53-
//! type Target<'a> = Word<'a>;
50+
//! /* .. */
51+
//! # type Target<'a> = Word<'a>;
52+
//! # fn borrow(&self) -> Word<'_> { todo!() }
53+
//! }
54+
//! ```
55+
//!
56+
//! By itself this isn't much, but here's the big trick. Types using this crate
57+
//! can be composed and converted into their borrowed or owned counterparts as
58+
//! needed:
59+
//!
60+
//! ```
61+
//! # use borrowme::borrowme;
62+
//! use std::collections::HashMap;
63+
//!
64+
//! #[borrowme]
65+
//! struct Word<'a> {
66+
//! text: &'a str,
67+
//! lang: Option<&'a str>,
68+
//! examples: Vec<&'a str>,
69+
//! }
5470
//!
55-
//! fn borrow(&self) -> Word<'_> {
56-
//! /* .. */
57-
//! # todo!()
58-
//! }
71+
//! #[borrowme]
72+
//! struct Dictionary<'a> {
73+
//! words: HashMap<&'a str, Word<'a>>,
5974
//! }
75+
//!
76+
//! let dictionary = Dictionary {
77+
//! /* .. */
78+
//! # words: HashMap::new(),
79+
//! };
80+
//!
81+
//! let owned_dictionary: OwnedDictionary = borrowme::to_owned(&dictionary);
82+
//! let dictionary2: Dictionary<'_> = borrowme::borrow(&owned_dictionary);
6083
//! ```
6184
//!
6285
//! <br>

0 commit comments

Comments
 (0)