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

Allow depending on std without depending on backtrace #257

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ cache: cargo
script:
- cargo test
- cargo test --features backtrace
- cargo test --no-default-features --features "std2 derive"
- cargo check --no-default-features
- cargo check --no-default-features --features std2
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ members = [".", "failure_derive"]
[features]
default = ["std", "derive"]
#small-error = ["std"]
std = ["backtrace"]
std = ["backtrace", "std2"]
# temporary feature to enable 'std' without enabling 'backtrace' while still keeping backwards
# compatibility ("std" has always depended on "backtrace" and it would be a breaking change to
# change that)
std2 = []
derive = ["failure_derive"]
8 changes: 4 additions & 4 deletions src/backtrace/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::fmt::{self, Debug, Display};

macro_rules! with_backtrace { ($($i:item)*) => ($(#[cfg(all(feature = "backtrace", feature = "std"))]$i)*) }
macro_rules! without_backtrace { ($($i:item)*) => ($(#[cfg(not(all(feature = "backtrace", feature = "std")))]$i)*) }
macro_rules! with_backtrace { ($($i:item)*) => ($(#[cfg(all(feature = "backtrace", feature = "std2"))]$i)*) }
macro_rules! without_backtrace { ($($i:item)*) => ($(#[cfg(not(all(feature = "backtrace", feature = "std2")))]$i)*) }

without_backtrace! {
/// A `Backtrace`.
Expand Down Expand Up @@ -43,12 +43,12 @@ without_backtrace! {
Backtrace { _secret: () }
}

#[cfg(feature = "std")]
#[cfg(feature = "std2")]
pub(crate) fn none() -> Backtrace {
Backtrace { _secret: () }
}

#[cfg(feature = "std")]
#[cfg(feature = "std2")]
pub(crate) fn is_none(&self) -> bool {
true
}
Expand Down
6 changes: 3 additions & 3 deletions src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use backtrace::Backtrace;
use context::Context;
use compat::Compat;

#[cfg(feature = "std")]
#[cfg(feature = "std2")]
use box_std::BoxStd;

#[cfg_attr(feature = "small-error", path = "./error_impl_small.rs")]
mod error_impl;
use self::error_impl::ErrorImpl;

#[cfg(feature = "std")]
#[cfg(feature = "std2")]
use std::error::Error as StdError;


Expand Down Expand Up @@ -59,7 +59,7 @@ impl Error {
/// Ok(92)
/// }
/// ```
#[cfg(feature = "std")]
#[cfg(feature = "std2")]
pub fn from_boxed_compat(err: Box<StdError + Sync + Send + 'static>) -> Error {
Error::from(BoxStd(err))
}
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
//! variable to a non-zero value (this also enables backtraces for panics).
//! Use the `RUST_FAILURE_BACKTRACE` variable to enable or disable backtraces
//! for `failure` specifically.
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std2"), no_std)]
#![deny(missing_docs)]
#![deny(warnings)]
#![cfg_attr(feature = "small-error", feature(extern_types, allocator_api))]

macro_rules! with_std { ($($i:item)*) => ($(#[cfg(feature = "std")]$i)*) }
macro_rules! without_std { ($($i:item)*) => ($(#[cfg(not(feature = "std"))]$i)*) }
macro_rules! with_std { ($($i:item)*) => ($(#[cfg(feature = "std2")]$i)*) }
macro_rules! without_std { ($($i:item)*) => ($(#[cfg(not(feature = "std2"))]$i)*) }

// Re-export libcore using an alias so that the macros can work without
// requiring `extern crate core` downstream.
#[doc(hidden)]
pub extern crate core as _core;

mod backtrace;
#[cfg(feature = "std")]
#[cfg(feature = "std2")]
mod box_std;
mod compat;
mod context;
Expand Down Expand Up @@ -251,10 +251,10 @@ impl Fail {
}
}

#[cfg(feature = "std")]
#[cfg(feature = "std2")]
impl<E: StdError + Send + Sync + 'static> Fail for E {}

#[cfg(feature = "std")]
#[cfg(feature = "std2")]
impl Fail for Box<Fail> {
fn cause(&self) -> Option<&Fail> {
(**self).cause()
Expand Down
22 changes: 11 additions & 11 deletions src/result_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub trait ResultExt<T, E> {
/// # tests::run_test();
/// # }
/// #
/// # #[cfg(not(all(feature = "std", feature = "derive")))] mod tests { pub fn run_test() { } }
/// #
/// # #[cfg(all(feature = "std", feature = "derive"))] mod tests {
/// # #[cfg(not(all(feature = "std2", feature = "derive")))] mod tests { pub fn run_test() { } }
/// #
/// # #[cfg(all(feature = "std2", feature = "derive"))] mod tests {
/// use std::error::Error;
/// # use std::fmt;
/// #
Expand Down Expand Up @@ -65,19 +65,19 @@ pub trait ResultExt<T, E> {
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "std", feature = "derive"))]
/// # #[cfg(all(feature = "std2", feature = "derive"))]
/// # #[macro_use] extern crate failure;
/// #
/// # #[cfg(all(feature = "std", feature = "derive"))]
/// # #[cfg(all(feature = "std2", feature = "derive"))]
/// # #[macro_use] extern crate failure_derive;
/// #
/// # fn main() {
/// # tests::run_test();
/// # }
/// #
/// # #[cfg(not(all(feature = "std", feature = "derive")))] mod tests { pub fn run_test() { } }
/// # #[cfg(not(all(feature = "std2", feature = "derive")))] mod tests { pub fn run_test() { } }
/// #
/// # #[cfg(all(feature = "std", feature = "derive"))] mod tests {
/// # #[cfg(all(feature = "std2", feature = "derive"))] mod tests {
/// #
/// # use failure::{self, ResultExt};
/// #
Expand Down Expand Up @@ -108,19 +108,19 @@ pub trait ResultExt<T, E> {
/// # Examples
///
/// ```
/// # #[cfg(all(feature = "std", feature = "derive"))]
/// # #[cfg(all(feature = "std2", feature = "derive"))]
/// # #[macro_use] extern crate failure;
/// #
/// # #[cfg(all(feature = "std", feature = "derive"))]
/// # #[cfg(all(feature = "std2", feature = "derive"))]
/// # #[macro_use] extern crate failure_derive;
/// #
/// # fn main() {
/// # tests::run_test();
/// # }
/// #
/// # #[cfg(not(all(feature = "std", feature = "derive")))] mod tests { pub fn run_test() { } }
/// # #[cfg(not(all(feature = "std2", feature = "derive")))] mod tests { pub fn run_test() { } }
/// #
/// # #[cfg(all(feature = "std", feature = "derive"))] mod tests {
/// # #[cfg(all(feature = "std2", feature = "derive"))] mod tests {
/// #
/// # use failure::{self, ResultExt};
/// #
Expand Down
1 change: 1 addition & 0 deletions travis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ test_failure_in() {
cd $1
cargo_test
cargo_test --no-default-features
cargo_test --features std2,derive
cargo_test --features backtrace
test_derive_in "$1/failure_derive"
cd $DIR
Expand Down