File tree Expand file tree Collapse file tree 3 files changed +90
-29
lines changed Expand file tree Collapse file tree 3 files changed +90
-29
lines changed Original file line number Diff line number Diff line change @@ -42,20 +42,39 @@ struct OwnedWord {
42
42
}
43
43
44
44
impl borrowme :: ToOwned for Word <'_ > {
45
- type Owned = OwnedWord ;
46
-
47
- fn to_owned (& self ) -> OwnedWord {
48
- /* .. */
49
- }
45
+ /* .. */
50
46
}
51
47
52
48
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:
54
56
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 >,
58
65
}
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 );
59
78
```
60
79
61
80
<br >
Original file line number Diff line number Diff line change @@ -42,20 +42,39 @@ struct OwnedWord {
42
42
}
43
43
44
44
impl borrowme :: ToOwned for Word <'_ > {
45
- type Owned = OwnedWord ;
46
-
47
- fn to_owned (& self ) -> OwnedWord {
48
- /* .. */
49
- }
45
+ /* .. */
50
46
}
51
47
52
48
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:
54
56
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 >,
58
65
}
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 );
59
78
```
60
79
61
80
<br >
Original file line number Diff line number Diff line change 41
41
//! }
42
42
//!
43
43
//! 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!() }
50
47
//! }
51
48
//!
52
49
//! 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
+ //! }
54
70
//!
55
- //! fn borrow(&self) -> Word<'_> {
56
- //! /* .. */
57
- //! # todo!()
58
- //! }
71
+ //! #[borrowme]
72
+ //! struct Dictionary<'a> {
73
+ //! words: HashMap<&'a str, Word<'a>>,
59
74
//! }
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);
60
83
//! ```
61
84
//!
62
85
//! <br>
You can’t perform that action at this time.
0 commit comments