forked from dtolnay/thiserror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_generics.rs
112 lines (99 loc) · 2.8 KB
/
test_generics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#![deny(clippy::all, clippy::pedantic)]
#![allow(
// Clippy bug: https://github.com/rust-lang/rust-clippy/issues/7422
clippy::nonstandard_macro_braces,
)]
#![allow(dead_code)]
use std::fmt::{Debug, Display};
use thiserror::Error;
struct NoFormattingType;
struct DisplayType;
impl Display for DisplayType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, stringify!(DisplayType))
}
}
impl Debug for DisplayType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, stringify!(DisplayType))
}
}
struct DebugType;
impl Debug for DebugType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, stringify!(DebugType))
}
}
/// Direct embedding of a generic in a field
///
/// Should produce the following instances:
///
/// ```rust
/// impl<Embedded> Display for DirectEmbedding<Embedded>
/// where
/// Embedded: Debug;
///
/// impl<Embedded> Error for DirectEmbedding<Embedded>
/// where
/// Self: Debug + Display;
/// ```
#[derive(Error, Debug)]
enum DirectEmbedding<Embedded> {
#[error("{0:?}")]
FatalError(Embedded),
}
/// #[from] handling but no Debug usage of the generic
///
/// Should produce the following instances:
///
/// ```rust
/// impl<Indirect> Display for FromGenericError<Indirect>;
///
/// impl<Indirect> Error for FromGenericError<Indirect>
/// where
/// SourceGenericError<Indirect>: Error,
/// Indirect: 'static,
/// Self: Debug + Display;
/// ```
// TODO: Parse generic parameters contained in #[from] usages and add them to Error impl
// #[derive(Error, Debug)]
// enum FromGenericError<Indirect> {
// #[error("Tadah")]
// SourceEmbedded(#[from] DirectEmbedding<Indirect>),
// }
/// Direct embedding of a generic in a field
///
/// Should produce the following instances:
///
/// ```rust
/// impl<HasDisplay, HasDebug> Display for DirectEmbedding<HasDisplay, HasDebug>
/// where
/// HasDisplay: Display,
/// HasDebug: Debug;
///
/// impl<HasDisplay, HasDebug> Error for DirectEmbedding<HasDisplay, HasDebug>
/// where
/// Self: Debug + Display;
/// ```
#[derive(Error)]
enum HybridDisplayType<HasDisplay, HasDebug, HasNeither> {
#[error("{0} : {1:?}")]
HybridDisplayCase(HasDisplay, HasDebug),
#[error("{0}")]
DisplayCase(HasDisplay, HasNeither),
#[error("{1:?}")]
DebugCase(HasNeither, HasDebug),
}
impl<HasDisplay, HasDebug, HasNeither> Debug
for HybridDisplayType<HasDisplay, HasDebug, HasNeither>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "HybridDisplayType")
}
}
fn display_hybrid_display_type(
instance: HybridDisplayType<DisplayType, DebugType, NoFormattingType>,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result {
Debug::fmt(&instance, f)
}