Skip to content

Commit 2b9db57

Browse files
authored
Implement core::error::Error on Rust 1.81+ (#747)
This commit updates when the `Error` trait is implemented to account for how in Rust 1.81-and-prior the `Error` trait is always available, even in `core`. This is currently done with detection at build-time of the current Rust compiler version because the MSRV for this crate is below 1.81. In the future when the MSRV is increased, however, the build script can be deleted.
1 parent 01c8b19 commit 2b9db57

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ edition = "2018"
55
keywords = ["object", "elf", "mach-o", "pe", "coff"]
66
license = "Apache-2.0 OR MIT"
77
repository = "https://github.com/gimli-rs/object"
8+
# NB: if this number increases to 1.81-or-later delete the `build.rs` script
9+
# as it's no longer necessary.
810
rust-version = "1.65"
911
description = "A unified interface for reading and writing object file formats."
1012
include = [

build.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::process::Command;
2+
use std::str;
3+
4+
fn main() {
5+
// Temporary check to see if the rustc version >= 1.81 in which case the
6+
// `Error` trait is always available. This is temporary because in the
7+
// future the MSRV of this crate will be beyond 1.81 in which case this
8+
// build script can be deleted.
9+
let minor = rustc_minor_version().unwrap_or(0);
10+
if minor >= 81 {
11+
println!("cargo:rustc-cfg=core_error");
12+
}
13+
if minor >= 80 {
14+
println!("cargo:rustc-check-cfg=cfg(core_error)");
15+
}
16+
}
17+
18+
fn rustc_minor_version() -> Option<u32> {
19+
let rustc = std::env::var("RUSTC").unwrap();
20+
let output = Command::new(rustc).arg("--version").output().ok()?;
21+
let version = str::from_utf8(&output.stdout).ok()?;
22+
let mut pieces = version.split('.');
23+
if pieces.next() != Some("rustc 1") {
24+
return None;
25+
}
26+
pieces.next()?.parse().ok()
27+
}

src/build/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ impl fmt::Display for Error {
2424

2525
#[cfg(feature = "std")]
2626
impl error::Error for Error {}
27+
#[cfg(all(not(feature = "std"), core_error))]
28+
impl core::error::Error for Error {}
2729

2830
impl From<read::Error> for Error {
2931
fn from(error: read::Error) -> Error {

src/read/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ impl fmt::Display for Error {
124124

125125
#[cfg(feature = "std")]
126126
impl std::error::Error for Error {}
127+
#[cfg(all(not(feature = "std"), core_error))]
128+
impl core::error::Error for Error {}
127129

128130
/// The result type used within the read module.
129131
pub type Result<T> = result::Result<T, Error>;

src/write/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ impl fmt::Display for Error {
6060

6161
#[cfg(feature = "std")]
6262
impl error::Error for Error {}
63+
#[cfg(all(not(feature = "std"), core_error))]
64+
impl core::error::Error for Error {}
6365

6466
/// The result type used within the write module.
6567
pub type Result<T> = result::Result<T, Error>;

0 commit comments

Comments
 (0)