Skip to content

Commit

Permalink
Merge pull request #48 from Mark-Simulacrum/serde-derive
Browse files Browse the repository at this point in the history
Utilize cfg_attr instead of a build script.
  • Loading branch information
alexcrichton authored May 20, 2017
2 parents 07ad200 + 18e1931 commit c246824
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 83 deletions.
10 changes: 3 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ documentation = "http://alexcrichton.com/backtrace-rs"
description = """
A library to acquire a stack trace (backtrace) at runtime in a Rust program.
"""
build = "build.rs"

[dependencies]
libc = "0.2"
cfg-if = "0.1"
rustc-demangle = "0.1.4"

# Optionally enable the ability to serialize a `Backtrace`
serde = { version = "0.8", optional = true }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
rustc-serialize = { version = "0.3", optional = true }

# Optionally demangle C++ frames' symbols in backtraces.
Expand All @@ -33,9 +32,6 @@ winapi = { version = "0.2.5", optional = true }
[target.'cfg(all(unix, not(target_os = "emscripten"), not(target_os = "macos"), not(target_os = "ios")))'.dependencies]
backtrace-sys = { path = "backtrace-sys", version = "0.1.3", optional = true }

[build-dependencies]
serde_codegen = { version = "0.8", optional = true }

# Each feature controls the two phases of finding a backtrace: getting a
# backtrace and then resolving instruction pointers to symbols. The default
# feature enables all the necessary features for each platform this library
Expand Down Expand Up @@ -88,4 +84,4 @@ default = ["libunwind", "libbacktrace", "coresymbolication", "dladdr", "dbghelp"
#
# Various features used for enabling rustc-serialize or syntex codegen.
serialize-rustc = ["rustc-serialize"]
serialize-serde = ["serde", "serde_codegen"]
serialize-serde = ["serde", "serde_derive"]
51 changes: 0 additions & 51 deletions build.rs

This file was deleted.

27 changes: 6 additions & 21 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,13 @@ use std::path::{Path, PathBuf};

use {trace, resolve, SymbolName};

// Ok so the `//~ HACK` directives here are, well, hacks. Right now we want to
// compile on stable for serde support, but we also want to use
// #[derive(Serialize, Deserialize)] macros *along* with the
// `#[derive(RustcEncodable, RustcDecodable)]` macros. In theory both of these
// can be behind a #[cfg_attr], but that unfortunately doesn't work for two
// reasons:
//
// 1. rust-lang/rust#32957 - means the include! of this module doesn't expand
// the RustcDecodable/RustcEncodable blocks.
// 2. serde-rs/serde#148 - means that Serialize/Deserialize won't get expanded.
//
// We just hack around it by doing #[cfg_attr] manually essentially. Our build
// script will just strip the `//~ HACKn` prefixes here if the corresponding
// feature is enabled.

/// Representation of an owned and self-contained backtrace.
///
/// This structure can be used to capture a backtrace at various points in a
/// program and later used to inspect what the backtrace was at that time.
#[derive(Clone)]
//~ HACK1 #[derive(RustcDecodable, RustcEncodable)]
//~ HACK2 #[derive(Deserialize, Serialize)]
#[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))]
#[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))]
pub struct Backtrace {
frames: Vec<BacktraceFrame>,
}
Expand All @@ -36,8 +21,8 @@ pub struct Backtrace {
/// This type is returned as a list from `Backtrace::frames` and represents one
/// stack frame in a captured backtrace.
#[derive(Clone)]
//~ HACK1 #[derive(RustcDecodable, RustcEncodable)]
//~ HACK2 #[derive(Deserialize, Serialize)]
#[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))]
#[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))]
pub struct BacktraceFrame {
ip: usize,
symbol_address: usize,
Expand All @@ -49,8 +34,8 @@ pub struct BacktraceFrame {
/// This type is returned as a list from `BacktraceFrame::symbols` and
/// represents the metadata for a symbol in a backtrace.
#[derive(Clone)]
//~ HACK1 #[derive(RustcDecodable, RustcEncodable)]
//~ HACK2 #[derive(Deserialize, Serialize)]
#[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))]
#[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))]
pub struct BacktraceSymbol {
name: Option<Vec<u8>>,
addr: Option<usize>,
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ extern crate libc;
#[cfg(feature = "serde")]
extern crate serde;

#[cfg(feature = "serde_derive")]
#[cfg_attr(feature = "serde_derive", macro_use)]
extern crate serde_derive;

#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize;

Expand All @@ -100,9 +104,7 @@ pub use symbolize::{resolve, Symbol, SymbolName};
mod symbolize;

pub use capture::{Backtrace, BacktraceFrame, BacktraceSymbol};
mod capture {
include!(concat!(env!("OUT_DIR"), "/capture.rs"));
}
mod capture;

#[allow(dead_code)]
struct Bomb {
Expand Down
2 changes: 1 addition & 1 deletion tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn is_serde() {
extern crate serde;

fn is_serialize<T: serde::ser::Serialize>() {}
fn is_deserialize<T: serde::de::Deserialize>() {}
fn is_deserialize<T: serde::de::DeserializeOwned>() {}

is_serialize::<backtrace::Backtrace>();
is_deserialize::<backtrace::Backtrace>();
Expand Down

0 comments on commit c246824

Please sign in to comment.