newer_type/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]
2#![doc = include_str!("./README.md")]
3
4// internal
5pub use newer_type_macro::__implement_internal;
6
7/// Implement a trait for given enum or struct. The trait should be defined with
8/// [`target`] attribute.
9///
10/// # Example
11///
12/// ```ignore
13/// use newer_type::implement;
14/// use newer_type_std::{ops::Extend, cmp::PartialEq};
15///
16/// #[implement(Extend<usize>)]
17/// struct Example1(Vec<usize>);
18///
19/// #[implement(Extend<T>)]
20/// struct Example2<T>(Vec<T>);
21///
22/// #[implement(for<T> PartialEq<T>)]
23/// struct Example3(String);
24///
25/// #[implement(for<T: std::fmt::Debug> PartialEq<T>)]
26/// struct Example4<U>(U);
27/// ```
28pub use newer_type_macro::implement;
29
30/// Define a trait for use of [`implement`] macro.
31///
32/// # Arguments (all optional)
33///
34/// - `alternative` ... Trait. If specified, implement this trait instead of the
35/// target trait itself. The target trait is used only for an argument of
36/// [`implement`] macro.
37/// - `newer_type` ... Set path to `newer_type` crate. Defaults to
38/// `::newer_type`. Example: `::your_crate::_export::newer_type`.
39/// - `repeater` ... Absolute path to the `Repeater` crate. see the example
40/// section. The `Repeater` trait is defined in the same crate that the target
41/// trait is defined, and should be visible from the users, which refer to the
42/// trait with `#[implement]` macro.
43///
44/// # Example
45///
46/// ```
47/// use newer_type::target;
48///
49/// pub trait Repeater<const TRAIT_ID : u64, const NTH : usize, T: ?Sized> {
50/// type Type;
51/// }
52///
53/// #[target(repeater = Repeater)]
54/// trait MyTrait {
55/// fn my_fn(&self) -> ::core::primitive::usize;
56/// }
57/// ```
58///
59/// ```
60/// use newer_type::target;
61/// type TypeFromContext = usize;
62///
63/// pub trait Repeater<const TRAIT_ID : u64, const NTH : usize, T: ?Sized> {
64/// type Type;
65/// }
66///
67/// #[target(repeater = Repeater)]
68/// trait MyTrait {
69/// fn my_fn(&self, t: TypeFromContext) -> Box<usize>;
70/// }
71/// ```
72///
73/// We recomend this pattern to set `repeater` path correctly.
74///
75/// ```ignore
76/// use newer_type::target;
77/// type TypeFromContext = usize;
78///
79/// // placed in crate root
80/// pub trait Repeater<const TRAIT_ID : u64, const NTH : usize, T: ?Sized> {
81/// type Type;
82/// }
83///
84/// macro_rules! emit_trait {
85/// () => {
86/// #[target(repeater = $crate::Repeater)]
87/// trait MyTrait {
88/// fn my_fn(&self, t: TypeFromContext) -> Box<usize>;
89/// }
90/// };
91/// }
92/// emit_trait!();
93/// ```
94pub use newer_type_macro::target;