Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

Commit 7866746

Browse files
author
Erik Zscheile
committed
Merge remote-tracking branch 'upstream/master' into issue251
2 parents c726ebe + c50ee10 commit 7866746

10 files changed

+160
-40
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ rust:
55
- nightly
66
# Oldest supported version for all features.
77
# Use of https://github.com/rust-lang/rfcs/pull/16
8-
- 1.14.0
8+
# and Backtrace needs 1.25 (see https://github.com/alexcrichton/backtrace-rs/pull/137)
9+
- 1.25.0
910
# Oldest supported version as dependency, with no features, tests, or examples.
1011
- 1.10.0
1112

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Unreleased
22

3+
# 0.12.1
4+
5+
- [`std::error::Error::cause` deprecation update](https://github.com/rust-lang-nursery/error-chain/pull/255)
6+
- [Macro invocations use 2018 style](https://github.com/rust-lang-nursery/error-chain/pull/253)
7+
38
# 0.12.0
49

510
- [Remove `impl Deref<Kind> for Error`](https://github.com/rust-lang-nursery/error-chain/pull/192)

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "error-chain"
3-
version = "0.12.0" # remember to update html_root_url
3+
version = "0.12.1" # remember to update html_root_url
44
authors = [ "Brian Anderson <[email protected]>",
55
"Paul Colomiets <[email protected]>",
66
"Colin Kiegel <[email protected]>",
@@ -15,12 +15,12 @@ readme = "README.md"
1515

1616
license = "MIT/Apache-2.0"
1717

18-
[badges]
19-
travis-ci = { repository = "rust-lang-nursery/error-chain" }
20-
2118
[features]
2219
default = ["backtrace", "example_generated"]
2320
example_generated = []
2421

2522
[dependencies]
2623
backtrace = { version = "0.3.3", optional = true }
24+
25+
[build-dependencies]
26+
version_check = "0.1.5"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# error-chain - Consistent error handling for Rust
22

3-
[![Build Status](https://api.travis-ci.org/rust-lang-nursery/error-chain.svg?branch=master)](https://travis-ci.org/rust-lang-nursery/error-chain)
3+
[![Build Status](https://travis-ci.com/rust-lang-nursery/error-chain.svg?branch=master)](https://travis-ci.com/rust-lang-nursery/error-chain)
44
[![Latest Version](https://img.shields.io/crates/v/error-chain.svg)](https://crates.io/crates/error-chain)
5-
[![License](https://img.shields.io/github/license/rust-lang-nursery/error-chain.svg)](https://github.com/rust-lang-nursery/error-chain)
5+
[![License](https://img.shields.io/badge/license-MIT%2FApache--2.0-green.svg)](https://github.com/rust-lang-nursery/error-chain)
66

77
`error-chain` makes it easy to take full advantage of Rust's error
88
handling features without the overhead of maintaining boilerplate

build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
extern crate version_check;
2+
3+
use version_check::is_min_version;
4+
5+
fn main() {
6+
// Switch on for versions that have Error::source
7+
// As introduced by https://github.com/rust-lang/rust/pull/53533
8+
if is_min_version("1.30").map(|(is_high_enough, _actual_version)| is_high_enough).unwrap_or(false)
9+
{
10+
println!("cargo:rustc-cfg=has_error_source");
11+
}
12+
}

src/backtrace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod imp {
77
use std::cell::UnsafeCell;
88
use std::env;
99
use std::fmt;
10-
use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
10+
use std::sync::atomic::{AtomicUsize, Ordering};
1111
use std::sync::{Arc, Mutex};
1212

1313
/// Internal representation of a backtrace
@@ -33,7 +33,7 @@ mod imp {
3333
/// in the generated error implementations.
3434
#[doc(hidden)]
3535
pub fn new() -> InternalBacktrace {
36-
static ENABLED: AtomicUsize = ATOMIC_USIZE_INIT;
36+
static ENABLED: AtomicUsize = AtomicUsize::new(0);
3737

3838
match ENABLED.load(Ordering::SeqCst) {
3939
0 => {

src/error_chain.rs

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,76 @@
1-
/// Prefer to use `error_chain` instead of this macro.
21
#[doc(hidden)]
32
#[macro_export]
3+
#[cfg(not(has_error_source))]
4+
macro_rules! impl_error_chain_cause_or_source {
5+
(
6+
types {
7+
$error_kind_name:ident
8+
}
9+
10+
foreign_links {
11+
$( $foreign_link_variant:ident ( $foreign_link_error_path:path )
12+
$( #[$meta_foreign_links:meta] )*; )*
13+
}
14+
) => {
15+
#[allow(unknown_lints, renamed_and_removed_lints)]
16+
#[allow(unused_doc_comment, unused_doc_comments)]
17+
fn cause(&self) -> Option<&::std::error::Error> {
18+
match self.1.next_error {
19+
Some(ref c) => Some(&**c),
20+
None => {
21+
match self.0 {
22+
$(
23+
$(#[$meta_foreign_links])*
24+
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
25+
foreign_err.cause()
26+
}
27+
) *
28+
_ => None
29+
}
30+
}
31+
}
32+
}
33+
};
34+
}
35+
36+
#[cfg(has_error_source)]
37+
#[doc(hidden)]
38+
#[macro_export]
39+
macro_rules! impl_error_chain_cause_or_source {
40+
(
41+
types {
42+
$error_kind_name:ident
43+
}
44+
45+
foreign_links {
46+
$( $foreign_link_variant:ident ( $foreign_link_error_path:path )
47+
$( #[$meta_foreign_links:meta] )*; )*
48+
}
49+
) => {
50+
#[allow(unknown_lints, renamed_and_removed_lints)]
51+
#[allow(unused_doc_comment, unused_doc_comments)]
52+
fn source(&self) -> Option<&(std::error::Error + 'static)> {
53+
match self.1.next_error {
54+
Some(ref c) => Some(&**c),
55+
None => {
56+
match self.0 {
57+
$(
58+
$(#[$meta_foreign_links])*
59+
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
60+
foreign_err.source()
61+
}
62+
) *
63+
_ => None
64+
}
65+
}
66+
}
67+
}
68+
};
69+
}
70+
71+
/// Prefer to use `error_chain` instead of this macro.
72+
#[doc(hidden)]
73+
#[macro_export(local_inner_macros)]
474
macro_rules! impl_error_chain_processed {
575
// Default values for `types`.
676
(
@@ -179,22 +249,13 @@ macro_rules! impl_error_chain_processed {
179249
self.description()
180250
}
181251

182-
#[allow(unknown_lints, renamed_and_removed_lints)]
183-
#[allow(unused_doc_comment, unused_doc_comments)]
184-
fn cause(&self) -> Option<&::std::error::Error> {
185-
match self.1.next_error {
186-
Some(ref c) => Some(&**c),
187-
None => {
188-
match self.0 {
189-
$(
190-
$(#[$meta_foreign_links])*
191-
$error_kind_name::$foreign_link_variant(ref foreign_err) => {
192-
foreign_err.cause()
193-
}
194-
) *
195-
_ => None
196-
}
197-
}
252+
impl_error_chain_cause_or_source!{
253+
types {
254+
$error_kind_name
255+
}
256+
foreign_links {
257+
$( $foreign_link_variant ( $foreign_link_error_path )
258+
$( #[$meta_foreign_links] )*; )*
198259
}
199260
}
200261
}
@@ -348,7 +409,7 @@ macro_rules! impl_error_chain_processed {
348409

349410
/// Internal macro used for reordering of the fields.
350411
#[doc(hidden)]
351-
#[macro_export]
412+
#[macro_export(local_inner_macros)]
352413
macro_rules! error_chain_processing {
353414
(
354415
({}, $b:tt, $c:tt, $d:tt)
@@ -401,7 +462,7 @@ macro_rules! error_chain_processing {
401462
}
402463

403464
/// Macro for generating error types and traits. See crate level documentation for details.
404-
#[macro_export]
465+
#[macro_export(local_inner_macros)]
405466
macro_rules! error_chain {
406467
( $( $block_name:ident { $( $block_content:tt )* } )* ) => {
407468
error_chain_processing! {

src/impl_error_chain_kind.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1-
// From https://github.com/tailhook/quick-error
2-
// Changes:
3-
// - replace `impl Error` by `impl Item::description`
4-
// - $imeta
1+
/// From https://github.com/tailhook/quick-error
2+
/// Changes:
3+
/// - replace `impl Error` by `impl Item::description`
4+
/// - $imeta
55
6+
/// Because of the `#[macro_export(local_inner_macros)]` usage on `impl_error_chain_kind` that macro
7+
/// will only look inside this crate for macros to invoke. So using `stringify` or `write` from
8+
/// the standard library will fail. Thus we here create simple wrappers for them that are not
9+
/// exported as `local_inner_macros`, and thus they can in turn use the standard library macros.
610
#[macro_export]
11+
macro_rules! stringify_internal {
12+
($($t:tt)*) => { stringify!($($t)*) }
13+
}
14+
15+
/// Macro used interally for output expanding an expression
16+
#[macro_export]
17+
macro_rules! write_internal {
18+
($dst:expr, $($arg:tt)*) => (write!($dst, $($arg)*))
19+
}
20+
21+
#[macro_export(local_inner_macros)]
722
#[doc(hidden)]
823
macro_rules! impl_error_chain_kind {
924
( $(#[$meta:meta])*
@@ -274,18 +289,18 @@ macro_rules! impl_error_chain_kind {
274289
{ display($self_:tt) -> ($( $exprs:tt )*) $( $tail:tt )*}
275290
) => {
276291
|impl_error_chain_kind!(IDENT $self_): &$name, f: &mut ::std::fmt::Formatter| {
277-
write!(f, $( $exprs )*)
292+
write_internal!(f, $( $exprs )*)
278293
}
279294
};
280295
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
281296
{ display($pattern:expr) $( $tail:tt )*}
282297
) => {
283-
|_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern) }
298+
|_, f: &mut ::std::fmt::Formatter| { write_internal!(f, $pattern) }
284299
};
285300
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
286301
{ display($pattern:expr, $( $exprs:tt )*) $( $tail:tt )*}
287302
) => {
288-
|_, f: &mut ::std::fmt::Formatter| { write!(f, $pattern, $( $exprs )*) }
303+
|_, f: &mut ::std::fmt::Formatter| { write_internal!(f, $pattern, $( $exprs )*) }
289304
};
290305
(FIND_DISPLAY_IMPL $name:ident $item:ident: $imode:tt
291306
{ $t:tt $( $tail:tt )*}
@@ -298,7 +313,7 @@ macro_rules! impl_error_chain_kind {
298313
{ }
299314
) => {
300315
|self_: &$name, f: &mut ::std::fmt::Formatter| {
301-
write!(f, "{}", self_.description())
316+
write_internal!(f, "{}", self_.description())
302317
}
303318
};
304319
(FIND_DESCRIPTION_IMPL $item:ident: $imode:tt $me:ident $fmt:ident
@@ -319,7 +334,7 @@ macro_rules! impl_error_chain_kind {
319334
[$( $var:ident ),*]
320335
{ }
321336
) => {
322-
stringify!($item)
337+
stringify_internal!($item)
323338
};
324339
(ITEM_BODY $(#[$imeta:meta])* $item:ident: UNIT
325340
) => { };

src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![deny(missing_docs)]
2-
#![doc(html_root_url = "https://docs.rs/error-chain/0.12.0")]
2+
#![doc(html_root_url = "https://docs.rs/error-chain/0.12.1")]
33

44
//! A library for consistent and reliable error handling
55
//!
@@ -570,7 +570,12 @@ impl<'a> Iterator for Iter<'a> {
570570
fn next<'b>(&'b mut self) -> Option<&'a error::Error> {
571571
match self.0.take() {
572572
Some(e) => {
573-
self.0 = e.cause();
573+
self.0 = match () {
574+
#[cfg(not(has_error_source))]
575+
() => e.cause(),
576+
#[cfg(has_error_source)]
577+
() => e.source(),
578+
};
574579
Some(e)
575580
}
576581
None => None,
@@ -783,7 +788,7 @@ macro_rules! bail {
783788
/// ```
784789
///
785790
/// See documentation for `bail!` macro for further details.
786-
#[macro_export]
791+
#[macro_export(local_inner_macros)]
787792
macro_rules! ensure {
788793
($cond:expr, $e:expr) => {
789794
if !($cond) {

tests/tests.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,15 @@ mod foreign_link_test {
292292
"Foreign error description"
293293
}
294294

295+
#[cfg(not(has_error_source))]
295296
fn cause(&self) -> Option<&::std::error::Error> {
296297
Some(&self.cause)
297298
}
299+
300+
#[cfg(has_error_source)]
301+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
302+
Some(&self.cause)
303+
}
298304
}
299305

300306
impl fmt::Display for ForeignError {
@@ -311,9 +317,15 @@ mod foreign_link_test {
311317
"Foreign error cause description"
312318
}
313319

320+
#[cfg(not(has_error_source))]
314321
fn cause(&self) -> Option<&::std::error::Error> {
315322
None
316323
}
324+
325+
#[cfg(has_error_source)]
326+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
327+
None
328+
}
317329
}
318330

319331
impl fmt::Display for ForeignErrorCause {
@@ -342,12 +354,21 @@ mod foreign_link_test {
342354
}
343355

344356
#[test]
357+
#[cfg(not(has_error_source))]
345358
fn finds_cause() {
346359
let chained_error = try_foreign_error().err().unwrap();
347360
assert_eq!(format!("{}", ForeignErrorCause {}),
348361
format!("{}", ::std::error::Error::cause(&chained_error).unwrap()));
349362
}
350363

364+
#[test]
365+
#[cfg(has_error_source)]
366+
fn finds_source() {
367+
let chained_error = try_foreign_error().err().unwrap();
368+
assert_eq!(format!("{}", ForeignErrorCause {}),
369+
format!("{}", ::std::error::Error::source(&chained_error).unwrap()));
370+
}
371+
351372
#[test]
352373
fn iterates() {
353374
let chained_error = try_foreign_error().err().unwrap();

0 commit comments

Comments
 (0)