malachite_base/named/
mod.rs

1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9/// Defines the name of a type. This is useful for constructing error messages in a generic
10/// function.
11pub trait Named {
12    /// The name of `Self`.
13    const NAME: &'static str;
14}
15
16/// Automatically implements [`Named`] for a type.
17///
18/// It doesn't work very well for types whose names contain several tokens, like `(u8, u8)`, `&str`,
19/// or `Vec<bool>`.
20///
21/// # Examples
22/// ```
23/// use malachite_base::named::Named;
24///
25/// assert_eq!(u8::NAME, "u8");
26/// assert_eq!(String::NAME, "String");
27/// ```
28#[macro_export]
29macro_rules! impl_named {
30    ($t:ident) => {
31        impl Named for $t {
32            /// The name of this type, as given by the [`stringify`] macro.
33            ///
34            /// See the documentation for [`impl_named`] for more details.
35            const NAME: &'static str = stringify!($t);
36        }
37    };
38}