Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement From<Frame> for BacktraceFrame #420

Merged
merged 4 commits into from
Apr 26, 2021
Merged
Changes from 2 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
48 changes: 48 additions & 0 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ impl From<Vec<BacktraceFrame>> for Backtrace {
}
}

impl From<crate::Frame> for BacktraceFrame {
fn from(frame: crate::Frame) -> BacktraceFrame {
BacktraceFrame {
frame: Frame::Raw(frame),
symbols: None,
}
}
}

impl Into<Vec<BacktraceFrame>> for Backtrace {
fn into(self) -> Vec<BacktraceFrame> {
self.frames
Expand Down Expand Up @@ -518,3 +527,42 @@ mod serde_impls {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_frame_conversion() {
// captures an original backtrace, and makes sure that the manual conversion
// to frames yields the same results.
let bt = Backtrace::new();
let original_frames = bt.frames();

let mut frames = vec![];
crate::trace(|frame| {
let converted = BacktraceFrame::from(frame.clone());
frames.push(converted);
true
});

// the first frames can be different because we call from slightly different places,
// and the `trace` version has an extra capture. But because of inlining the number of
// frames that differ may be different between release and debug versions. Plus who knows
// what the compiler will do in the future. So we just take 4 frames from the end and make
// sure they match
for (converted, og) in frames
.iter()
.rev()
.take(4)
.zip(original_frames.iter().rev().take(4))
{
println!(
"converted {:?}, og {:?}",
converted.symbol_address(),
og.symbol_address()
);
assert_eq!(converted.symbol_address(), og.symbol_address());
}
}
}