Skip to content

Commit a494218

Browse files
authored
[error] Implement core::error::Error for errors (#1663)
1 parent f8134e2 commit a494218

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

.github/workflows/ci.yml

+27-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jobs:
5454
# These are the names of specific Rust versions detected in
5555
# `build.rs`. Each of these represents the minimum Rust version for
5656
# which a particular feature is supported.
57+
"zerocopy-core-error",
5758
"zerocopy-generic-bounds-in-const-fn",
5859
"zerocopy-target-has-atomics",
5960
"zerocopy-aarch64-simd",
@@ -87,6 +88,8 @@ jobs:
8788
features: "--all-features"
8889
- toolchain: "stable"
8990
features: "--all-features"
91+
- toolchain: "zerocopy-core-error"
92+
features: "--all-features"
9093
- toolchain: "zerocopy-generic-bounds-in-const-fn"
9194
features: "--all-features"
9295
- toolchain: "zerocopy-target-has-atomics"
@@ -107,6 +110,8 @@ jobs:
107110
# other than "msrv", "stable", and "nightly". These other versions
108111
# exist to exercise zerocopy behavior which differs by toolchain;
109112
# zerocopy-derive doesn't behave different on these toolchains.
113+
- crate: "zerocopy-derive"
114+
toolchain: "zerocopy-core-error"
110115
- crate: "zerocopy-derive"
111116
toolchain: "zerocopy-generic-bounds-in-const-fn"
112117
- crate: "zerocopy-derive"
@@ -137,7 +142,28 @@ jobs:
137142
target: "thumbv6m-none-eabi"
138143
- toolchain: "zerocopy-aarch64-simd"
139144
target: "wasm32-wasi"
140-
# Exclude most targets targets from the
145+
# Exclude most targets targets from the `zerocopy-core-error`
146+
# toolchain since the `zerocopy-core-error` feature is unrelated to
147+
# compilation target. This only leaves i686 and x86_64 targets.
148+
- toolchain: "zerocopy-core-error"
149+
target: "arm-unknown-linux-gnueabi"
150+
- toolchain: "zerocopy-core-error"
151+
target: "aarch64-unknown-linux-gnu"
152+
- toolchain: "zerocopy-core-error"
153+
target: "powerpc-unknown-linux-gnu"
154+
- toolchain: "zerocopy-core-error"
155+
target: "powerpc64-unknown-linux-gnu"
156+
- toolchain: "zerocopy-core-error"
157+
target: "riscv64gc-unknown-linux-gnu"
158+
- toolchain: "zerocopy-core-error"
159+
target: "s390x-unknown-linux-gnu"
160+
- toolchain: "zerocopy-core-error"
161+
target: "x86_64-pc-windows-msvc"
162+
- toolchain: "zerocopy-core-error"
163+
target: "thumbv6m-none-eabi"
164+
- toolchain: "zerocopy-core-error"
165+
target: "wasm32-wasi"
166+
# Exclude most targets targets from the
141167
# `zerocopy-generic-bounds-in-const-fn` toolchain since the
142168
# `zerocopy-generic-bounds-in-const-fn` feature is unrelated to
143169
# compilation target. This only leaves i686 and x86_64 targets.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ exclude = [".*"]
3232
# as high as the specified version. In the emitted `--cfg`, dashes are replaced
3333
# by underscores.
3434

35+
# From 1.81.0, Rust supports the `core::error::Error` trait.
36+
zerocopy-core-error = "1.81.0"
37+
3538
# From 1.61.0, Rust supports generic types with trait bounds in `const fn`.
3639
zerocopy-generic-bounds-in-const-fn = "1.61.0"
3740

build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ use std::{env, fs, process::Command, str};
6060
fn main() {
6161
// Avoid unnecessary re-building.
6262
println!("cargo:rerun-if-changed=build.rs");
63+
// This is necessary because changes to the list of detected Rust toolchain
64+
// versions will affect what `--cfg`s this script emits. Without this,
65+
// changes to that list have no effect on the build without running `cargo
66+
// clean` or similar.
67+
println!("cargo:rerun-if-changed=Cargo.toml");
6368

6469
let version_cfgs = parse_version_cfgs_from_cargo_toml();
6570
let rustc_version = rustc_version();

src/error.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ use core::{
6565
ops::Deref,
6666
};
6767

68+
#[cfg(zerocopy_core_error)]
69+
use core::error::Error;
70+
#[cfg(all(not(zerocopy_core_error), any(feature = "std", test)))]
71+
use std::error::Error;
72+
6873
use crate::{util::SendSyncPhantomData, KnownLayout, TryFromBytes};
6974
#[cfg(doc)]
7075
use crate::{FromBytes, Ref};
@@ -126,9 +131,8 @@ impl<A: fmt::Display, S: fmt::Display, V: fmt::Display> fmt::Display for Convert
126131
}
127132
}
128133

129-
#[cfg(any(feature = "std", test))]
130-
#[allow(clippy::std_instead_of_core)]
131-
impl<A, S, V> std::error::Error for ConvertError<A, S, V>
134+
#[cfg(any(zerocopy_core_error, feature = "std", test))]
135+
impl<A, S, V> Error for ConvertError<A, S, V>
132136
where
133137
A: fmt::Display + fmt::Debug,
134138
S: fmt::Display + fmt::Debug,
@@ -228,9 +232,8 @@ where
228232
}
229233
}
230234

231-
#[cfg(any(feature = "std", test))]
232-
#[allow(clippy::std_instead_of_core)]
233-
impl<Src, Dst: ?Sized> std::error::Error for AlignmentError<Src, Dst>
235+
#[cfg(any(zerocopy_core_error, feature = "std", test))]
236+
impl<Src, Dst: ?Sized> Error for AlignmentError<Src, Dst>
234237
where
235238
Src: Deref,
236239
Dst: KnownLayout,
@@ -353,9 +356,8 @@ where
353356
}
354357
}
355358

356-
#[cfg(any(feature = "std", test))]
357-
#[allow(clippy::std_instead_of_core)]
358-
impl<Src, Dst: ?Sized> std::error::Error for SizeError<Src, Dst>
359+
#[cfg(any(zerocopy_core_error, feature = "std", test))]
360+
impl<Src, Dst: ?Sized> Error for SizeError<Src, Dst>
359361
where
360362
Src: Deref,
361363
Dst: KnownLayout,
@@ -441,9 +443,8 @@ where
441443
}
442444
}
443445

444-
#[cfg(any(feature = "std", test))]
445-
#[allow(clippy::std_instead_of_core)]
446-
impl<Src, Dst: ?Sized> std::error::Error for ValidityError<Src, Dst>
446+
#[cfg(any(zerocopy_core_error, feature = "std", test))]
447+
impl<Src, Dst: ?Sized> Error for ValidityError<Src, Dst>
447448
where
448449
Src: Deref,
449450
Dst: KnownLayout + TryFromBytes,

0 commit comments

Comments
 (0)