1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//! # Derive macro for structs/enums with Cow
//!
//! To automatically convert `Type<'a>` to `Type<'static>` and more.
//!
//! # Example
//!
//! This can be very helpful for types which use borrow with serde.
//!
//! ```rust
//! # use std::borrow::Cow;
//! # use ownable::{IntoOwned, ToBorrowed, ToOwned};
//! #[derive(IntoOwned, ToBorrowed, ToOwned)]
//! // #[derive(serde::Serialize, serde::Deserialize)]
//! pub struct Type<'a> {
//! // #[serde(borrow)]
//! cow: Cow<'a, str>,
//! #[ownable(clone)] owned: String, // always clone this field
//! }
//! ```
//!
//! Will derive something functionally similar to:
//! ```rust
//! # extern crate alloc;extern crate core;
//! # use core::borrow::Borrow;
//! # use std::borrow::Cow;
//! # struct Type<'a>{cow: Cow<'a, str>,owned: String}
//! impl Type<'_> {
//! /// Copy the structure and clone the original values if it's not owned.
//! /// This is always a deep copy of the structure.
//! pub fn into_owned(self) -> Type<'static> {
//! Type {
//! cow: Cow::Owned(Cow::into_owned(self.cow)),
//! owned: self.owned.clone(), // always cloned, as requested
//! }
//! }
//! /// Copy the structure and clone the original values.
//! /// This is always a deep copy.
//! pub fn to_owned(&self) -> Type<'static> {
//! Type {
//! cow: Cow::Owned(str::to_owned(self.cow.borrow())),
//! owned: self.owned.clone(), // always cloned, as requested
//! }
//! }
//! /// Copy the structure and reference the original values.
//! /// This is always a deep copy of the structure.
//! pub fn to_borrowed(&self) -> Type {
//! Type {
//! cow: Cow::Borrowed(self.cow.borrow()),
//! owned: self.owned.clone(), // always cloned, as requested
//! }
//! }
//! }
//! ```
//!
//! But actually each function only calls a function of [traits](crate::traits), which are derived.
//!
//! If the derive does not work it can be implemented by hand and still derived for types which use it.
//!
//! # Generics
//!
//! The derive macro supports all kinds of generics: lifetimes, types, consts. And the first
//! two with bounds and all as many times as you want.
//!
//! # References
//!
//! References are not supported out of the box, because they can't be changed into an owned type.
//!
//! But it's possible to specify which lifetime(s) are used solely for references and then those
//! will be always copied (the reference) and thus the lifetime is not changed.
//!
//! Please note that not only the type containing a reference but also types containing such a
//! type are required to be marked.
//!
//! ## Example
//!
//! ```rust
//! # use std::borrow::Cow;
//! # use ownable::{IntoOwned, ToBorrowed, ToOwned};
//! #[derive(IntoOwned, ToBorrowed, ToOwned)]
//! #[ownable(reference = "'b")]
//! pub struct Inner<'a, 'b> {
//! cow: Cow<'a, str>,
//! referenced: &'b str,
//! }
//!
//! // Also types, containing types with references, must be marked.
//! #[derive(IntoOwned, ToBorrowed, ToOwned)]
//! #[ownable(reference = "'b")]
//! pub struct Outer<'a, 'b> {
//! inner: Inner<'a, 'b>,
//! }
//! ```
//! Will derive functions with these signatures:
//! ```rust
//! # use std::borrow::Cow;
//! # struct Inner<'a, 'b> {
//! # cow: Cow<'a, str>,
//! # referenced: &'b str,
//! # }
//! impl<'b> Inner<'_, 'b> {
//! pub fn into_owned(self) -> Inner<'static, 'b> {
//! // Call the trait, which is also derived
//! # todo!()
//! }
//! pub fn to_owned(&self) -> Inner<'static, 'b> {
//! // Call the trait, which is also derived
//! # todo!()
//! }
//! pub fn to_borrowed(&self) -> Inner<'_, 'b> {
//! // Call the trait, which is also derived
//! # todo!()
//! }
//! }
//!
//! // The `Outer` will look similar.
//! ```
//!
//! # Possible Errors
//!
//! If the following error occurs then one of the fields has a missing trait.
//! ```text
//! error[E0277]: the trait bound `String: IntoOwned` is not satisfied
//! ```
//!
//! This can sometimes be fixed with `#[ownable(clone)]` as seen in the example above,
//! otherwise [`AsCopy`](crate::AsCopy)/[`AsClone`](crate::AsClone) can help.
//!
//! And as the last resort the impl for the surrounding structure can be hand written.
//!
//! # Attributes
//!
//! ## clone
//!
//! With `#[ownable(clone)]` and `#[ownable(clone = false|true)]` it's possible to denote that this
//! enum|struct/variant/field should always be cloned. It can be overwritten (i.e. set to true at
//! top level and then false at the fields to not be cloned).
//!
//! For an example see the at the top.
//!
//! ## function
//!
//! With `#[ownable(function = false)]` at top level (enum/struct) the functions mentioned above
//! are not implemented, only the [traits](crate::traits).
//!
//! ## reference
//!
//! With `#[ownable(reference = "..")]` one or more comma separated lifetimes can be supplied to
//! be used for references only, see [References](#references) above.
//!
//! # AsCopy/AsClone
//!
//! If the impls for the copy types are not enough or `#[ownable(clone)]` does not work in that
//! position then [`AsCopy`](crate::AsCopy) and [`AsClone`](crate::AsClone) can be used to wrap a value which then
//! works in this environment as expected. Both are transparent and do use only exact the same
//! space as the original type and all impls (Eq, Display, Hash, ...) only pass the calls though
//! to the inner type
//!
//! ## Example
//!
//! Example of an more complex type:
//! ```rust
//! # use std::borrow::Cow;
//! # use std::collections::HashMap;
//! # use ownable::{AsClone, IntoOwned, ToBorrowed, ToOwned};
//! #[derive(IntoOwned, ToBorrowed, ToOwned)]
//! pub struct Type<'a> {
//! cow: Cow<'a, str>,
//! nested: Option<Box<Type<'a>>>,
//! map: HashMap<AsClone<String>, Cow<'a, str>>,
//! #[ownable(clone)] owned: String, // always clone this field
//! number: usize, // many copy types have a trait impl, and thus can be used without the `#[ownable(clone)]`
//! }
//! ```
//!
//! # Features
//! * `std` - Traits are also implemented for types which are not in [core](::core) or [alloc](::alloc) (currently [`HashMap`](::std::collections::HashMap) and [`HashSet`](::std::collections::HashSet)).
//!
//! `std` is enabled by default.
//!
//! ## Usage
//! With defaults (includes `std`):
//! ```toml
//! [dependencies]
//! ownable = "1.0"
//! ```
//!
//! With `no_std` (but still requires alloc):
//! ```toml
//! [dependencies]
//! ownable = { version = "1.0", default-features = false }
//! ```
extern crate alloc;
pub use ;
pub use ;