Skip to main content

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 enum_from;
122#[macro_use]
123mod from_str;
124#[macro_use]
125mod iter;
126
127#[cfg(test)]
128mod tests {
129    #![allow(dead_code, clippy::from_over_into)]
130
131    use alloc::string::{String, ToString as _};
132    use core::ops::{Deref, DerefMut};
133
134    #[derive(Debug)]
135    struct Foo(String);
136
137    crate::impl_as_ref!(Foo => String);
138    crate::impl_as_mut!(Foo => String);
139
140    crate::impl_deref!(Foo => String);
141    crate::impl_deref_mut!(Foo);
142
143    static_assertions::assert_impl_all!(
144        Foo:
145        // impls
146        AsRef<String>,
147        AsMut<String>,
148        Deref<Target = String>,
149        DerefMut,
150    );
151
152    #[derive(Debug)]
153    struct Bar {
154        foo: Foo,
155    }
156
157    crate::impl_as_ref!(Bar => foo: Foo);
158    crate::impl_as_mut!(Bar => foo: Foo);
159
160    crate::impl_deref!(Bar => foo: String);
161    crate::impl_deref_mut!(Bar => foo);
162
163    static_assertions::assert_impl_all!(
164        Bar:
165        // impls
166        AsRef<Foo>,
167        AsMut<Foo>,
168        Deref<Target = String>,
169        DerefMut,
170    );
171
172    #[allow(dead_code)]
173    #[test]
174    fn impl_display() {
175        enum Foo {
176            Bar,
177            Qux,
178        }
179        crate::impl_display_enum!(Foo: Bar => "bar", Qux => "qux");
180        assert_eq!(Foo::Bar.to_string(), "bar");
181        assert_eq!(Foo::Qux.to_string(), "qux");
182
183        enum FooComma {
184            Bar,
185            Qux,
186        }
187        crate::impl_display_enum!(FooComma: Bar => "bar", Qux => "qux",);
188
189        enum FooContents {
190            Bar(u64, u64),
191        }
192        crate::impl_display_enum!(FooContents: Bar (x, y) => "x: {x}; y: {y}");
193        assert_eq!(FooContents::Bar(4, 2).to_string(), "x: 4; y: 2");
194
195        enum FooContents2 {
196            Qux { msg: &'static str },
197        }
198        crate::impl_display_enum!(FooContents2: Qux { msg } => "msg: {msg}");
199        assert_eq!(FooContents2::Qux { msg: "foo" }.to_string(), "msg: foo");
200    }
201}