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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// These links overwrite the ones in `README.md`
// to become proper intra-doc links in Rust docs.
//! [`From`]: macro@crate::From
//! [`Into`]: macro@crate::Into
//! [`FromStr`]: macro@crate::FromStr
//! [`TryInto`]: macro@crate::TryInto
//! [`IntoIterator`]: macro@crate::IntoIterator
//! [`AsRef`]: macro@crate::AsRef
//!
//! [`Debug`]: macro@crate::Debug
//! [`Display`-like]: macro@crate::Display
//!
//! [`Error`]: macro@crate::Error
//!
//! [`Index`]: macro@crate::Index
//! [`Deref`]: macro@crate::Deref
//! [`Not`-like]: macro@crate::Not
//! [`Add`-like]: macro@crate::Add
//! [`Mul`-like]: macro@crate::Mul
//! [`Sum`-like]: macro@crate::Sum
//! [`IndexMut`]: macro@crate::IndexMut
//! [`DerefMut`]: macro@crate::DerefMut
//! [`AddAssign`-like]: macro@crate::AddAssign
//! [`MulAssign`-like]: macro@crate::MulAssign
//!
//! [`Constructor`]: macro@crate::Constructor
//! [`IsVariant`]: macro@crate::IsVariant
//! [`Unwrap`]: macro@crate::Unwrap
//! [`TryUnwrap`]: macro@crate::TryUnwrap

// The README includes doctests requiring these features. To make sure that
// tests pass when not all features are provided we exclude it when the
// required features are not available.
#![cfg_attr(
    all(
        feature = "add",
        feature = "display",
        feature = "from",
        feature = "into"
    ),
    doc = include_str!("../README.md")
)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(all(not(feature = "std"), feature = "error"), feature(error_in_core))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(any(not(docsrs), ci), deny(rustdoc::all))]
#![forbid(non_ascii_idents, unsafe_code)]
#![warn(clippy::nonstandard_macro_braces)]

// Not public, but exported API. For macro expansion internals only.
#[doc(hidden)]
pub mod __private {
    #[cfg(feature = "debug")]
    pub use crate::fmt::{debug_tuple, DebugTuple};

    #[cfg(feature = "error")]
    pub use crate::vendor::thiserror::aserror::AsDynError;
}

// The modules containing error types and other helpers.

#[cfg(feature = "add")]
mod add;
#[cfg(feature = "add")]
pub use crate::add::{BinaryError, WrongVariantError};

#[cfg(any(feature = "add", feature = "not"))]
mod ops;
#[cfg(any(feature = "add", feature = "not"))]
pub use crate::ops::UnitError;

#[cfg(feature = "debug")]
mod fmt;

#[cfg(feature = "error")]
mod vendor;

#[cfg(feature = "from_str")]
mod r#str;
#[cfg(feature = "from_str")]
#[doc(inline)]
pub use crate::r#str::FromStrError;

#[cfg(feature = "try_into")]
mod convert;
#[cfg(feature = "try_into")]
#[doc(inline)]
pub use crate::convert::TryIntoError;

#[cfg(feature = "try_unwrap")]
mod try_unwrap;
#[cfg(feature = "try_unwrap")]
#[doc(inline)]
pub use crate::try_unwrap::TryUnwrapError;

// When re-exporting traits from std we need to do a pretty crazy trick, because we ONLY want
// to re-export the traits and not derives that are called the same in the std module,
// because those would conflict with our own. The way we do this is by first importing both
// the trait and possible derive into a separate module and re-export them. Then we wildcard import
// all the things from that module into the main module, but we also import our own derive by its
// exact name. Due to the way wildcard imports work in rust, that results in our own derive taking
// precedence over any derive from std. For some reason the named re-export of our own derive
// cannot be in in this (or really any) macro too. It will somehow still consider it a wildcard
// then and will result in this warning ambiguous_glob_reexports, and not actually exporting of our
// derive.
macro_rules! re_export_traits((
    $feature:literal, $new_module_name:ident, $module:path $(, $traits:ident)* $(,)?) => {
        #[cfg(all(feature = $feature, any(not(docsrs), ci)))]
        mod $new_module_name {
            #[doc(hidden)]
            pub use $module::{$($traits),*};
        }

        #[cfg(all(feature = $feature, any(not(docsrs), ci)))]
        #[doc(hidden)]
        pub use crate::$new_module_name::*;
    }
);

re_export_traits!(
    "add",
    add_traits,
    core::ops,
    Add,
    BitAnd,
    BitOr,
    BitXor,
    Sub,
);
re_export_traits!(
    "add_assign",
    add_assign_traits,
    core::ops,
    AddAssign,
    BitAndAssign,
    BitOrAssign,
    BitXorAssign,
    SubAssign,
);
re_export_traits!("as_mut", as_mut_traits, core::convert, AsMut);
re_export_traits!("as_ref", as_ref_traits, core::convert, AsRef);
re_export_traits!("debug", debug_traits, core::fmt, Debug);
re_export_traits!("deref", deref_traits, core::ops, Deref);
re_export_traits!("deref_mut", deref_mut_traits, core::ops, DerefMut);
re_export_traits!(
    "display",
    display_traits,
    core::fmt,
    Binary,
    Display,
    LowerExp,
    LowerHex,
    Octal,
    Pointer,
    UpperExp,
    UpperHex,
);

#[cfg(not(feature = "std"))]
re_export_traits!("error", error_traits, core::error, Error);
#[cfg(feature = "std")]
re_export_traits!("error", error_traits, std::error, Error);

re_export_traits!("from", from_traits, core::convert, From);

re_export_traits!("from_str", from_str_traits, core::str, FromStr);

re_export_traits!("index", index_traits, core::ops, Index);

re_export_traits!("index_mut", index_mut_traits, core::ops, IndexMut);

re_export_traits!("into", into_traits, core::convert, Into);

re_export_traits!(
    "into_iterator",
    into_iterator_traits,
    core::iter,
    IntoIterator,
);

re_export_traits!("mul", mul_traits, core::ops, Div, Mul, Rem, Shl, Shr);

#[cfg(feature = "mul_assign")]
re_export_traits!(
    "mul_assign",
    mul_assign_traits,
    core::ops,
    DivAssign,
    MulAssign,
    RemAssign,
    ShlAssign,
    ShrAssign,
);

re_export_traits!("not", not_traits, core::ops, Neg, Not);

re_export_traits!("sum", sum_traits, core::iter, Product, Sum);

re_export_traits!("try_into", try_into_traits, core::convert, TryInto);

// Now re-export our own derives by their exact name to overwrite any derives that the trait
// re-exporting might inadvertently pull into scope.
#[cfg(feature = "add")]
pub use derive_more_impl::{Add, BitAnd, BitOr, BitXor, Sub};

#[cfg(feature = "add_assign")]
pub use derive_more_impl::{
    AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, SubAssign,
};

#[cfg(feature = "as_mut")]
pub use derive_more_impl::AsMut;

#[cfg(feature = "as_ref")]
pub use derive_more_impl::AsRef;

#[cfg(feature = "constructor")]
pub use derive_more_impl::Constructor;

#[cfg(feature = "debug")]
pub use derive_more_impl::Debug;

#[cfg(feature = "deref")]
pub use derive_more_impl::Deref;

#[cfg(feature = "deref_mut")]
pub use derive_more_impl::DerefMut;

#[cfg(feature = "display")]
pub use derive_more_impl::{
    Binary, Display, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex,
};

#[cfg(feature = "error")]
pub use derive_more_impl::Error;

#[cfg(feature = "from")]
pub use derive_more_impl::From;

#[cfg(feature = "from_str")]
pub use derive_more_impl::FromStr;

#[cfg(feature = "index")]
pub use derive_more_impl::Index;

#[cfg(feature = "index_mut")]
pub use derive_more_impl::IndexMut;

#[cfg(feature = "into")]
pub use derive_more_impl::Into;

#[cfg(feature = "into_iterator")]
pub use derive_more_impl::IntoIterator;

#[cfg(feature = "is_variant")]
pub use derive_more_impl::IsVariant;

#[cfg(feature = "mul")]
pub use derive_more_impl::{Div, Mul, Rem, Shl, Shr};

#[cfg(feature = "mul_assign")]
pub use derive_more_impl::{DivAssign, MulAssign, RemAssign, ShlAssign, ShrAssign};

#[cfg(feature = "not")]
pub use derive_more_impl::{Neg, Not};

#[cfg(feature = "sum")]
pub use derive_more_impl::{Product, Sum};

#[cfg(feature = "try_into")]
pub use derive_more_impl::TryInto;

#[cfg(feature = "try_unwrap")]
pub use derive_more_impl::TryUnwrap;

#[cfg(feature = "unwrap")]
pub use derive_more_impl::Unwrap;

// Check if any feature is enabled
#[cfg(not(any(
    feature = "full",
    feature = "add",
    feature = "add_assign",
    feature = "as_mut",
    feature = "as_ref",
    feature = "constructor",
    feature = "debug",
    feature = "deref",
    feature = "deref_mut",
    feature = "display",
    feature = "error",
    feature = "from",
    feature = "from_str",
    feature = "index",
    feature = "index_mut",
    feature = "into",
    feature = "into_iterator",
    feature = "is_variant",
    feature = "mul",
    feature = "mul_assign",
    feature = "not",
    feature = "sum",
    feature = "try_into",
    feature = "try_unwrap",
    feature = "unwrap",
)))]
compile_error!(
    "at least one derive feature must be enabled (or the \"full\" one enabling all the derives)"
);