-
Notifications
You must be signed in to change notification settings - Fork 61
/
generics.rs
184 lines (150 loc) · 4.21 KB
/
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
use snafu::{prelude::*, Backtrace};
type BoxError = Box<dyn std::error::Error>;
#[derive(Debug, Snafu)]
enum Error<'a, 'x, A, Y> {
Everything {
source: BoxError,
name: &'a str,
length: A,
backtrace: Backtrace,
},
Lifetime {
key: &'x i32,
},
Type {
value: Y,
},
}
fn cause_error() -> Result<(), BoxError> {
Ok(())
}
fn example<'s, 'k, V>(name: &'s str, key: &'k i32, value: V) -> Result<(), Error<'s, 'k, usize, V>>
where
V: std::fmt::Debug,
{
let length = name.len();
cause_error().context(EverythingSnafu { name, length })?;
if name == "alice" {
return LifetimeSnafu { key }.fail();
}
if name == "bob" {
return TypeSnafu { value }.fail();
}
Ok(())
}
#[test]
fn implements_error() {
let name = String::from("hello");
let key = Box::new(42);
let value = vec![false];
example(&name, &key, value).unwrap();
}
mod bounds {
mod inline {
use snafu::prelude::*;
use std::fmt::{Debug, Display};
#[derive(Debug, Snafu)]
pub struct ApiError<T: Debug + Display>(Error<T>);
#[derive(Debug, Snafu)]
enum Error<T: Display> {
#[snafu(display("Boom: {value}"))]
_Boom { value: T },
#[snafu(whatever, display("{message}"))]
Whatever {
message: String,
#[snafu(source(from(Box<dyn std::error::Error>, Some)))]
source: Option<Box<dyn std::error::Error>>,
},
}
#[test]
fn implements_error() {
fn check_bounds<T: std::error::Error>() {}
check_bounds::<Error<i32>>();
check_bounds::<ApiError<i32>>();
}
}
mod where_clause {
use snafu::prelude::*;
use std::fmt::{Debug, Display};
#[derive(Debug, Snafu)]
pub struct ApiError<T>(Error<T>)
where
T: Debug + Display;
#[derive(Debug, Snafu)]
enum Error<T>
where
T: Display,
{
#[snafu(display("Boom: {value}"))]
_Boom { value: T },
#[snafu(whatever, display("{message}"))]
Whatever {
message: String,
#[snafu(source(from(Box<dyn std::error::Error>, Some)))]
source: Option<Box<dyn std::error::Error>>,
},
}
#[test]
fn implements_error() {
fn check_bounds<T: std::error::Error>() {}
check_bounds::<Error<i32>>();
check_bounds::<ApiError<i32>>();
}
}
}
mod const_generics {
use snafu::prelude::*;
#[derive(Debug, Snafu)]
#[snafu(display("Exceeded {N}"))]
pub struct Error<const N: i32>;
#[test]
fn implements_error() {
fn check_bounds<T: std::error::Error>() {}
check_bounds::<Error<1>>();
check_bounds::<Error<2>>();
}
#[test]
fn can_be_constructed() {
fn make_one() -> Result<(), Error<1>> {
Snafu.fail()
}
fn make_two() -> Result<(), Error<2>> {
Snafu.fail()
}
assert!(make_one().is_err());
assert!(make_two().is_err());
}
#[test]
fn can_use_const_in_display() {
let e: Error<42> = Snafu.build();
assert_eq!(e.to_string(), "Exceeded 42");
}
mod with_default {
use snafu::prelude::*;
#[derive(Debug, Snafu)]
#[snafu(display("Exceeded {N}"))]
pub struct Error<const N: i32 = 42>;
#[test]
fn implements_error() {
fn check_bounds<T: std::error::Error>() {}
check_bounds::<Error>();
check_bounds::<Error<99>>();
}
#[test]
fn can_be_constructed() {
fn make_forty_two() -> Result<(), Error> {
Snafu.fail()
}
fn make_ninety_nine() -> Result<(), Error<99>> {
Snafu.fail()
}
assert!(make_forty_two().is_err());
assert!(make_ninety_nine().is_err());
}
#[test]
fn can_use_const_in_display() {
let e: Error = Snafu.build();
assert_eq!(e.to_string(), "Exceeded 42");
}
}
}