newer_type/lib.rs
1#![doc = include_str!("./README.md")]
2
3// internal
4pub use newer_type_macro::__implement_internal;
5
6/// Implement a trait for given enum or struct. The trait should be defined with
7/// [`target`] attribute.
8///
9/// # Example
10///
11/// ```
12/// use newer_type::implement;
13/// use newer_type::traits::{Extend, PartialEq};
14///
15/// #[implement(Extend<usize>)]
16/// struct Example1(Vec<usize>);
17///
18/// #[implement(Extend<T>)]
19/// struct Example2<T>(Vec<T>);
20///
21/// #[implement(for<T> PartialEq<T>)]
22/// struct Example3(String);
23///
24/// #[implement(for<T: std::fmt::Debug> PartialEq<T>)]
25/// struct Example4<U>(U);
26/// ```
27pub use newer_type_macro::implement;
28
29/// Define a trait for use of [`implement`] macro.
30///
31/// # Arguments (all optional)
32///
33/// - `alternative` ... Trait. If specified, implement this trait instead of the
34/// target trait itself. The target trait is used only for an argument of
35/// [`implement`] macro. See implementation of [`traits`].
36/// - `newer_type` ... Set path to `newer_type` crate. Defaults to
37/// `::newer_type`. Example: `::your_crate::_export::newer_type`.
38///
39/// # Example
40///
41/// ```
42/// use newer_type::target;
43///
44/// #[target]
45/// trait MyTrait {
46/// fn my_fn(&self) -> ::core::primitive::usize;
47/// }
48/// ```
49///
50/// ```
51/// use newer_type::target;
52/// type TypeFromContext = usize;
53/// #[target]
54/// trait MyTrait {
55/// fn my_fn(&self, t: TypeFromContext) -> Box<usize>;
56/// }
57/// ```
58pub use newer_type_macro::target;
59
60#[doc(hidden)]
61pub struct Alternate(::core::convert::Infallible);
62
63#[doc(hidden)]
64pub trait Repeater<const TRAIT_NUM: u64, const N: usize> {
65 type Type;
66}
67
68pub mod traits {
69 use super::*;
70 #[cfg(doc)]
71 use crate as newer_type;
72
73 macro_rules! emit_traits {
74 () => {};
75 (
76 $(#[doc = $doc_example:literal])*
77 $([$($trait_params:ident),*$(,)?])?{
78 #[target(alternative = $alternative:path)]
79 pub trait $trait_name:ident $($trait_contents:tt)*
80 }
81 $($t:tt)*
82 ) => {
83 #[target(alternative = $alternative, newer_type = $crate)]
84 #[doc = concat!("This trait is empty declaration of [`", stringify!($alternative), "`] to be used")]
85 #[doc = "with [`newer_type::implement`]."]
86 ///
87 /// # Example
88 ///
89 $(#[doc = $doc_example])*
90 pub trait $trait_name $($trait_contents)*
91 emit_traits!{ $($t)* }
92 };
93 }
94
95 emit_traits! {
96 /// ```
97 /// # use newer_type::implement;
98 /// #[implement(newer_type::traits::IntoIterator)]
99 /// struct MyStruct {
100 /// slot: Vec<u8>,
101 /// }
102 /// ```
103 {
104 #[target(alternative = ::core::iter::IntoIterator)]
105 pub trait IntoIterator {
106 type Item;
107 type IntoIter: ::core::iter::Iterator<Item = Self::Item>;
108 fn into_iter(self) -> Self::IntoIter;
109 }
110 }
111
112 /// ```
113 /// # use newer_type::implement;
114 /// #[implement(for<A> newer_type::traits::Extend<A>)]
115 /// struct MyStruct {
116 /// slot: Vec<u8>,
117 /// }
118 /// ```
119 [A]{
120 #[target(alternative = ::core::iter::Extend)]
121 pub trait Extend<A> {
122 fn extend<T>(&mut self, iter: T)
123 where
124 T: ::core::iter::IntoIterator<Item = A>;
125 }
126 }
127
128 /// ```
129 /// # use newer_type::implement;
130 /// #[implement(newer_type::traits::Iterator)]
131 /// struct MyStruct {
132 /// slot: std::vec::IntoIter<u8>,
133 /// }
134 /// ```
135 {
136 #[target(alternative = ::core::iter::Iterator)]
137 pub trait Iterator {
138 type Item;
139 fn next(&mut self) -> ::core::option::Option<Self::Item>;
140 fn size_hint(
141 &self,
142 ) -> (
143 ::core::primitive::usize,
144 ::core::option::Option<::core::primitive::usize>,
145 );
146 fn count(self) -> ::core::primitive::usize
147 where
148 Self: ::core::marker::Sized;
149 fn last(self) -> ::core::option::Option<Self::Item>
150 where
151 Self: ::core::marker::Sized;
152 fn nth(&mut self, n: ::core::primitive::usize) -> ::core::option::Option<Self::Item>;
153 }
154 }
155
156 /// ```
157 /// # use newer_type::implement;
158 /// #[implement(newer_type::traits::Iterator, newer_type::traits::FusedIterator)]
159 /// struct MyStruct {
160 /// slot: std::vec::IntoIter<u8>,
161 /// }
162 /// ```
163 {
164 #[target(alternative = ::core::iter::FusedIterator)]
165 pub trait FusedIterator: ::core::iter::Iterator {}
166 }
167
168 /// ```
169 /// # use newer_type::implement;
170 /// #[implement(newer_type::traits::Iterator, newer_type::traits::ExactSizeIterator)]
171 /// struct MyStruct {
172 /// slot: std::vec::IntoIter<u8>,
173 /// }
174 /// ```
175 {
176 #[target(alternative = ::core::iter::ExactSizeIterator)]
177 pub trait ExactSizeIterator: ::core::iter::Iterator {
178 fn len(&self) -> ::core::primitive::usize;
179 }
180 }
181
182 /// ```
183 /// # use newer_type::implement;
184 /// #[implement(newer_type::traits::Iterator, newer_type::traits::DoubleEndedIterator)]
185 /// struct MyStruct {
186 /// slot: std::vec::IntoIter<u8>,
187 /// }
188 /// ```
189 {
190 #[target(alternative = ::core::iter::DoubleEndedIterator)]
191 pub trait DoubleEndedIterator: ::core::iter::Iterator {
192 fn next_back(&mut self) -> ::core::option::Option<Self::Item>;
193
194 fn nth_back(
195 &mut self,
196 n: ::core::primitive::usize,
197 ) -> ::core::option::Option<Self::Item>;
198 // fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R
199 // where
200 // Self: ::core::marker::Sized,
201 // F: ::core::ops::FnMut(B, Self::Item) -> R,
202 // R: ::core::ops::Try<Output = B>;
203 fn rfold<B, F>(self, init: B, f: F) -> B
204 where
205 Self: ::core::marker::Sized,
206 F: ::core::ops::FnMut(B, Self::Item) -> B;
207 fn rfind<P>(&mut self, predicate: P) -> ::core::option::Option<Self::Item>
208 where
209 Self: ::core::marker::Sized,
210 P: ::core::ops::FnMut(&Self::Item) -> ::core::primitive::bool;
211 }
212 }
213
214 /// ```
215 /// # use newer_type::implement;
216 /// #[implement(for<Borrowed> newer_type::traits::Borrow<Borrowed>)]
217 /// struct MyStruct {
218 /// slot: u8
219 /// }
220 /// ```
221 [Borrowed]{
222 #[target(alternative = ::core::borrow::Borrow)]
223 pub trait Borrow<Borrowed>
224 where
225 Borrowed: ?::core::marker::Sized,
226 {
227 // Required method
228 fn borrow(&self) -> &Borrowed;
229 }
230 }
231
232 /// ```
233 /// # use newer_type::implement;
234 /// #[implement(for<Borrowed> newer_type::traits::Borrow<Borrowed>)]
235 /// #[implement(for<Borrowed> newer_type::traits::BorrowMut<Borrowed>)]
236 /// struct MyStruct {
237 /// slot: u8,
238 /// }
239 /// ```
240 [Borrowed]{
241 #[target(alternative = ::core::borrow::BorrowMut)]
242 pub trait BorrowMut<Borrowed>: ::core::borrow::Borrow<Borrowed>
243 where
244 Borrowed: ?::core::marker::Sized,
245 {
246 fn borrow_mut(&mut self) -> &mut Borrowed;
247 }
248 }
249
250 /// ```ignore
251 /// # use newer_type::implement;
252 /// #[implement(newer_type::traits::ToOwned)]
253 /// struct MyStruct {
254 /// slot: String,
255 /// }
256 /// ```
257 {
258 #[target(alternative = ::std::borrow::ToOwned)]
259 pub trait ToOwned {
260 type Owned: ::core::borrow::Borrow<Self>;
261 fn to_owned(&self) -> Self::Owned;
262 fn clone_into(&self, target: &mut Self::Owned);
263 }
264 }
265
266 /// ```
267 /// # use newer_type::implement;
268 /// #[implement(for<Rhs> newer_type::traits::PartialEq<Rhs>)]
269 /// struct MyStruct {
270 /// slot: u8,
271 /// }
272 /// ```
273 [Rhs]{
274 #[target(alternative = ::core::cmp::PartialEq)]
275 pub trait PartialEq<Rhs = Self>
276 where
277 Rhs: ?::core::marker::Sized,
278 {
279 fn eq(&self, other: &Rhs) -> ::core::primitive::bool;
280 fn ne(&self, other: &Rhs) -> ::core::primitive::bool;
281 }
282 }
283
284 /// ```ignore
285 /// # use newer_type::implement;
286 /// #[implement(newer_type::traits::PartialEq<Self>)]
287 /// #[implement(newer_type::traits::Eq)]
288 /// struct MyStruct {
289 /// slot: u8
290 /// }
291 /// ```
292 {
293 #[target(alternative = ::core::cmp::Eq)]
294 pub trait Eq: ::core::cmp::PartialEq {}
295 }
296
297 /// ```ignore
298 /// # use newer_type::implement;
299 /// #[implement(newer_type::traits::PartialOrd<u8>)]
300 /// struct MyStruct {
301 /// slot: u8
302 /// }
303 /// ```
304 [Rhs]{
305 #[target(alternative = ::core::cmp::PartialOrd)]
306 pub trait PartialOrd<Rhs = Self>: ::core::cmp::PartialEq<Rhs>
307 where
308 Rhs: ?::core::marker::Sized,
309 {
310 fn partial_cmp(&self, other: &Rhs)
311 -> ::core::option::Option<::core::cmp::Ordering>;
312
313 fn lt(&self, other: &Rhs) -> ::core::primitive::bool;
314 fn le(&self, other: &Rhs) -> ::core::primitive::bool;
315 fn gt(&self, other: &Rhs) -> ::core::primitive::bool;
316 fn ge(&self, other: &Rhs) -> ::core::primitive::bool;
317 }
318 }
319
320 /// ```ignore
321 /// # use newer_type::implement;
322 /// #[implement(newer_type::traits::PartialEq<MyStruct>)]
323 /// #[implement(newer_type::traits::Eq)]
324 /// #[implement(newer_type::traits::PartialOrd<MyStruct>)]
325 /// #[implement(newer_type::traits::Ord)]
326 /// struct MyStruct {
327 /// slot: u8
328 /// }
329 /// ```
330 {
331 #[target(alternative = ::core::cmp::Ord)]
332 pub trait Ord: ::core::cmp::Eq + ::core::cmp::PartialOrd {
333 fn cmp(&self, other: &Self) -> ::core::cmp::Ordering;
334 // fn max(self, other: Self) -> Self
335 // where
336 // Self: ::core::marker::Sized;
337 // fn min(self, other: Self) -> Self
338 // where
339 // Self: ::core::marker::Sized;
340 // fn clamp(self, min: Self, max: Self) -> Self
341 // where
342 // Self: ::core::marker::Sized;
343 }
344 }
345 /// ```
346 /// # use newer_type::implement;
347 /// #[implement(newer_type::traits::Hash)]
348 /// struct MyStruct {
349 /// slot: u8
350 /// }
351 /// ```
352 {
353 #[target(alternative = ::core::hash::Hash)]
354 pub trait Hash {
355 fn hash<H>(&self, state: &mut H)
356 where H: ::core::hash::Hasher;
357 }
358 }
359 /// ```
360 /// # use newer_type::implement;
361 /// #[implement(newer_type::traits::Display)]
362 /// struct MyStruct {
363 /// slot: u8
364 /// }
365 /// ```
366 {
367 #[target(alternative = ::core::fmt::Display)]
368 pub trait Display {
369 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result;
370 }
371 }
372 /// ```
373 /// # use newer_type::implement;
374 /// #[implement(newer_type::traits::Debug)]
375 /// struct MyStruct {
376 /// slot: u8
377 /// }
378 /// ```
379 {
380 #[target(alternative = ::core::fmt::Debug)]
381 pub trait Debug {
382 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result;
383 }
384 }
385 }
386}