impl_more/lib.rs
1//! Concise, declarative trait implementation macros.
2//!
3//! # `#[no_std]`
4//!
5//! Where possible, these macros emit `#[no_std]`-compatible code.
6//!
7//! # Usage
8//!
9//! ```
10//! # use std::error::Error as _;
11//! struct MyNewTypeStruct(String);
12//!
13//! impl_more::impl_as_ref!(MyNewTypeStruct => String);
14//! impl_more::impl_as_mut!(MyNewTypeStruct => String);
15//! impl_more::forward_as_ref!(MyNewTypeStruct => str);
16//! impl_more::forward_as_mut!(MyNewTypeStruct => str);
17//!
18//! impl_more::impl_deref!(MyNewTypeStruct => String);
19//! impl_more::impl_deref_mut!(MyNewTypeStruct);
20//! // or, to deref through String too:
21//! // impl_more::forward_deref_and_mut!(MyNewTypeStruct => ref str);
22//!
23//! impl_more::impl_from!(String => MyNewTypeStruct);
24//! impl_more::impl_into!(MyNewTypeStruct => String);
25//!
26//! impl_more::forward_display!(MyNewTypeStruct);
27//! impl_more::forward_from_str!(MyNewTypeStruct => String);
28//!
29//! struct Items<T>(Vec<T>);
30//!
31//! impl_more::forward_into_iterator!(<T> in Items<T> => Vec<T>; owned, ref, ref_mut);
32//! impl_more::forward_from_iterator!(<T> in Items<T> => Vec<T>);
33//! impl_more::forward_extend!(<T> in Items<T> => Vec<T>);
34//!
35//! struct Mask(u32);
36//!
37//! impl_more::forward_debug!(Mask);
38//! impl_more::forward_lower_hex!(Mask);
39//! impl_more::forward_binary!(Mask);
40//!
41//! enum MyEnum {
42//! Bar,
43//! Qux,
44//! }
45//!
46//! impl_more::impl_display_enum!(MyEnum: Bar => "bar", Qux => "qux");
47//!
48//! enum Coords {
49//! Xy(i64, i64),
50//! Xyz(i64, i64, i64),
51//! Uv(i64, i64),
52//! }
53//!
54//! impl_more::impl_display_enum!(
55//! Coords:
56//! Xy(x, y) => "{x}, {y}",
57//! Xyz(x, y, z) => "{x}, {y}, {z}",
58//! Uv(u, _) => "{u}",
59//! );
60//!
61//! #[derive(Debug)]
62//! struct MyError(eyre::Report);
63//!
64//! impl_more::forward_display!(MyError);
65//! impl_more::forward_error!(MyError);
66//!
67//! let err = MyError(eyre::eyre!("something went wrong"));
68//! assert_eq!(err.source().unwrap().to_string(), "something went wrong");
69//!
70//! #[derive(Debug)]
71//! struct MyFieldError {
72//! cause: eyre::Report,
73//! }
74//!
75//! impl_more::forward_display!(MyFieldError => cause);
76//! impl_more::forward_error!(MyFieldError => cause);
77//!
78//! let err = MyFieldError {
79//! cause: eyre::eyre!("something else went wrong"),
80//! };
81//! assert_eq!(
82//! err.source().unwrap().to_string(),
83//! "something else went wrong"
84//! );
85//!
86//! #[derive(Debug)]
87//! enum Err {
88//! Io(std::io::Error),
89//! Generic(String),
90//! }
91//!
92//! impl_more::impl_display_enum! {
93//! Err:
94//! Io(err) => "{err}",
95//! Generic(msg) => "{msg}",
96//! }
97//! impl_more::impl_error_enum!(Err: Io(err) => err);
98//! ```
99
100#![cfg_attr(not(docsrs), no_std)]
101#![cfg_attr(docsrs, feature(doc_cfg))]
102
103#[cfg(test)]
104extern crate alloc;
105#[cfg(test)]
106extern crate std;
107
108#[macro_use]
109mod as_ref;
110#[macro_use]
111mod deref;
112#[macro_use]
113mod display;
114#[macro_use]
115mod error;
116#[macro_use]
117mod fmt;
118#[macro_use]
119mod from;
120#[macro_use]
121mod from_str;
122#[macro_use]
123mod iter;
124
125#[cfg(test)]
126mod tests {
127 #![allow(dead_code, clippy::from_over_into)]
128
129 use alloc::string::{String, ToString as _};
130 use core::ops::{Deref, DerefMut};
131
132 #[derive(Debug)]
133 struct Foo(String);
134
135 crate::impl_as_ref!(Foo => String);
136 crate::impl_as_mut!(Foo => String);
137
138 crate::impl_deref!(Foo => String);
139 crate::impl_deref_mut!(Foo);
140
141 static_assertions::assert_impl_all!(
142 Foo:
143 // impls
144 AsRef<String>,
145 AsMut<String>,
146 Deref<Target = String>,
147 DerefMut,
148 );
149
150 #[derive(Debug)]
151 struct Bar {
152 foo: Foo,
153 }
154
155 crate::impl_as_ref!(Bar => foo: Foo);
156 crate::impl_as_mut!(Bar => foo: Foo);
157
158 crate::impl_deref!(Bar => foo: String);
159 crate::impl_deref_mut!(Bar => foo);
160
161 static_assertions::assert_impl_all!(
162 Bar:
163 // impls
164 AsRef<Foo>,
165 AsMut<Foo>,
166 Deref<Target = String>,
167 DerefMut,
168 );
169
170 #[allow(dead_code)]
171 #[test]
172 fn impl_display() {
173 enum Foo {
174 Bar,
175 Qux,
176 }
177 crate::impl_display_enum!(Foo: Bar => "bar", Qux => "qux");
178 assert_eq!(Foo::Bar.to_string(), "bar");
179 assert_eq!(Foo::Qux.to_string(), "qux");
180
181 enum FooComma {
182 Bar,
183 Qux,
184 }
185 crate::impl_display_enum!(FooComma: Bar => "bar", Qux => "qux",);
186
187 enum FooContents {
188 Bar(u64, u64),
189 }
190 crate::impl_display_enum!(FooContents: Bar (x, y) => "x: {x}; y: {y}");
191 assert_eq!(FooContents::Bar(4, 2).to_string(), "x: 4; y: 2");
192
193 enum FooContents2 {
194 Qux { msg: &'static str },
195 }
196 crate::impl_display_enum!(FooContents2: Qux { msg } => "msg: {msg}");
197 assert_eq!(FooContents2::Qux { msg: "foo" }.to_string(), "msg: foo");
198 }
199}